1 | import { anyString, spy, verify, resetCalls } from "ts-mockito";
|
2 |
|
3 | import { setupFixture } from "backfill-utils-test";
|
4 | import { getCacheStorageProvider } from "backfill-cache";
|
5 | import { Hasher } from "backfill-hasher";
|
6 | import { createConfig } from "backfill-config";
|
7 |
|
8 | import { backfill } from "../index";
|
9 | import { createBuildCommand } from "../commandRunner";
|
10 |
|
11 | describe("backfill", () => {
|
12 | it("with cache miss and then cache hit", async () => {
|
13 |
|
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 |
|
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 |
|
39 | const spiedCacheStorage = spy(cacheStorage);
|
40 | const spiedBuildCommand = jest.fn(buildCommand);
|
41 | const spiedHasher = spy(hasher);
|
42 |
|
43 |
|
44 | await backfill(config, cacheStorage, spiedBuildCommand, hasher);
|
45 |
|
46 |
|
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 |
|
57 | await backfill(config, cacheStorage, buildCommand, hasher);
|
58 |
|
59 |
|
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 | });
|