forked from cloudflare/workers-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworkers.test.ts
71 lines (56 loc) · 1.98 KB
/
workers.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { existsSync } from "fs";
import { mockSpinner } from "helpers/__tests__/mocks";
import { getLatestTypesEntrypoint } from "helpers/compatDate";
import { readFile, writeFile } from "helpers/files";
import { beforeEach, describe, expect, test, vi } from "vitest";
import { addWorkersTypesToTsConfig } from "../workers";
import { createTestContext } from "./helpers";
import type { C3Context } from "types";
vi.mock("helpers/files");
vi.mock("helpers/compatDate");
vi.mock("fs");
vi.mock("@cloudflare/cli/interactive");
beforeEach(() => {
mockSpinner();
});
const mockCompatDate = "2024-01-17";
describe("addWorkersTypesToTsConfig", () => {
let ctx: C3Context;
beforeEach(() => {
ctx = createTestContext();
ctx.args.ts = true;
vi.mocked(existsSync).mockImplementation(() => true);
vi.mocked(getLatestTypesEntrypoint).mockReturnValue(mockCompatDate);
// Mock the read of tsconfig.json
vi.mocked(readFile).mockImplementation(
() => `{ "compilerOptions": { "types": ["@cloudflare/workers-types"]} }`,
);
});
test("happy path", async () => {
await addWorkersTypesToTsConfig(ctx);
expect(writeFile).toHaveBeenCalled();
expect(vi.mocked(writeFile).mock.calls[0][1]).toContain(
`"@cloudflare/workers-types/${mockCompatDate}"`,
);
});
test("tsconfig.json not found", async () => {
vi.mocked(existsSync).mockImplementation(() => false);
await addWorkersTypesToTsConfig(ctx);
expect(writeFile).not.toHaveBeenCalled();
});
test("latest entrypoint not found", async () => {
vi.mocked(getLatestTypesEntrypoint).mockReturnValue(null);
await addWorkersTypesToTsConfig(ctx);
expect(writeFile).not.toHaveBeenCalled();
});
test("don't clobber existing entrypoints", async () => {
vi.mocked(readFile).mockImplementation(
() =>
`{ "compilerOptions": { "types" : ["@cloudflare/workers-types/2021-03-20"]} }`,
);
await addWorkersTypesToTsConfig(ctx);
expect(vi.mocked(writeFile).mock.calls[0][1]).toContain(
`"@cloudflare/workers-types/2021-03-20"`,
);
});
});