forked from cloudflare/workers-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
guess-worker-format.test.ts
120 lines (111 loc) · 3.86 KB
/
guess-worker-format.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import { writeFile } from "fs/promises";
import path from "path";
import guessWorkerFormat from "../deployment-bundle/guess-worker-format";
import { mockConsoleMethods } from "./helpers/mock-console";
import { runInTempDir } from "./helpers/run-in-tmp";
describe("guess worker format", () => {
runInTempDir();
const std = mockConsoleMethods();
it('should detect a "modules" worker', async () => {
await writeFile("./index.ts", "export default {};");
// Note that this isn't actually a valid worker, because it's missing
// a fetch handler. Regardless, our heuristic is simply to check for exports.
const guess = await guessWorkerFormat(
path.join(process.cwd(), "./index.ts"),
process.cwd(),
undefined
);
expect(guess).toBe("modules");
});
it('should detect a "service-worker" worker', async () => {
await writeFile("./index.ts", "");
// Note that this isn't actually a valid worker, because it's missing
// a fetch listener. Regardless, our heuristic is simply to check for
// the lack of exports.
const guess = await guessWorkerFormat(
path.join(process.cwd(), "./index.ts"),
process.cwd(),
undefined
);
expect(guess).toBe("service-worker");
});
it('should detect a "service-worker" worker using `typeof module`', async () => {
await writeFile("./index.ts", "typeof module");
const guess = await guessWorkerFormat(
path.join(process.cwd(), "./index.ts"),
process.cwd(),
undefined
);
expect(guess).toBe("service-worker");
});
it('should detect a "service-worker" worker using imports', async () => {
await writeFile(
"./dep.ts",
`
const value = 'thing';
export default value;
`
);
await writeFile(
"./index.ts",
`
import value from './dep.ts';
addEventListener('fetch', (event) => {
event.respondWith(new Response(value));
});
`
);
const guess = await guessWorkerFormat(
path.join(process.cwd(), "./index.ts"),
process.cwd(),
undefined
);
expect(guess).toBe("service-worker");
});
it("should throw an error when the hint doesn't match the guess (modules - service-worker)", async () => {
await writeFile("./index.ts", "export default {};");
await expect(
guessWorkerFormat(
path.join(process.cwd(), "./index.ts"),
process.cwd(),
"service-worker"
)
).rejects.toThrow(
"You configured this worker to be a 'service-worker', but the file you are trying to build appears to have a `default` export like a module worker. Please pass `--format modules`, or simply remove the configuration."
);
});
it("should throw an error when the hint doesn't match the guess (service-worker - modules)", async () => {
await writeFile("./index.ts", "");
await expect(
guessWorkerFormat(
path.join(process.cwd(), "./index.ts"),
process.cwd(),
"modules"
)
).rejects.toThrow(
"You configured this worker to be 'modules', but the file you are trying to build doesn't export a handler. Please pass `--format service-worker`, or simply remove the configuration."
);
});
it("should not error if a .js entry point has jsx", async () => {
await writeFile("./index.js", "console.log(<div/>)");
const guess = await guessWorkerFormat(
path.join(process.cwd(), "./index.js"),
process.cwd(),
undefined
);
expect(guess).toBe("service-worker");
});
it("logs a warning when a worker has exports, but not a default one", async () => {
await writeFile("./index.ts", "export const foo = 1;");
const guess = await guessWorkerFormat(
path.join(process.cwd(), "./index.ts"),
process.cwd(),
undefined
);
expect(guess).toBe("service-worker");
expect(std.warn).toMatchInlineSnapshot(`
"[33m▲ [43;33m[[43;30mWARNING[43;33m][0m [1mThe entrypoint index.ts has exports like an ES Module, but hasn't defined a default export like a module worker normally would. Building the worker using \\"service-worker\\" format...[0m
"
`);
});
});