|
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,146 @@ 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 | | - }); |
| 37 | + expect(output.stdout).toMatchInlineSnapshot(` |
| 38 | + " |
| 39 | + ⛅️ wrangler 3.109.1 |
| 40 | + -------------------- |
39 | 41 |
|
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... |
44 | 43 |
|
45 | | - const fileExists = existsSync( |
46 | | - path.join(helper.tmpPath, "./.wrangler/types/runtime.d.ts") |
47 | | - ); |
| 44 | + interface Env { |
| 45 | + } |
48 | 46 |
|
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... |
65 | 48 |
|
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 | + } |
72 | 59 |
|
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". |
74 | 62 |
|
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 | + `); |
78 | 68 | }); |
79 | 69 |
|
80 | | - it("should generate types", async () => { |
| 70 | + it("should generate runtime types and env types in one file at the default path", async () => { |
81 | 71 | const helper = new WranglerE2ETestHelper(); |
82 | 72 | await helper.seed(seed); |
83 | | - await helper.run(`wrangler types --x-include-runtime="./types.d.ts"`); |
| 73 | + const output = await helper.run(`wrangler types`); |
84 | 74 |
|
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 | + -------------------- |
88 | 79 |
|
89 | | - expect(file).contains('declare module "cloudflare:workers"'); |
90 | | - }); |
| 80 | + Generating project types... |
91 | 81 |
|
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 { |
101 | 83 | } |
102 | | - `, |
103 | | - }); |
104 | | - const output = await helper.run( |
105 | | - `wrangler types --x-include-runtime="./types.d.ts"` |
106 | | - ); |
107 | 84 |
|
108 | | - expect(output.stdout).toContain( |
109 | | - `📣 You can now uninstall "@cloudflare/workers-types".` |
110 | | - ); |
111 | | - }); |
| 85 | + Generating runtime types... |
112 | 86 |
|
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 |
118 | 92 | { |
119 | 93 | "compilerOptions": { |
120 | | - "types": ["node"] |
| 94 | + "types": ["worker-configuration.d.ts"] |
121 | 95 | } |
122 | 96 | } |
123 | | - `, |
124 | | - }); |
125 | | - const output = await helper.run( |
126 | | - `wrangler types --x-include-runtime="./types.d.ts"` |
127 | | - ); |
128 | 97 |
|
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" |
131 | 109 | ); |
| 110 | + expect(file).contains('declare module "cloudflare:workers"'); |
| 111 | + expect(file).contains("interface Env"); |
132 | 112 | }); |
133 | 113 |
|
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 | 114 | it("should include header with version information in the generated types", async () => { |
154 | 115 | const helper = new WranglerE2ETestHelper(); |
155 | 116 | 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" `); |
157 | 118 |
|
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"); |
161 | 122 |
|
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( |
163 | 127 | /\/\/ Runtime types generated with workerd@1\.\d+\.\d \d\d\d\d-\d\d-\d\d ([a-z_]+,?)*/ |
164 | 128 | ); |
165 | 129 | }); |
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 () => { |
167 | 132 | const helper = new WranglerE2ETestHelper(); |
168 | 133 | 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") |
174 | 157 | ); |
175 | | - const file = (await readFile(runtimeTypesFile)).toString(); |
176 | | - |
177 | | - const header = file.split("\n")[0]; |
178 | 158 |
|
179 | | - await writeFile(runtimeTypesFile, header + "\n" + "SOME_RANDOM_DATA"); |
| 159 | + await helper.run(`wrangler types`); |
180 | 160 |
|
181 | | - await helper.run(`wrangler types --x-include-runtime`); |
| 161 | + const file2 = (await readFile(typesPath)).toString(); |
182 | 162 |
|
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 | + } |
184 | 169 |
|
185 | | - expect(file2.split("\n")[1]).toBe("SOME_RANDOM_DATA"); |
| 170 | + // Begin runtime types |
| 171 | + FAKE RUNTIME" |
| 172 | + `); |
186 | 173 | }); |
187 | 174 | }); |
0 commit comments