|
1 | | -import { existsSync } from "node:fs"; |
| 1 | +import { readFileSync } from "node:fs"; |
2 | 2 | import { readFile, writeFile } from "node:fs/promises"; |
3 | 3 | import path from "node:path"; |
4 | 4 | import { describe, expect, it } from "vitest"; |
@@ -29,159 +29,96 @@ const seed = { |
29 | 29 | }; |
30 | 30 |
|
31 | 31 | describe("types", () => { |
32 | | - it("should not generate runtime types without flag", async () => { |
| 32 | + it("should generate runtime types without a flag", async () => { |
33 | 33 | const helper = new WranglerE2ETestHelper(); |
34 | 34 | await helper.seed(seed); |
35 | 35 | const output = await helper.run(`wrangler types`); |
36 | 36 |
|
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."); |
61 | 39 | 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" |
71 | 41 | ); |
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"); |
78 | 43 | }); |
79 | 44 |
|
80 | | - it("should generate types", async () => { |
| 45 | + it("should generate runtime types and env types in one file at the default path", async () => { |
81 | 46 | const helper = new WranglerE2ETestHelper(); |
82 | 47 | 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."); |
108 | 53 | expect(output.stdout).toContain( |
109 | | - `📣 You can now uninstall "@cloudflare/workers-types".` |
| 54 | + "✨ Types written to worker-configuration.d.ts" |
110 | 55 | ); |
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" |
131 | 59 | ); |
| 60 | + expect(file).contains('declare module "cloudflare:workers"'); |
| 61 | + expect(file).contains("interface Env"); |
132 | 62 | }); |
133 | 63 |
|
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 | | - }); |
153 | 64 | it("should include header with version information in the generated types", async () => { |
154 | 65 | const helper = new WranglerE2ETestHelper(); |
155 | 66 | 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" `); |
157 | 68 |
|
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"); |
161 | 72 |
|
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( |
163 | 77 | /\/\/ Runtime types generated with workerd@1\.\d+\.\d \d\d\d\d-\d\d-\d\d ([a-z_]+,?)*/ |
164 | 78 | ); |
165 | 79 | }); |
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 () => { |
167 | 82 | const helper = new WranglerE2ETestHelper(); |
168 | 83 | 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") |
174 | 107 | ); |
175 | | - const file = (await readFile(runtimeTypesFile)).toString(); |
176 | | - |
177 | | - const header = file.split("\n")[0]; |
178 | 108 |
|
179 | | - await writeFile(runtimeTypesFile, header + "\n" + "SOME_RANDOM_DATA"); |
| 109 | + await helper.run(`wrangler types`); |
180 | 110 |
|
181 | | - await helper.run(`wrangler types --x-include-runtime`); |
| 111 | + const file2 = (await readFile(typesPath)).toString(); |
182 | 112 |
|
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 | + } |
184 | 119 |
|
185 | | - expect(file2.split("\n")[1]).toBe("SOME_RANDOM_DATA"); |
| 120 | + // Begin runtime types |
| 121 | + FAKE RUNTIME" |
| 122 | + `); |
186 | 123 | }); |
187 | 124 | }); |
0 commit comments