Skip to content

Commit 2a098e4

Browse files
committed
fix and add tests
1 parent 1b0d071 commit 2a098e4

File tree

3 files changed

+735
-302
lines changed

3 files changed

+735
-302
lines changed

fixtures/type-generation/tests/type-generation.file-comment.test.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ describe("`wrangler types` - file comment", () => {
2828
"packages",
2929
"wrangler"
3030
);
31-
execSync(`npx ${wranglerPath} types ${args}`, {
31+
execSync(`npx ${wranglerPath} types ${args} --include-runtime=false`, {
3232
cwd: tempDir,
3333
});
3434
const typesFile = join(tempDir, expectedOutputFile);
@@ -38,7 +38,9 @@ describe("`wrangler types` - file comment", () => {
3838
describe("includes a comment specifying the command run", () => {
3939
it("(base command)", async ({ expect }) => {
4040
const typesCommandOutput = runWranglerTypesCommand();
41-
expect(typesCommandOutput).toContain("by running `wrangler types`");
41+
expect(typesCommandOutput).toContain(
42+
"by running `wrangler types --include-runtime=false`"
43+
);
4244
});
4345

4446
it("(with types customization)", async ({ expect }) => {
@@ -47,14 +49,14 @@ describe("`wrangler types` - file comment", () => {
4749
"./cflare-env.d.ts"
4850
);
4951
expect(typesCommandOutput).toContain(
50-
"by running `wrangler types --env-interface MyCloudflareEnv ./cflare-env.d.ts`"
52+
"by running `wrangler types --env-interface MyCloudflareEnv ./cflare-env.d.ts --include-runtime=false`"
5153
);
5254
});
5355

5456
it("(with wrangler top level options)", async ({ expect }) => {
5557
const typesCommandOutput = runWranglerTypesCommand("-c wranglerA.toml");
5658
expect(typesCommandOutput).toContain(
57-
"by running `wrangler types -c wranglerA.toml`"
59+
"by running `wrangler types -c wranglerA.toml --include-runtime=false`"
5860
);
5961
});
6062
});
Lines changed: 99 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { existsSync } from "node:fs";
1+
import { readFileSync } from "node:fs";
22
import { readFile, writeFile } from "node:fs/promises";
33
import path from "node:path";
44
import { describe, expect, it } from "vitest";
@@ -29,159 +29,146 @@ const seed = {
2929
};
3030

3131
describe("types", () => {
32-
it("should not generate runtime types without flag", async () => {
32+
it("should generate runtime types without a flag", async () => {
3333
const helper = new WranglerE2ETestHelper();
3434
await helper.seed(seed);
3535
const output = await helper.run(`wrangler types`);
3636

37-
expect(output.stdout).not.toContain(`Generating runtime types...`);
38-
});
37+
expect(output.stdout).toMatchInlineSnapshot(`
38+
"
39+
⛅️ wrangler 3.109.1
40+
--------------------
3941
40-
it("should generate runtime types at the default path", async () => {
41-
const helper = new WranglerE2ETestHelper();
42-
await helper.seed(seed);
43-
const output = await helper.run(`wrangler types --x-include-runtime`);
42+
Generating project types...
4443
45-
const fileExists = existsSync(
46-
path.join(helper.tmpPath, "./.wrangler/types/runtime.d.ts")
47-
);
44+
interface Env {
45+
}
4846
49-
expect(fileExists).toEqual(true);
50-
expect(output.stdout).toContain(`Generating runtime types...`);
51-
expect(output.stdout).toContain(`Generating project types...`);
52-
expect(output.stdout).toContain(
53-
`✨ Runtime types written to ./.wrangler/types/runtime.d.ts`
54-
);
55-
expect(output.stdout).toContain(
56-
`"types": ["./.wrangler/types/runtime.d.ts"]`
57-
);
58-
expect(output.stdout).toContain(
59-
`📣 Since you have Node.js compatibility mode enabled, you should consider adding Node.js for TypeScript by running "npm i --save-dev @types/[email protected]". Please see the docs for more details: https://developers.cloudflare.com/workers/languages/typescript/#transitive-loading-of-typesnode-overrides-cloudflareworkers-types`
60-
);
61-
expect(output.stdout).toContain(
62-
`Remember to run 'wrangler types --x-include-runtime' again if you change 'compatibility_date' or 'compatibility_flags' in your wrangler.toml file.`
63-
);
64-
});
47+
Generating runtime types...
6548
66-
it("should generate runtime types at the provided path", async () => {
67-
const helper = new WranglerE2ETestHelper();
68-
await helper.seed(seed);
69-
const output = await helper.run(
70-
`wrangler types --x-include-runtime="./types.d.ts"`
71-
);
49+
Runtime types generated.
50+
────────────────────────────────────────────────────────────
51+
✨ Types written to worker-configuration.d.ts
52+
53+
Action required Update your tsconfig.json to include the generated types
54+
{
55+
"compilerOptions": {
56+
"types": ["worker-configuration.d.ts"]
57+
}
58+
}
7259
73-
const fileExists = existsSync(path.join(helper.tmpPath, "./types.d.ts"));
60+
Action required Install types@node
61+
Since you have the \`nodejs_compat\` flag, you should install Node.js types by running "npm i --save-dev @types/node".
7462
75-
expect(fileExists).toEqual(true);
76-
expect(output.stdout).toContain(`✨ Runtime types written to ./types.d.ts`);
77-
expect(output.stdout).toContain(`"types": ["./types.d.ts"]`);
63+
📖 Read about runtime types
64+
https://developers.cloudflare.com/workers/languages/typescript/#generate-runtime-types
65+
📣 Remember to rerun 'wrangler types' after you change your wrangler.toml file.
66+
"
67+
`);
7868
});
7969

80-
it("should generate types", async () => {
70+
it("should generate runtime types and env types in one file at the default path", async () => {
8171
const helper = new WranglerE2ETestHelper();
8272
await helper.seed(seed);
83-
await helper.run(`wrangler types --x-include-runtime="./types.d.ts"`);
73+
const output = await helper.run(`wrangler types`);
8474

85-
const file = (
86-
await readFile(path.join(helper.tmpPath, "./types.d.ts"))
87-
).toString();
75+
expect(output.stdout).toMatchInlineSnapshot(`
76+
"
77+
⛅️ wrangler 3.109.1
78+
--------------------
8879
89-
expect(file).contains('declare module "cloudflare:workers"');
90-
});
80+
Generating project types...
9181
92-
it("should recommend to uninstall @cloudflare/workers-types", async () => {
93-
const helper = new WranglerE2ETestHelper();
94-
await helper.seed({
95-
...seed,
96-
"tsconfig.json": dedent`
97-
{
98-
"compilerOptions": {
99-
"types": ["@cloudflare/workers-types"]
100-
}
82+
interface Env {
10183
}
102-
`,
103-
});
104-
const output = await helper.run(
105-
`wrangler types --x-include-runtime="./types.d.ts"`
106-
);
10784
108-
expect(output.stdout).toContain(
109-
`📣 You can now uninstall "@cloudflare/workers-types".`
110-
);
111-
});
85+
Generating runtime types...
11286
113-
it("should not recommend to install @types/node if 'node' exists in types array", async () => {
114-
const helper = new WranglerE2ETestHelper();
115-
await helper.seed({
116-
...seed,
117-
"tsconfig.json": dedent`
87+
Runtime types generated.
88+
────────────────────────────────────────────────────────────
89+
✨ Types written to worker-configuration.d.ts
90+
91+
Action required Update your tsconfig.json to include the generated types
11892
{
11993
"compilerOptions": {
120-
"types": ["node"]
94+
"types": ["worker-configuration.d.ts"]
12195
}
12296
}
123-
`,
124-
});
125-
const output = await helper.run(
126-
`wrangler types --x-include-runtime="./types.d.ts"`
127-
);
12897
129-
expect(output.stdout).not.toContain(
130-
`📣 Since you have Node.js compatibility mode enabled, you should consider adding Node.js for TypeScript by running "npm i --save-dev @types/[email protected]". Please see the docs for more details: https://developers.cloudflare.com/workers/languages/typescript/#transitive-loading-of-typesnode-overrides-cloudflareworkers-types`
98+
Action required Install types@node
99+
Since you have the \`nodejs_compat\` flag, you should install Node.js types by running "npm i --save-dev @types/node".
100+
101+
📖 Read about runtime types
102+
https://developers.cloudflare.com/workers/languages/typescript/#generate-runtime-types
103+
📣 Remember to rerun 'wrangler types' after you change your wrangler.toml file.
104+
"
105+
`);
106+
const file = readFileSync(
107+
path.join(helper.tmpPath, "./worker-configuration.d.ts"),
108+
"utf8"
131109
);
110+
expect(file).contains('declare module "cloudflare:workers"');
111+
expect(file).contains("interface Env");
132112
});
133113

134-
it("should not error with nodejs_compat flags", async () => {
135-
const helper = new WranglerE2ETestHelper();
136-
await helper.seed({
137-
...seed,
138-
"wrangler.toml": dedent`
139-
name = "test-worker"
140-
main = "src/index.ts"
141-
compatibility_date = "2023-01-01"
142-
compatibility_flags = ["nodejs_compat", "experimental:nodejs_compat_v2"]
143-
`,
144-
});
145-
146-
const output = await helper.run(
147-
`wrangler types --x-include-runtime="./types.d.ts"`
148-
);
149-
150-
expect(output.stderr).toBe("");
151-
expect(output.status).toBe(0);
152-
});
153114
it("should include header with version information in the generated types", async () => {
154115
const helper = new WranglerE2ETestHelper();
155116
await helper.seed(seed);
156-
await helper.run(`wrangler types --x-include-runtime="./types.d.ts"`);
117+
await helper.run(`wrangler types "./types.d.ts" `);
157118

158-
const file = (
159-
await readFile(path.join(helper.tmpPath, "./types.d.ts"))
160-
).toString();
119+
const file = (await readFile(path.join(helper.tmpPath, "./types.d.ts")))
120+
.toString()
121+
.split("\n");
161122

162-
expect(file.split("\n")[0]).match(
123+
expect(file[0]).toMatchInlineSnapshot(
124+
`"// Generated by Wrangler by running \`wrangler types ./types.d.ts\`"`
125+
);
126+
expect(file[1]).match(
163127
/\/\/ Runtime types generated with workerd@1\.\d+\.\d \d\d\d\d-\d\d-\d\d ([a-z_]+,?)*/
164128
);
165129
});
166-
it("should not regenerate types if the header matches", async () => {
130+
131+
it("should not regenerate runtime types if the header matches, but should regenerate env types", async () => {
167132
const helper = new WranglerE2ETestHelper();
168133
await helper.seed(seed);
169-
await helper.run(`wrangler types --x-include-runtime`);
170-
171-
const runtimeTypesFile = path.join(
172-
helper.tmpPath,
173-
"./.wrangler/types/runtime.d.ts"
134+
await helper.run(`wrangler types`);
135+
136+
const typesPath = path.join(helper.tmpPath, "worker-configuration.d.ts");
137+
const file = (await readFile(typesPath)).toString().split("\n");
138+
139+
await writeFile(
140+
typesPath,
141+
[
142+
file[0],
143+
file[1],
144+
"FAKE ENV",
145+
"// Begin runtime types",
146+
"FAKE RUNTIME",
147+
].join("\n")
148+
);
149+
console.log(
150+
[
151+
file[0],
152+
file[1],
153+
"FAKE ENV",
154+
"// Begin runtime types",
155+
"FAKE RUNTIME",
156+
].join("\n")
174157
);
175-
const file = (await readFile(runtimeTypesFile)).toString();
176-
177-
const header = file.split("\n")[0];
178158

179-
await writeFile(runtimeTypesFile, header + "\n" + "SOME_RANDOM_DATA");
159+
await helper.run(`wrangler types`);
180160

181-
await helper.run(`wrangler types --x-include-runtime`);
161+
const file2 = (await readFile(typesPath)).toString();
182162

183-
const file2 = (await readFile(runtimeTypesFile)).toString();
163+
expect(file2).toMatchInlineSnapshot(`
164+
"// Generated by Wrangler by running \`wrangler types\`
165+
// Runtime types generated with [email protected] 2023-01-01 nodejs_compat,no_global_navigator
166+
// eslint-disable-next-line @typescript-eslint/no-empty-interface,@typescript-eslint/no-empty-object-type
167+
interface Env {
168+
}
184169
185-
expect(file2.split("\n")[1]).toBe("SOME_RANDOM_DATA");
170+
// Begin runtime types
171+
FAKE RUNTIME"
172+
`);
186173
});
187174
});

0 commit comments

Comments
 (0)