1
1
import { toDashCase } from '@utils' ;
2
2
3
3
import { LogLevel } from '../../declarations' ;
4
- import { BOOLEAN_CLI_ARGS , NUMBER_CLI_ARGS , STRING_CLI_ARGS } from '../config-flags' ;
5
- import { parseEqualsArg , parseFlags } from '../parse-flags' ;
4
+ import {
5
+ BOOLEAN_CLI_FLAGS ,
6
+ NUMBER_CLI_FLAGS ,
7
+ STRING_ARRAY_CLI_FLAGS ,
8
+ STRING_CLI_FLAGS ,
9
+ StringArrayCLIFlag ,
10
+ } from '../config-flags' ;
11
+ import { Empty , parseEqualsArg , parseFlags } from '../parse-flags' ;
6
12
7
13
describe ( 'parseFlags' , ( ) => {
8
14
it ( 'should get known and unknown args' , ( ) => {
@@ -54,7 +60,7 @@ describe('parseFlags', () => {
54
60
* will result in a value like `[true, true]` being set on ConfigFlags, which
55
61
* will cause these tests to start failing.
56
62
*/
57
- describe . each ( BOOLEAN_CLI_ARGS ) ( 'should parse boolean flag %s' , ( cliArg ) => {
63
+ describe . each ( BOOLEAN_CLI_FLAGS ) ( 'should parse boolean flag %s' , ( cliArg ) => {
58
64
it ( 'should parse arg' , ( ) => {
59
65
const flags = parseFlags ( [ `--${ cliArg } ` ] ) ;
60
66
expect ( flags . knownArgs ) . toEqual ( [ `--${ cliArg } ` ] ) ;
@@ -75,14 +81,14 @@ describe('parseFlags', () => {
75
81
expect ( flags [ cliArg ] ) . toBe ( false ) ;
76
82
} ) ;
77
83
78
- it ( 'should set value to null if not present' , ( ) => {
84
+ it ( 'should not set value if not present' , ( ) => {
79
85
const flags = parseFlags ( [ ] ) ;
80
86
expect ( flags . knownArgs ) . toEqual ( [ ] ) ;
81
- expect ( flags [ cliArg ] ) . toBe ( null ) ;
87
+ expect ( flags [ cliArg ] ) . toBe ( undefined ) ;
82
88
} ) ;
83
89
} ) ;
84
90
85
- describe . each ( STRING_CLI_ARGS ) ( 'should parse string flag %s' , ( cliArg ) => {
91
+ describe . each ( STRING_CLI_FLAGS ) ( 'should parse string flag %s' , ( cliArg ) => {
86
92
it ( `should parse "--${ cliArg } value"` , ( ) => {
87
93
const flags = parseFlags ( [ `--${ cliArg } ` , 'test-value' ] ) ;
88
94
expect ( flags . knownArgs ) . toEqual ( [ `--${ cliArg } ` , 'test-value' ] ) ;
@@ -112,7 +118,7 @@ describe('parseFlags', () => {
112
118
} ) ;
113
119
} ) ;
114
120
115
- it . each ( NUMBER_CLI_ARGS ) ( 'should parse number flag %s' , ( cliArg ) => {
121
+ it . each ( NUMBER_CLI_FLAGS ) ( 'should parse number flag %s' , ( cliArg ) => {
116
122
const flags = parseFlags ( [ `--${ cliArg } ` , '42' ] ) ;
117
123
expect ( flags . knownArgs ) . toEqual ( [ `--${ cliArg } ` , '42' ] ) ;
118
124
expect ( flags . unknownArgs ) . toEqual ( [ ] ) ;
@@ -194,7 +200,7 @@ describe('parseFlags', () => {
194
200
195
201
it ( 'should not parse --max-workers' , ( ) => {
196
202
const flags = parseFlags ( [ ] ) ;
197
- expect ( flags . maxWorkers ) . toBe ( null ) ;
203
+ expect ( flags . maxWorkers ) . toBe ( undefined ) ;
198
204
} ) ;
199
205
} ) ;
200
206
@@ -246,12 +252,136 @@ describe('parseFlags', () => {
246
252
[ '--fooBar=baz' , '--fooBar' , 'baz' ] ,
247
253
[ '--foo-bar=4' , '--foo-bar' , '4' ] ,
248
254
[ '--fooBar=twenty=3*4' , '--fooBar' , 'twenty=3*4' ] ,
249
- [ '--fooBar' , '--fooBar' , '' ] ,
250
- [ '--foo-bar' , '--foo-bar' , '' ] ,
255
+ [ '--fooBar' , '--fooBar' , Empty ] ,
256
+ [ '--foo-bar' , '--foo-bar' , Empty ] ,
257
+ [ '--foo-bar=""' , '--foo-bar' , '""' ] ,
251
258
] ) ( 'should parse %s correctly' , ( testArg , expectedArg , expectedValue ) => {
252
259
const [ arg , value ] = parseEqualsArg ( testArg ) ;
253
260
expect ( arg ) . toBe ( expectedArg ) ;
254
- expect ( value ) . toBe ( expectedValue ) ;
261
+ expect ( value ) . toEqual ( expectedValue ) ;
262
+ } ) ;
263
+ } ) ;
264
+
265
+ describe . each ( STRING_ARRAY_CLI_FLAGS ) ( 'should parse string flag %s' , ( cliArg : StringArrayCLIFlag ) => {
266
+ it ( `should parse single value: "--${ cliArg } test-value"` , ( ) => {
267
+ const flags = parseFlags ( [ `--${ cliArg } ` , 'test-value' ] ) ;
268
+ expect ( flags . knownArgs ) . toEqual ( [ `--${ cliArg } ` , 'test-value' ] ) ;
269
+ expect ( flags . unknownArgs ) . toEqual ( [ ] ) ;
270
+ expect ( flags [ cliArg ] ) . toEqual ( [ 'test-value' ] ) ;
271
+ } ) ;
272
+
273
+ it ( `should parse multiple values: "--${ cliArg } test-value"` , ( ) => {
274
+ const flags = parseFlags ( [ `--${ cliArg } ` , 'test-value' , `--${ cliArg } ` , 'second-test-value' ] ) ;
275
+ expect ( flags . knownArgs ) . toEqual ( [ `--${ cliArg } ` , 'test-value' , `--${ cliArg } ` , 'second-test-value' ] ) ;
276
+ expect ( flags . unknownArgs ) . toEqual ( [ ] ) ;
277
+ expect ( flags [ cliArg ] ) . toEqual ( [ 'test-value' , 'second-test-value' ] ) ;
278
+ } ) ;
279
+
280
+ it ( `should parse "--${ cliArg } =value"` , ( ) => {
281
+ const flags = parseFlags ( [ `--${ cliArg } =path/to/file.js` ] ) ;
282
+ expect ( flags . knownArgs ) . toEqual ( [ `--${ cliArg } ` , 'path/to/file.js' ] ) ;
283
+ expect ( flags . unknownArgs ) . toEqual ( [ ] ) ;
284
+ expect ( flags [ cliArg ] ) . toEqual ( [ 'path/to/file.js' ] ) ;
285
+ } ) ;
286
+
287
+ it ( `should parse multiple values: "--${ cliArg } =test-value"` , ( ) => {
288
+ const flags = parseFlags ( [ `--${ cliArg } =test-value` , `--${ cliArg } =second-test-value` ] ) ;
289
+ expect ( flags . knownArgs ) . toEqual ( [ `--${ cliArg } ` , 'test-value' , `--${ cliArg } ` , 'second-test-value' ] ) ;
290
+ expect ( flags . unknownArgs ) . toEqual ( [ ] ) ;
291
+ expect ( flags [ cliArg ] ) . toEqual ( [ 'test-value' , 'second-test-value' ] ) ;
292
+ } ) ;
293
+
294
+ it ( `should parse "--${ toDashCase ( cliArg ) } value"` , ( ) => {
295
+ const flags = parseFlags ( [ `--${ toDashCase ( cliArg ) } ` , 'path/to/file.js' ] ) ;
296
+ expect ( flags . knownArgs ) . toEqual ( [ `--${ toDashCase ( cliArg ) } ` , 'path/to/file.js' ] ) ;
297
+ expect ( flags . unknownArgs ) . toEqual ( [ ] ) ;
298
+ expect ( flags [ cliArg ] ) . toEqual ( [ 'path/to/file.js' ] ) ;
299
+ } ) ;
300
+
301
+ it ( `should parse multiple values: "--${ toDashCase ( cliArg ) } test-value"` , ( ) => {
302
+ const flags = parseFlags ( [
303
+ `--${ toDashCase ( cliArg ) } ` ,
304
+ 'test-value' ,
305
+ `--${ toDashCase ( cliArg ) } ` ,
306
+ 'second-test-value' ,
307
+ ] ) ;
308
+ expect ( flags . knownArgs ) . toEqual ( [
309
+ `--${ toDashCase ( cliArg ) } ` ,
310
+ 'test-value' ,
311
+ `--${ toDashCase ( cliArg ) } ` ,
312
+ 'second-test-value' ,
313
+ ] ) ;
314
+ expect ( flags . unknownArgs ) . toEqual ( [ ] ) ;
315
+ expect ( flags [ cliArg ] ) . toEqual ( [ 'test-value' , 'second-test-value' ] ) ;
316
+ } ) ;
317
+
318
+ it ( `should parse "--${ toDashCase ( cliArg ) } =value"` , ( ) => {
319
+ const flags = parseFlags ( [ `--${ toDashCase ( cliArg ) } =path/to/file.js` ] ) ;
320
+ expect ( flags . knownArgs ) . toEqual ( [ `--${ toDashCase ( cliArg ) } ` , 'path/to/file.js' ] ) ;
321
+ expect ( flags . unknownArgs ) . toEqual ( [ ] ) ;
322
+ expect ( flags [ cliArg ] ) . toEqual ( [ 'path/to/file.js' ] ) ;
323
+ } ) ;
324
+
325
+ it ( `should parse multiple values: "--${ toDashCase ( cliArg ) } =test-value"` , ( ) => {
326
+ const flags = parseFlags ( [ `--${ toDashCase ( cliArg ) } =test-value` , `--${ toDashCase ( cliArg ) } =second-test-value` ] ) ;
327
+ expect ( flags . knownArgs ) . toEqual ( [
328
+ `--${ toDashCase ( cliArg ) } ` ,
329
+ 'test-value' ,
330
+ `--${ toDashCase ( cliArg ) } ` ,
331
+ 'second-test-value' ,
332
+ ] ) ;
333
+ expect ( flags . unknownArgs ) . toEqual ( [ ] ) ;
334
+ expect ( flags [ cliArg ] ) . toEqual ( [ 'test-value' , 'second-test-value' ] ) ;
335
+ } ) ;
336
+ } ) ;
337
+
338
+ describe ( 'error reporting' , ( ) => {
339
+ it ( 'should throw if you pass no argument to a string flag' , ( ) => {
340
+ expect ( ( ) => {
341
+ parseFlags ( [ '--cacheDirectory' , '--someOtherFlag' ] ) ;
342
+ } ) . toThrow ( 'when parsing CLI flag "--cacheDirectory": expected a string argument but received nothing' ) ;
343
+ } ) ;
344
+
345
+ it ( 'should throw if you pass no argument to a string array flag' , ( ) => {
346
+ expect ( ( ) => {
347
+ parseFlags ( [ '--reporters' , '--someOtherFlag' ] ) ;
348
+ } ) . toThrow ( 'when parsing CLI flag "--reporters": expected a string argument but received nothing' ) ;
349
+ } ) ;
350
+
351
+ it ( 'should throw if you pass no argument to a number flag' , ( ) => {
352
+ expect ( ( ) => {
353
+ parseFlags ( [ '--port' , '--someOtherFlag' ] ) ;
354
+ } ) . toThrow ( 'when parsing CLI flag "--port": expected a number argument but received nothing' ) ;
355
+ } ) ;
356
+
357
+ it ( 'should throw if you pass a non-number argument to a number flag' , ( ) => {
358
+ expect ( ( ) => {
359
+ parseFlags ( [ '--port' , 'stringy' ] ) ;
360
+ } ) . toThrow ( 'when parsing CLI flag "--port": expected a number but received "stringy"' ) ;
361
+ } ) ;
362
+
363
+ it ( 'should throw if you pass a bad number argument to a number flag' , ( ) => {
364
+ expect ( ( ) => {
365
+ parseFlags ( [ '--port=NaN' ] ) ;
366
+ } ) . toThrow ( 'when parsing CLI flag "--port": expected a number but received "NaN"' ) ;
367
+ } ) ;
368
+
369
+ it ( 'should throw if you pass no argument to a string/number flag' , ( ) => {
370
+ expect ( ( ) => {
371
+ parseFlags ( [ '--maxWorkers' ] ) ;
372
+ } ) . toThrow ( 'when parsing CLI flag "--maxWorkers": expected a string or a number but received nothing' ) ;
373
+ } ) ;
374
+
375
+ it ( 'should throw if you pass an invalid log level for --logLevel' , ( ) => {
376
+ expect ( ( ) => {
377
+ parseFlags ( [ '--logLevel' , 'potato' ] ) ;
378
+ } ) . toThrow ( 'when parsing CLI flag "--logLevel": expected to receive a valid log level but received "potato"' ) ;
379
+ } ) ;
380
+
381
+ it ( 'should throw if you pass no argument to --logLevel' , ( ) => {
382
+ expect ( ( ) => {
383
+ parseFlags ( [ '--logLevel' ] ) ;
384
+ } ) . toThrow ( 'when parsing CLI flag "--logLevel": expected to receive a valid log level but received nothing' ) ;
255
385
} ) ;
256
386
} ) ;
257
387
} ) ;
0 commit comments