@@ -3,16 +3,19 @@ import * as TOML from "@iarna/toml";
3
3
import { mockConfirm } from "./mock-dialogs" ;
4
4
import { runWrangler } from "./run-wrangler" ;
5
5
import { runInTempDir } from "./run-in-tmp" ;
6
+ import { mockConsoleMethods } from "./mock-console" ;
6
7
import * as fs from "node:fs" ;
7
8
8
9
describe ( "wrangler" , ( ) => {
9
10
runInTempDir ( ) ;
10
11
12
+ const std = mockConsoleMethods ( ) ;
13
+
11
14
describe ( "no command" , ( ) => {
12
15
it ( "should display a list of available commands" , async ( ) => {
13
- const { stdout , stderr } = await runWrangler ( ) ;
16
+ await runWrangler ( ) ;
14
17
15
- expect ( stdout ) . toMatchInlineSnapshot ( `
18
+ expect ( std . out ) . toMatchInlineSnapshot ( `
16
19
"wrangler
17
20
18
21
Commands:
@@ -36,16 +39,23 @@ describe("wrangler", () => {
36
39
-l, --local Run on my machine [boolean] [default: false]"
37
40
` ) ;
38
41
39
- expect ( stderr ) . toMatchInlineSnapshot ( `""` ) ;
42
+ expect ( std . err ) . toMatchInlineSnapshot ( `""` ) ;
40
43
} ) ;
41
44
} ) ;
42
45
43
46
describe ( "invalid command" , ( ) => {
44
47
it ( "should display an error" , async ( ) => {
45
- const { error, stdout, stderr } = await runWrangler ( "invalid-command" ) ;
46
-
47
- expect ( stdout ) . toMatchInlineSnapshot ( `""` ) ;
48
- expect ( stderr ) . toMatchInlineSnapshot ( `
48
+ let err : Error | undefined ;
49
+ try {
50
+ await runWrangler ( "invalid-command" ) ;
51
+ } catch ( e ) {
52
+ err = e ;
53
+ } finally {
54
+ expect ( err ?. message ) . toBe ( `Unknown command: invalid-command.` ) ;
55
+ }
56
+
57
+ expect ( std . out ) . toMatchInlineSnapshot ( `""` ) ;
58
+ expect ( std . err ) . toMatchInlineSnapshot ( `
49
59
"wrangler
50
60
51
61
Commands:
@@ -70,9 +80,6 @@ describe("wrangler", () => {
70
80
71
81
Unknown command: invalid-command."
72
82
` ) ;
73
- expect ( error ) . toMatchInlineSnapshot (
74
- `[Error: Unknown command: invalid-command.]`
75
- ) ;
76
83
} ) ;
77
84
} ) ;
78
85
@@ -90,15 +97,19 @@ describe("wrangler", () => {
90
97
} ) ;
91
98
92
99
it ( "should display warning when wrangler.toml already exists, and exit if user does not want to carry on" , async ( ) => {
93
- fs . writeFileSync ( "./wrangler.toml" , "" , "utf-8" ) ;
100
+ fs . writeFileSync (
101
+ "./wrangler.toml" ,
102
+ 'compatibility_date="something-else"' , // use a fake value to make sure the file is not overwritten
103
+ "utf-8"
104
+ ) ;
94
105
mockConfirm ( {
95
106
text : "Do you want to continue initializing this project?" ,
96
107
result : false ,
97
108
} ) ;
98
- const { warnings } = await runWrangler ( "init" ) ;
99
- expect ( warnings ) . toContain ( "wrangler.toml file already exists!" ) ;
109
+ await runWrangler ( "init" ) ;
110
+ expect ( std . warn ) . toContain ( "wrangler.toml file already exists!" ) ;
100
111
const parsed = TOML . parse ( await fsp . readFile ( "./wrangler.toml" , "utf-8" ) ) ;
101
- expect ( typeof parsed . compatibility_date ) . toBe ( "undefined " ) ;
112
+ expect ( parsed . compatibility_date ) . toBe ( "something-else " ) ;
102
113
} ) ;
103
114
104
115
it ( "should display warning when wrangler.toml already exists, but continue if user does want to carry on" , async ( ) => {
@@ -113,10 +124,8 @@ describe("wrangler", () => {
113
124
result : false ,
114
125
}
115
126
) ;
116
- const { warnings } = await runWrangler ( "init" ) ;
117
- expect ( warnings ) . toContain ( "wrangler.toml file already exists!" ) ;
118
- const parsed = TOML . parse ( await fsp . readFile ( "./wrangler.toml" , "utf-8" ) ) ;
119
- expect ( typeof parsed . compatibility_date ) . toBe ( "string" ) ;
127
+ await runWrangler ( "init" ) ;
128
+ expect ( std . warn ) . toContain ( "wrangler.toml file already exists!" ) ;
120
129
} ) ;
121
130
122
131
it ( "should create a package.json if none is found and user confirms" , async ( ) => {
@@ -137,14 +146,23 @@ describe("wrangler", () => {
137
146
) ;
138
147
expect ( packageJson . name ) . toEqual ( "worker" ) ; // TODO: should we infer the name from the directory?
139
148
expect ( packageJson . version ) . toEqual ( "0.0.1" ) ;
149
+ expect ( packageJson . devDependencies ) . toEqual ( {
150
+ wrangler : expect . any ( String ) ,
151
+ } ) ;
140
152
expect ( fs . existsSync ( "./tsconfig.json" ) ) . toBe ( false ) ;
141
153
} ) ;
142
154
143
155
it ( "should not touch an existing package.json in the same directory" , async ( ) => {
144
- mockConfirm ( {
145
- text : "Would you like to use typescript?" ,
146
- result : false ,
147
- } ) ;
156
+ mockConfirm (
157
+ {
158
+ text : "Would you like to install wrangler into your package.json?" ,
159
+ result : false ,
160
+ } ,
161
+ {
162
+ text : "Would you like to use typescript?" ,
163
+ result : false ,
164
+ }
165
+ ) ;
148
166
149
167
fs . writeFileSync (
150
168
"./package.json" ,
@@ -160,11 +178,46 @@ describe("wrangler", () => {
160
178
expect ( packageJson . version ) . toEqual ( "1.0.0" ) ;
161
179
} ) ;
162
180
163
- it ( "should not touch an existing package.json in an ancestor directory" , async ( ) => {
164
- mockConfirm ( {
165
- text : "Would you like to use typescript?" ,
166
- result : false ,
181
+ it ( "should offer to install wrangler into an existing package.json" , async ( ) => {
182
+ mockConfirm (
183
+ {
184
+ text : "Would you like to install wrangler into your package.json?" ,
185
+ result : true ,
186
+ } ,
187
+ {
188
+ text : "Would you like to use typescript?" ,
189
+ result : false ,
190
+ }
191
+ ) ;
192
+
193
+ fs . writeFileSync (
194
+ "./package.json" ,
195
+ JSON . stringify ( { name : "test" , version : "1.0.0" } ) ,
196
+ "utf-8"
197
+ ) ;
198
+
199
+ await runWrangler ( "init" ) ;
200
+ const packageJson = JSON . parse (
201
+ fs . readFileSync ( "./package.json" , "utf-8" )
202
+ ) ;
203
+ expect ( packageJson . name ) . toEqual ( "test" ) ;
204
+ expect ( packageJson . version ) . toEqual ( "1.0.0" ) ;
205
+ expect ( packageJson . devDependencies ) . toEqual ( {
206
+ wrangler : expect . any ( String ) ,
167
207
} ) ;
208
+ } ) ;
209
+
210
+ it ( "should not touch an existing package.json in an ancestor directory" , async ( ) => {
211
+ mockConfirm (
212
+ {
213
+ text : "Would you like to install wrangler into your package.json?" ,
214
+ result : false ,
215
+ } ,
216
+ {
217
+ text : "Would you like to use typescript?" ,
218
+ result : false ,
219
+ }
220
+ ) ;
168
221
169
222
fs . writeFileSync (
170
223
"./package.json" ,
@@ -182,8 +235,12 @@ describe("wrangler", () => {
182
235
const packageJson = JSON . parse (
183
236
fs . readFileSync ( "../../package.json" , "utf-8" )
184
237
) ;
185
- expect ( packageJson . name ) . toEqual ( "test" ) ;
186
- expect ( packageJson . version ) . toEqual ( "1.0.0" ) ;
238
+ expect ( packageJson ) . toMatchInlineSnapshot ( `
239
+ Object {
240
+ "name": "test",
241
+ "version": "1.0.0",
242
+ }
243
+ ` ) ;
187
244
} ) ;
188
245
189
246
it ( "should create a tsconfig.json and install `workers-types` if none is found and user confirms" , async ( ) => {
@@ -210,13 +267,21 @@ describe("wrangler", () => {
210
267
) ;
211
268
expect ( packageJson . devDependencies ) . toEqual ( {
212
269
"@cloudflare/workers-types" : expect . any ( String ) ,
270
+ wrangler : expect . any ( String ) ,
213
271
} ) ;
214
272
} ) ;
215
273
216
274
it ( "should not touch an existing tsconfig.json in the same directory" , async ( ) => {
217
275
fs . writeFileSync (
218
276
"./package.json" ,
219
- JSON . stringify ( { name : "test" , version : "1.0.0" } ) ,
277
+ JSON . stringify ( {
278
+ name : "test" ,
279
+ version : "1.0.0" ,
280
+ devDependencies : {
281
+ wrangler : "0.0.0" ,
282
+ "@cloudflare/workers-types" : "0.0.0" ,
283
+ } ,
284
+ } ) ,
220
285
"utf-8"
221
286
) ;
222
287
fs . writeFileSync (
@@ -232,10 +297,56 @@ describe("wrangler", () => {
232
297
expect ( tsconfigJson . compilerOptions ) . toEqual ( { } ) ;
233
298
} ) ;
234
299
235
- it ( "should not touch an existing package.json in an ancestor directory" , async ( ) => {
300
+ it ( "should offer to install type definitions in an existing typescript project" , async ( ) => {
301
+ mockConfirm (
302
+ {
303
+ text : "Would you like to install wrangler into your package.json?" ,
304
+ result : false ,
305
+ } ,
306
+ {
307
+ text : "Would you like to install the type definitions for Workers into your package.json?" ,
308
+ result : true ,
309
+ }
310
+ ) ;
236
311
fs . writeFileSync (
237
312
"./package.json" ,
238
- JSON . stringify ( { name : "test" , version : "1.0.0" } ) ,
313
+ JSON . stringify ( {
314
+ name : "test" ,
315
+ version : "1.0.0" ,
316
+ } ) ,
317
+ "utf-8"
318
+ ) ;
319
+ fs . writeFileSync (
320
+ "./tsconfig.json" ,
321
+ JSON . stringify ( { compilerOptions : { } } ) ,
322
+ "utf-8"
323
+ ) ;
324
+
325
+ await runWrangler ( "init" ) ;
326
+ const tsconfigJson = JSON . parse (
327
+ fs . readFileSync ( "./tsconfig.json" , "utf-8" )
328
+ ) ;
329
+ // unchanged tsconfig
330
+ expect ( tsconfigJson . compilerOptions ) . toEqual ( { } ) ;
331
+ const packageJson = JSON . parse (
332
+ fs . readFileSync ( "./package.json" , "utf-8" )
333
+ ) ;
334
+ expect ( packageJson . devDependencies ) . toEqual ( {
335
+ "@cloudflare/workers-types" : expect . any ( String ) ,
336
+ } ) ;
337
+ } ) ;
338
+
339
+ it ( "should not touch an existing tsconfig.json in an ancestor directory" , async ( ) => {
340
+ fs . writeFileSync (
341
+ "./package.json" ,
342
+ JSON . stringify ( {
343
+ name : "test" ,
344
+ version : "1.0.0" ,
345
+ devDependencies : {
346
+ wrangler : "0.0.0" ,
347
+ "@cloudflare/workers-types" : "0.0.0" ,
348
+ } ,
349
+ } ) ,
239
350
"utf-8"
240
351
) ;
241
352
fs . writeFileSync (
@@ -258,32 +369,50 @@ describe("wrangler", () => {
258
369
} ) ;
259
370
260
371
it ( "should error if `--type` is used" , async ( ) => {
261
- const { error } = await runWrangler ( "init --type" ) ;
262
- expect ( error ) . toMatchInlineSnapshot (
263
- `[Error: The --type option is no longer supported.]`
264
- ) ;
372
+ let err : undefined | Error ;
373
+ try {
374
+ await runWrangler ( "init --type" ) ;
375
+ } catch ( e ) {
376
+ err = e ;
377
+ } finally {
378
+ expect ( err ?. message ) . toBe ( `The --type option is no longer supported.` ) ;
379
+ }
265
380
} ) ;
266
381
267
382
it ( "should error if `--type javascript` is used" , async ( ) => {
268
- const { error } = await runWrangler ( "init --type javascript" ) ;
269
- expect ( error ) . toMatchInlineSnapshot (
270
- `[Error: The --type option is no longer supported.]`
271
- ) ;
383
+ let err : undefined | Error ;
384
+ try {
385
+ await runWrangler ( "init --type javascript" ) ;
386
+ } catch ( e ) {
387
+ err = e ;
388
+ } finally {
389
+ expect ( err ?. message ) . toBe ( `The --type option is no longer supported.` ) ;
390
+ }
272
391
} ) ;
273
392
274
393
it ( "should error if `--type rust` is used" , async ( ) => {
275
- const { error } = await runWrangler ( "init --type rust" ) ;
276
- expect ( error ) . toMatchInlineSnapshot (
277
- `[Error: The --type option is no longer supported.]`
278
- ) ;
394
+ let err : undefined | Error ;
395
+ try {
396
+ await runWrangler ( "init --type rust" ) ;
397
+ } catch ( e ) {
398
+ err = e ;
399
+ } finally {
400
+ expect ( err ?. message ) . toBe ( `The --type option is no longer supported.` ) ;
401
+ }
279
402
} ) ;
280
403
281
404
it ( "should error if `--type webpack` is used" , async ( ) => {
282
- const { error } = await runWrangler ( "init --type webpack" ) ;
283
- expect ( error ) . toMatchInlineSnapshot ( `
284
- [Error: The --type option is no longer supported.
285
- If you wish to use webpack then you will need to create a custom build.]
286
- ` ) ;
405
+ let err : undefined | Error ;
406
+ try {
407
+ await runWrangler ( "init --type webpack" ) ;
408
+ } catch ( e ) {
409
+ err = e ;
410
+ } finally {
411
+ expect ( err ?. message ) . toBe (
412
+ `The --type option is no longer supported.
413
+ If you wish to use webpack then you will need to create a custom build.`
414
+ ) ;
415
+ }
287
416
} ) ;
288
417
} ) ;
289
418
} ) ;
0 commit comments