UNPKG

1.98 kBPlain TextView Raw
1import { anyString, spy, verify, resetCalls } from "ts-mockito";
2
3import { setupFixture } from "backfill-utils-test";
4import { getCacheStorageProvider } from "backfill-cache";
5import { Hasher } from "backfill-hasher";
6import { createConfig } from "backfill-config";
7
8import { backfill } from "../index";
9import { createBuildCommand } from "../commandRunner";
10
11describe("backfill", () => {
12 it("with cache miss and then cache hit", async () => {
13 // Set up
14 await setupFixture("basic");
15
16 const config = createConfig();
17 const {
18 cacheStorageConfig,
19 clearOutputFolder,
20 internalCacheFolder,
21 outputFolder,
22 packageRoot
23 } = config;
24
25 // Arrange
26 const cacheStorage = getCacheStorageProvider(
27 cacheStorageConfig,
28 internalCacheFolder
29 );
30 const buildCommandRaw = "npm run compile";
31 const buildCommand = createBuildCommand(
32 [buildCommandRaw],
33 clearOutputFolder,
34 outputFolder
35 );
36 const hasher = new Hasher({ packageRoot, outputFolder }, buildCommandRaw);
37
38 // Spy
39 const spiedCacheStorage = spy(cacheStorage);
40 const spiedBuildCommand = jest.fn(buildCommand);
41 const spiedHasher = spy(hasher);
42
43 // Execute
44 await backfill(config, cacheStorage, spiedBuildCommand, hasher);
45
46 // Assert
47 verify(spiedHasher.createPackageHash()).once();
48 expect(spiedBuildCommand).toHaveBeenCalled();
49 verify(spiedCacheStorage.fetch(anyString(), anyString())).once();
50 verify(spiedCacheStorage.put(anyString(), anyString())).once();
51
52 resetCalls(spiedHasher);
53 resetCalls(spiedCacheStorage);
54 jest.clearAllMocks();
55
56 // Execute
57 await backfill(config, cacheStorage, buildCommand, hasher);
58
59 // Assert
60 verify(spiedHasher.createPackageHash()).once();
61 expect(spiedBuildCommand).not.toHaveBeenCalled();
62 verify(spiedCacheStorage.fetch(anyString(), anyString())).once();
63 verify(spiedCacheStorage.put(anyString(), anyString())).never();
64 });
65});