-
Notifications
You must be signed in to change notification settings - Fork 784
/
cli-main.js
877 lines (754 loc) 路 26.8 KB
/
cli-main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
"use strict";
const expectedOutput = require( "./fixtures/expected/tap-outputs" );
const execute = require( "./helpers/execute" );
const semver = require( "semver" );
QUnit.module( "CLI Main", () => {
QUnit.test( "defaults to running tests in 'test' directory", async assert => {
const command = "qunit";
const execution = await execute( command );
assert.equal( execution.code, 0 );
assert.equal( execution.stderr, "" );
assert.equal( execution.stdout, expectedOutput[ command ] );
} );
QUnit.test( "errors if no test files are found to run", async assert => {
try {
await execute( "qunit does-not-exist.js" );
} catch ( e ) {
assert.true( e.stderr.includes( "No files were found matching" ) );
}
} );
QUnit.test( "accepts globs for test files to run", async assert => {
const command = "qunit 'glob/**/*-test.js'";
const execution = await execute( command );
assert.equal( execution.code, 0 );
assert.equal( execution.stderr, "" );
assert.equal( execution.stdout, expectedOutput[ command ] );
} );
QUnit.test( "runs a single JS file", async assert => {
const command = "qunit single.js";
const execution = await execute( command );
assert.equal( execution.code, 0 );
assert.equal( execution.stderr, "" );
assert.equal( execution.stdout, expectedOutput[ command ] );
} );
QUnit.test( "runs multiple JS files", async assert => {
const command = "qunit single.js double.js";
const execution = await execute( command );
assert.equal( execution.code, 0 );
assert.equal( execution.stderr, "" );
assert.equal( execution.stdout, expectedOutput[ command ] );
} );
QUnit.test( "runs all JS files in a directory matching an arg", async assert => {
const command = "qunit test";
const execution = await execute( command );
assert.equal( execution.code, 0 );
assert.equal( execution.stderr, "" );
assert.equal( execution.stdout, expectedOutput[ command ] );
} );
QUnit.test( "runs multiple types of file paths", async assert => {
const command = "qunit test single.js 'glob/**/*-test.js'";
const execution = await execute( command );
assert.equal( execution.code, 0 );
assert.equal( execution.stderr, "" );
assert.equal( execution.stdout, expectedOutput[ command ] );
} );
QUnit.test( "logs test files that fail to load properly", async assert => {
try {
await execute( "qunit syntax-error/test.js" );
} catch ( e ) {
assert.true( e.stdout.includes( "not ok 1 global failure" ) );
assert.true( e.stdout.includes( "Failed to load file syntax-error/test.js" ) );
assert.true( e.stdout.includes( "ReferenceError: varIsNotDefined is not defined" ) );
assert.equal( e.code, 1 );
}
} );
QUnit.test( "report assert.throws() failures properly", async assert => {
const command = "qunit fail/throws-match.js";
try {
await execute( command );
} catch ( e ) {
assert.equal( e.code, 1 );
assert.equal( e.stderr, "" );
assert.equal( e.stdout, expectedOutput[ command ] );
}
} );
QUnit.test( "exit code is 1 when failing tests are present", async assert => {
try {
await execute( "qunit fail/failure.js" );
} catch ( e ) {
assert.equal( e.code, 1 );
}
} );
QUnit.test( "exit code is 1 when no tests are run", async assert => {
const command = "qunit no-tests";
try {
await execute( command );
} catch ( e ) {
assert.equal( e.code, 1 );
assert.equal( e.stderr, "" );
assert.equal( e.stdout, expectedOutput[ command ] );
}
} );
QUnit.test( "exit code is 0 when no tests are run and failOnZeroTests is `false`", async assert => {
const command = "qunit assert-expect/no-tests.js";
const execution = await execute( command );
assert.equal( execution.code, 0 );
assert.equal( execution.stderr, "" );
assert.equal( execution.stdout, expectedOutput[ command ] );
} );
QUnit.test( "exit code is 1 when no tests exit before done", async assert => {
const command = "qunit hanging-test";
try {
await execute( command );
} catch ( e ) {
assert.equal( e.code, 1 );
assert.equal( e.stderr, expectedOutput[ command ] );
}
} );
QUnit.test( "unhandled rejections fail tests", async assert => {
const command = "qunit unhandled-rejection.js";
try {
const result = await execute( command );
assert.pushResult( {
result: false,
actual: result.stdout
} );
} catch ( e ) {
assert.equal( e.code, 1 );
assert.equal( e.stderr, "" );
assert.equal( e.stdout, expectedOutput[ command ] );
}
} );
QUnit.test( "hard errors in test using `assert.async` are caught and reported", async assert => {
const command = "qunit hard-error-in-test-with-no-async-handler.js";
try {
const result = await execute( command );
assert.pushResult( {
result: false,
actual: result.stdout
} );
} catch ( e ) {
assert.equal( e.code, 1 );
assert.equal( e.stderr, "" );
assert.true( e.stdout.includes( "Died on test #2 at " ) );
assert.true( e.stdout.includes( "Error: expected error thrown in test" ) );
}
} );
QUnit.test( "hard errors in hook are caught and reported", async assert => {
const command = "qunit hard-error-in-hook.js";
try {
const result = await execute( command );
assert.pushResult( {
result: false,
actual: result.stdout
} );
} catch ( e ) {
assert.equal( e.code, 1 );
assert.equal( e.stderr, "" );
assert.true( e.stdout.includes( "message: before failed on contains a hard error: expected error thrown in hook" ) );
assert.true( e.stdout.includes( "Error: expected error thrown in hook" ) );
}
} );
// Regression test against "details of begin error swallowed"
// https://github.com/qunitjs/qunit/issues/1446
QUnit.test( "report failure in begin callback", async assert => {
const command = "qunit bad-callbacks/begin-throw.js";
try {
const result = await execute( command );
assert.pushResult( {
result: false,
actual: result.stdout
} );
} catch ( e ) {
assert.equal( e.code, 1 );
assert.equal( e.stdout, expectedOutput[ command ] );
}
} );
QUnit.test( "report failure in done callback", async assert => {
const command = "qunit bad-callbacks/done-throw.js";
try {
const result = await execute( command );
assert.pushResult( {
result: false,
actual: result.stdout
} );
} catch ( e ) {
assert.equal( e.code, 1 );
assert.equal( e.stdout, expectedOutput[ command ] );
}
} );
QUnit.test( "report failure in moduleDone callback", async assert => {
const command = "qunit bad-callbacks/moduleDone-throw.js";
try {
const result = await execute( command );
assert.pushResult( {
result: false,
actual: result.stdout
} );
} catch ( e ) {
assert.equal( e.code, 1 );
// FIXME: The details of this error are swallowed
assert.equal( e.stdout, `TAP version 13
ok 1 module1 > test1` );
assert.equal( e.stderr, "Error: Process exited before tests finished running" );
}
} );
QUnit.test( "report failure in testStart callback", async assert => {
const command = "qunit bad-callbacks/testStart-throw.js";
try {
const result = await execute( command );
assert.pushResult( {
result: false,
actual: result.stdout
} );
} catch ( e ) {
assert.equal( e.code, 1 );
// FIXME: The details of this error are swallowed
assert.equal( e.stdout, "TAP version 13" );
assert.equal( e.stderr, "Error: Process exited before tests finished running" );
}
} );
QUnit.test( "callbacks", async assert => {
const expected = `CALLBACK: begin1
CALLBACK: begin2
CALLBACK: moduleStart1
CALLBACK: moduleStart2
CALLBACK: testStart1
CALLBACK: testStart2
CALLBACK: module1 > before
CALLBACK: module1 > beforeEach
TEST: module1 > test1
CALLBACK: log1
CALLBACK: log2
CALLBACK: module1 > afterEach
CALLBACK: testDone1
CALLBACK: testDone2
CALLBACK: moduleStart1
CALLBACK: moduleStart2
CALLBACK: testStart1
CALLBACK: testStart2
CALLBACK: module2 > before
CALLBACK: module1 > beforeEach
CALLBACK: module2 > beforeEach
TEST: module2 > test1
CALLBACK: log1
CALLBACK: log2
CALLBACK: module2 > afterEach
CALLBACK: module1 > afterEach
CALLBACK: module2 > after
CALLBACK: testDone1
CALLBACK: testDone2
CALLBACK: moduleDone1
CALLBACK: moduleDone2
CALLBACK: moduleStart1
CALLBACK: moduleStart2
CALLBACK: testStart1
CALLBACK: testStart2
CALLBACK: module3 > before
CALLBACK: module1 > beforeEach
CALLBACK: module3 > beforeEach
TEST: module3 > test1
CALLBACK: log1
CALLBACK: log2
CALLBACK: module3 > afterEach
CALLBACK: module1 > afterEach
CALLBACK: module3 > after
CALLBACK: testDone1
CALLBACK: testDone2
CALLBACK: moduleDone1
CALLBACK: moduleDone2
CALLBACK: testStart1
CALLBACK: testStart2
CALLBACK: module1 > beforeEach
TEST: module1 > test2
CALLBACK: log1
CALLBACK: log2
CALLBACK: module1 > afterEach
CALLBACK: testDone1
CALLBACK: testDone2
CALLBACK: moduleStart1
CALLBACK: moduleStart2
CALLBACK: testStart1
CALLBACK: testStart2
CALLBACK: module4 > before
CALLBACK: module1 > beforeEach
CALLBACK: module4 > beforeEach
TEST: module4 > test1
CALLBACK: log1
CALLBACK: log2
CALLBACK: module4 > afterEach
CALLBACK: module1 > afterEach
CALLBACK: module4 > after
CALLBACK: module1 > after
CALLBACK: testDone1
CALLBACK: testDone2
CALLBACK: moduleDone1
CALLBACK: moduleDone2
CALLBACK: moduleDone1
CALLBACK: moduleDone2
CALLBACK: done1
CALLBACK: done2`;
const command = "qunit callbacks.js";
const execution = await execute( command );
assert.equal( execution.stderr, expected );
assert.equal( execution.code, 0 );
} );
QUnit.test( "callbacks with promises", async assert => {
const expected = `CALLBACK: begin
CALLBACK: begin2
CALLBACK: moduleStart
CALLBACK: moduleStart
CALLBACK: testStart - test1
CALLBACK: testDone - test1
CALLBACK: moduleDone - module1 > nestedModule1
CALLBACK: testStart - test2
CALLBACK: testDone - test2
CALLBACK: moduleStart
CALLBACK: testStart - test3
CALLBACK: testDone - test3
CALLBACK: moduleDone - module1 > nestedModule2
CALLBACK: moduleDone - module1
CALLBACK: done`;
const command = "qunit callbacks-promises.js";
const execution = await execute( command );
assert.equal( execution.stderr, expected );
assert.equal( execution.code, 0 );
} );
QUnit.test( "global hooks order", async assert => {
const expected = `
HOOK: A1 @ global beforeEach-1
HOOK: A1 @ global beforeEach-2
HOOK: A1 @ global afterEach-2
HOOK: A1 @ global afterEach-1
HOOK: B1 @ B before
HOOK: B1 @ global beforeEach-1
HOOK: B1 @ global beforeEach-2
HOOK: B1 @ B beforeEach
HOOK: B1 @ B afterEach
HOOK: B1 @ global afterEach-2
HOOK: B1 @ global afterEach-1
HOOK: B2 @ global beforeEach-1
HOOK: B2 @ global beforeEach-2
HOOK: B2 @ B beforeEach
HOOK: B2 @ B afterEach
HOOK: B2 @ global afterEach-2
HOOK: B2 @ global afterEach-1
HOOK: BC1 @ BC before
HOOK: BC1 @ global beforeEach-1
HOOK: BC1 @ global beforeEach-2
HOOK: BC1 @ B beforeEach
HOOK: BC1 @ BC beforeEach
HOOK: BC1 @ BC afterEach
HOOK: BC1 @ B afterEach
HOOK: BC1 @ global afterEach-2
HOOK: BC1 @ global afterEach-1
HOOK: BC2 @ global beforeEach-1
HOOK: BC2 @ global beforeEach-2
HOOK: BC2 @ B beforeEach
HOOK: BC2 @ BC beforeEach
HOOK: BC2 @ BC afterEach
HOOK: BC2 @ B afterEach
HOOK: BC2 @ global afterEach-2
HOOK: BC2 @ global afterEach-1
HOOK: BCD1 @ BCD before
HOOK: BCD1 @ global beforeEach-1
HOOK: BCD1 @ global beforeEach-2
HOOK: BCD1 @ B beforeEach
HOOK: BCD1 @ BC beforeEach
HOOK: BCD1 @ BCD beforeEach
HOOK: BCD1 @ BCD afterEach
HOOK: BCD1 @ BC afterEach
HOOK: BCD1 @ B afterEach
HOOK: BCD1 @ global afterEach-2
HOOK: BCD1 @ global afterEach-1
HOOK: BCD1 @ BCD after
HOOK: BCD1 @ BC after
HOOK: BCD1 @ B after`;
const command = "qunit hooks-global-order.js";
const execution = await execute( command );
assert.equal( execution.stderr, expected.trim() );
assert.equal( execution.code, 0 );
} );
QUnit.test( "global hooks context", async assert => {
const command = "qunit hooks-global-context.js";
const execution = await execute( command );
assert.equal( execution.code, 0 );
assert.equal( execution.stderr, "" );
assert.equal( execution.stdout, expectedOutput[ command ] );
} );
if ( semver.gte( process.versions.node, "12.0.0" ) ) {
QUnit.test( "run ESM test suite with import statement", async assert => {
const command = "qunit ../../es2018/esm.mjs";
const execution = await execute( command );
assert.equal( execution.code, 0 );
// Node 12 enabled ESM by default, without experimental flag,
// but left the warning in stderr. The warning was removed in Node 14.
// Don't bother checking stderr
const stderr = semver.gte( process.versions.node, "14.0.0" ) ? execution.stderr : "";
assert.equal( stderr, "" );
assert.equal( execution.stdout, expectedOutput[ command ] );
} );
}
// https://nodejs.org/dist/v12.12.0/docs/api/cli.html#cli_enable_source_maps
if ( semver.gte( process.versions.node, "14.0.0" ) ) {
QUnit.test( "normal trace with native source map", async assert => {
const command = "qunit sourcemap/source.js";
try {
await execute( command );
} catch ( e ) {
assert.equal( e.code, 1 );
assert.equal( e.stderr, "" );
assert.equal( e.stdout, expectedOutput[ command ] );
}
} );
// skip if running in code coverage mode - that leads to conflicting maps-on-maps that invalidate this test
QUnit[ process.env.NYC_PROCESS_ID ? "skip" : "test" ](
"mapped trace with native source map", async function( assert ) {
const command = "NODE_OPTIONS='--enable-source-maps' qunit sourcemap/source.min.js";
try {
await execute( command );
} catch ( e ) {
assert.equal( e.code, 1 );
assert.equal( e.stderr, "" );
assert.equal( e.stdout, expectedOutput[ command ] );
}
} );
}
QUnit.test( "timeouts correctly recover", async assert => {
const command = "qunit timeout";
try {
await execute( command );
} catch ( e ) {
assert.equal( e.code, 1 );
assert.equal( e.stderr, "" );
assert.equal( e.stdout, expectedOutput[ command ] );
}
} );
QUnit.test( "allows running zero-assertion tests", async assert => {
const command = "qunit zero-assertions.js";
const execution = await execute( command );
assert.equal( execution.code, 0 );
assert.equal( execution.stderr, "" );
assert.equal( execution.stdout, expectedOutput[ command ] );
} );
// https://nodejs.org/docs/v14.0.0/api/v8.html#v8_v8_getheapsnapshot
// Created in Node 11.x, but starts working the way we need from Node 14.
if ( semver.gte( process.versions.node, "14.0.0" ) ) {
QUnit.test( "callbacks and hooks from modules are properly released for garbage collection", async assert => {
const command = "node --expose-gc ../../../bin/qunit.js memory-leak/*.js";
const execution = await execute( command );
assert.equal( execution.code, 0 );
assert.equal( execution.stderr, "" );
assert.equal( execution.stdout, expectedOutput[ command ] );
} );
QUnit.test( "callbacks and hooks from filtered-out modules are properly released for garbage collection", async assert => {
const command = "node --expose-gc ../../../bin/qunit.js --filter '!child' memory-leak/*.js";
const execution = await execute( command );
assert.equal( execution.code, 0 );
assert.equal( execution.stderr, "" );
assert.equal( execution.stdout, expectedOutput[ command ] );
} );
}
QUnit.module( "filter", () => {
QUnit.test( "can properly filter tests", async assert => {
const command = "qunit --filter 'single' test single.js 'glob/**/*-test.js'";
const equivalentCommand = "qunit single.js";
const execution = await execute( command );
assert.equal( execution.code, 0 );
assert.equal( execution.stderr, "" );
assert.equal( execution.stdout, expectedOutput[ equivalentCommand ] );
} );
QUnit.test( "exit code is 1 when no tests match filter", async assert => {
const command = "qunit qunit --filter 'no matches' test";
try {
await execute( command );
} catch ( e ) {
assert.equal( e.code, 1 );
assert.equal( e.stderr, "" );
assert.equal( e.stdout, expectedOutput[ command ] );
}
} );
} );
QUnit.module( "require", () => {
QUnit.test( "can properly require dependencies and modules", async assert => {
const command = "qunit single.js --require require-dep --require './node_modules/require-dep/module.js'";
const execution = await execute( command );
assert.equal( execution.code, 0 );
assert.equal( execution.stderr, "" );
assert.equal( execution.stdout, expectedOutput[ command ] );
} );
QUnit.test( "displays helpful error when failing to require a file", async assert => {
const command = "qunit single.js --require 'does-not-exist-at-all'";
try {
await execute( command );
} catch ( e ) {
assert.equal( e.code, 1 );
assert.true( e.stderr.includes( "Error: Cannot find module 'does-not-exist-at-all'" ) );
assert.equal( e.stdout, "" );
}
} );
} );
QUnit.module( "seed", () => {
QUnit.test( "can properly seed tests", async assert => {
const command = "qunit --seed 's33d' test single.js 'glob/**/*-test.js'";
const execution = await execute( command );
assert.equal( execution.code, 0 );
assert.equal( execution.stderr, "" );
assert.equal( execution.stdout, expectedOutput[ command ] );
} );
} );
QUnit.module( "notrycatch", () => {
QUnit.test( "errors if notrycatch is used and a rejection occurs", async assert => {
try {
await execute( "qunit notrycatch/returns-rejection.js" );
} catch ( e ) {
assert.pushResult( {
// only in stdout due to using `console.log` in manual `unhandledRejection` handler
result: e.stdout.includes( "Unhandled Rejection: bad things happen sometimes" ),
actual: e.stdout + "\n" + e.stderr
} );
}
} );
QUnit.test( "errors if notrycatch is used and a rejection occurs in a hook", async assert => {
try {
await execute( "qunit notrycatch/returns-rejection-in-hook.js" );
} catch ( e ) {
assert.pushResult( {
// only in stdout due to using `console.log` in manual `unhandledRejection` handler
result: e.stdout.includes( "Unhandled Rejection: bad things happen sometimes" ),
actual: e.stdout + "\n" + e.stderr
} );
}
} );
} );
QUnit.test( "config.filter (string)", async assert => {
const command = "qunit config-filter-string.js";
const execution = await execute( command );
assert.equal( execution.code, 0 );
assert.equal( execution.stderr, "" );
assert.equal( execution.stdout, expectedOutput[ command ] );
} );
QUnit.test( "config.filter (regex)", async assert => {
const command = "qunit config-filter-regex.js";
const execution = await execute( command );
assert.equal( execution.code, 0 );
assert.equal( execution.stderr, "" );
assert.equal( execution.stdout, expectedOutput[ command ] );
} );
QUnit.test( "config.filter (regex exclude)", async assert => {
const command = "qunit config-filter-regex-exclude.js";
const execution = await execute( command );
assert.equal( execution.code, 0 );
assert.equal( execution.stderr, "" );
assert.equal( execution.stdout, expectedOutput[ command ] );
} );
QUnit.test( "config.module", async assert => {
const command = "qunit config-module.js";
const execution = await execute( command );
assert.equal( execution.code, 0 );
assert.equal( execution.stderr, "" );
assert.equal( execution.stdout, expectedOutput[ command ] );
} );
QUnit.test( "config.moduleId", async assert => {
const command = "qunit config-moduleId.js";
const execution = await execute( command );
assert.equal( execution.code, 0 );
assert.equal( execution.stderr, "" );
assert.equal( execution.stdout, expectedOutput[ command ] );
} );
QUnit.test( "config.testId", async assert => {
const command = "qunit config-testId.js";
const execution = await execute( command );
assert.equal( execution.code, 0 );
assert.equal( execution.stderr, "" );
assert.equal( execution.stdout, expectedOutput[ command ] );
} );
QUnit.test( "config.testTimeout", async assert => {
const command = "qunit config-testTimeout.js";
try {
await execute( command );
} catch ( e ) {
assert.equal( e.code, 1 );
assert.equal( e.stderr, "" );
assert.equal( e.stdout, expectedOutput[ command ] );
}
} );
QUnit.module( "noglobals", () => {
QUnit.test( "add global variable", async assert => {
try {
await execute( "qunit noglobals/add-global.js" );
} catch ( e ) {
assert.pushResult( {
result: e.stdout.includes( "message: Introduced global variable(s): dummyGlobal" ),
actual: e.stdout + "\n" + e.stderr
} );
}
} );
QUnit.test( "remove global variable", async assert => {
try {
await execute( "qunit noglobals/remove-global.js" );
} catch ( e ) {
assert.pushResult( {
result: e.stdout.includes( "message: Deleted global variable(s): dummyGlobal" ),
actual: e.stdout + "\n" + e.stderr
} );
}
} );
QUnit.test( "forgive qunit DOM global variables", async assert => {
const execution = await execute( "qunit noglobals/ignored.js" );
assert.equal( execution.code, 0 );
assert.equal( execution.stderr, "" );
} );
} );
QUnit.module( "assert.async", () => {
QUnit.test( "call after tests timeout", async assert => {
const command = "qunit done-after-timeout.js";
try {
await execute( command );
} catch ( e ) {
assert.equal( e.code, 1 );
assert.equal( e.stdout, expectedOutput[ command ] );
}
} );
QUnit.test( "drooling call to callback across tests", async assert => {
const command = "qunit drooling-done.js";
try {
await execute( command );
} catch ( e ) {
assert.equal( e.code, 1 );
assert.equal( e.stdout, expectedOutput[ command ] );
}
} );
QUnit.test( "extra call to callback across tests", async assert => {
const command = "qunit drooling-extra-done.js";
try {
await execute( command );
} catch ( e ) {
assert.equal( e.code, 1 );
assert.equal( e.stdout, expectedOutput[ command ] );
}
} );
QUnit.test( "extra call to callback outside tests", async assert => {
const command = "qunit drooling-extra-done-outside.js";
try {
await execute( command );
} catch ( e ) {
assert.equal( e.code, 1 );
assert.equal( e.stdout, expectedOutput[ command ] );
}
} );
QUnit.test( "too many calls to callback", async assert => {
const command = "qunit too-many-done-calls.js";
try {
await execute( command );
} catch ( e ) {
assert.equal( e.code, 1 );
assert.equal( e.stdout, expectedOutput[ command ] );
}
} );
} );
QUnit.module( "only", () => {
QUnit.test( "test", async assert => {
const command = "qunit only/test.js";
const execution = await execute( command );
assert.equal( execution.code, 0 );
assert.equal( execution.stderr, "" );
assert.equal( execution.stdout, expectedOutput[ command ] );
} );
QUnit.test( "nested modules", async assert => {
const command = "qunit only/module.js";
const execution = await execute( command );
assert.equal( execution.code, 0 );
assert.equal( execution.stderr, "" );
assert.equal( execution.stdout, expectedOutput[ command ] );
} );
QUnit.test( "module followed by test", async assert => {
const command = "qunit only/module-then-test.js";
const execution = await execute( command );
assert.equal( execution.code, 0 );
assert.equal( execution.stderr, "" );
assert.equal( execution.stdout, expectedOutput[ command ] );
} );
QUnit.test( "flat modules", async assert => {
const command = "qunit only/module-flat.js";
const execution = await execute( command );
assert.equal( execution.code, 0 );
assert.equal( execution.stderr, "" );
assert.equal( execution.stdout, expectedOutput[ command ] );
} );
} );
// Regression test for https://github.com/qunitjs/qunit/issues/1478
QUnit.test( "nested module scopes", async assert => {
const command = "qunit module-nested.js";
const execution = await execute( command );
assert.equal( execution.code, 0 );
assert.equal( execution.stdout, expectedOutput[ command ] );
} );
QUnit.test( "warns about incorrect hook usage", async assert => {
const command = "qunit incorrect-hooks-warning/test.js";
const execution = await execute( command );
assert.equal( execution.code, 0 );
assert.equal( execution.stderr, "The `beforeEach` hook was called inside the wrong module (`module providing hooks > module not providing hooks`). Instead, use hooks provided by the callback to the containing module (`module providing hooks`). This will become an error in QUnit 3.0.", "The warning shows" );
assert.equal( execution.stdout, expectedOutput[ command ] );
} );
QUnit.test( "warns about unsupported async module callback", async assert => {
const command = "qunit async-module-warning/test.js";
const execution = await execute( command );
assert.equal( execution.code, 0 );
assert.equal( execution.stderr, "Returning a promise from a module callback is not supported. Instead, use hooks for async behavior. This will become an error in QUnit 3.0.", "The warning shows" );
assert.equal( execution.stdout, expectedOutput[ command ] );
} );
QUnit.test( "warns about unsupported promise return value from module", async assert => {
const command = "qunit async-module-warning/promise-test.js";
const execution = await execute( command );
assert.equal( execution.code, 0 );
assert.equal( execution.stderr, "Returning a promise from a module callback is not supported. Instead, use hooks for async behavior. This will become an error in QUnit 3.0.", "The warning shows" );
assert.equal( execution.stdout, expectedOutput[ command ] );
} );
QUnit.module( "assert.expect failing conditions", () => {
QUnit.test( "mismatched expected assertions", async assert => {
const command = "qunit assert-expect/failing-expect.js";
try {
const result = await execute( command );
assert.pushResult( {
result: false,
actual: result.stdout
} );
} catch ( e ) {
assert.equal( e.code, 1 );
assert.equal( e.stderr, "" );
// can't match exactly due to stack frames including internal line numbers
assert.true( e.stdout.includes( "message: Expected 2 assertions, but 1 were run" ), e.stdout );
}
} );
QUnit.test( "no assertions run - use expect(0)", async assert => {
const command = "qunit assert-expect/no-assertions.js";
try {
const result = await execute( command );
assert.pushResult( {
result: true,
actual: result.stdout
} );
} catch ( e ) {
assert.equal( e.code, 1 );
assert.equal( e.stderr, "" );
// can't match exactly due to stack frames including internal line numbers
assert.true( e.stdout.includes( "Expected at least one assertion, but none were run - call expect(0) to accept zero assertions." ), e.stdout );
}
} );
QUnit.test( "requireExpects", async assert => {
const command = "qunit assert-expect/require-expects.js";
try {
const result = await execute( command );
assert.pushResult( {
result: false,
actual: result.stdout
} );
} catch ( e ) {
assert.equal( e.code, 1 );
assert.equal( e.stderr, "" );
assert.true( e.stdout.includes( "message: Expected number of assertions to be defined, but expect() was not called." ), e.stdout );
}
} );
} );
} );