Skip to content

Commit 7200196

Browse files
committed
fix and add tests
1 parent 6670e41 commit 7200196

File tree

4 files changed

+699
-316
lines changed

4 files changed

+699
-316
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: 62 additions & 125 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,96 @@ 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-
});
39-
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`);
44-
45-
const fileExists = existsSync(
46-
path.join(helper.tmpPath, "./.wrangler/types/runtime.d.ts")
47-
);
48-
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-
);
37+
expect(output.stdout).toContain("Generating runtime types...");
38+
expect(output.stdout).toContain("Runtime types generated.");
6139
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-
});
65-
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"`
40+
"✨ Types written to worker-configuration.d.ts"
7141
);
72-
73-
const fileExists = existsSync(path.join(helper.tmpPath, "./types.d.ts"));
74-
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"]`);
42+
expect(output.stdout).toContain("📖 Read about runtime types");
7843
});
7944

80-
it("should generate types", async () => {
45+
it("should generate runtime types and env types in one file at the default path", async () => {
8146
const helper = new WranglerE2ETestHelper();
8247
await helper.seed(seed);
83-
await helper.run(`wrangler types --x-include-runtime="./types.d.ts"`);
84-
85-
const file = (
86-
await readFile(path.join(helper.tmpPath, "./types.d.ts"))
87-
).toString();
88-
89-
expect(file).contains('declare module "cloudflare:workers"');
90-
});
91-
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-
}
101-
}
102-
`,
103-
});
104-
const output = await helper.run(
105-
`wrangler types --x-include-runtime="./types.d.ts"`
106-
);
107-
48+
const output = await helper.run(`wrangler types`);
49+
expect(output.stdout).toContain("Generating project types...");
50+
expect(output.stdout).toContain("interface Env {");
51+
expect(output.stdout).toContain("Generating runtime types...");
52+
expect(output.stdout).toContain("Runtime types generated.");
10853
expect(output.stdout).toContain(
109-
`📣 You can now uninstall "@cloudflare/workers-types".`
54+
"✨ Types written to worker-configuration.d.ts"
11055
);
111-
});
112-
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`
118-
{
119-
"compilerOptions": {
120-
"types": ["node"]
121-
}
122-
}
123-
`,
124-
});
125-
const output = await helper.run(
126-
`wrangler types --x-include-runtime="./types.d.ts"`
127-
);
128-
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`
56+
const file = readFileSync(
57+
path.join(helper.tmpPath, "./worker-configuration.d.ts"),
58+
"utf8"
13159
);
60+
expect(file).contains('declare module "cloudflare:workers"');
61+
expect(file).contains("interface Env");
13262
});
13363

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-
});
15364
it("should include header with version information in the generated types", async () => {
15465
const helper = new WranglerE2ETestHelper();
15566
await helper.seed(seed);
156-
await helper.run(`wrangler types --x-include-runtime="./types.d.ts"`);
67+
await helper.run(`wrangler types "./types.d.ts" `);
15768

158-
const file = (
159-
await readFile(path.join(helper.tmpPath, "./types.d.ts"))
160-
).toString();
69+
const file = (await readFile(path.join(helper.tmpPath, "./types.d.ts")))
70+
.toString()
71+
.split("\n");
16172

162-
expect(file.split("\n")[0]).match(
73+
expect(file[0]).toMatchInlineSnapshot(
74+
`"// Generated by Wrangler by running \`wrangler types ./types.d.ts\`"`
75+
);
76+
expect(file[1]).match(
16377
/\/\/ Runtime types generated with workerd@1\.\d+\.\d \d\d\d\d-\d\d-\d\d ([a-z_]+,?)*/
16478
);
16579
});
166-
it("should not regenerate types if the header matches", async () => {
80+
81+
it("should not regenerate runtime types if the header matches, but should regenerate env types", async () => {
16782
const helper = new WranglerE2ETestHelper();
16883
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"
84+
await helper.run(`wrangler types`);
85+
86+
const typesPath = path.join(helper.tmpPath, "worker-configuration.d.ts");
87+
const file = (await readFile(typesPath)).toString().split("\n");
88+
89+
await writeFile(
90+
typesPath,
91+
[
92+
file[0],
93+
file[1],
94+
"FAKE ENV",
95+
"// Begin runtime types",
96+
"FAKE RUNTIME",
97+
].join("\n")
98+
);
99+
console.log(
100+
[
101+
file[0],
102+
file[1],
103+
"FAKE ENV",
104+
"// Begin runtime types",
105+
"FAKE RUNTIME",
106+
].join("\n")
174107
);
175-
const file = (await readFile(runtimeTypesFile)).toString();
176-
177-
const header = file.split("\n")[0];
178108

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

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

183-
const file2 = (await readFile(runtimeTypesFile)).toString();
113+
expect(file2).toMatchInlineSnapshot(`
114+
"// Generated by Wrangler by running \`wrangler types\`
115+
// Runtime types generated with [email protected] 2023-01-01 nodejs_compat,no_global_navigator
116+
// eslint-disable-next-line @typescript-eslint/no-empty-interface,@typescript-eslint/no-empty-object-type
117+
interface Env {
118+
}
184119
185-
expect(file2.split("\n")[1]).toBe("SOME_RANDOM_DATA");
120+
// Begin runtime types
121+
FAKE RUNTIME"
122+
`);
186123
});
187124
});

0 commit comments

Comments
 (0)