-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
Expand file tree
/
Copy pathtest-runner-run.mjs
More file actions
873 lines (767 loc) · 29.1 KB
/
test-runner-run.mjs
File metadata and controls
873 lines (767 loc) · 29.1 KB
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
import * as common from '../common/index.mjs';
import * as fixtures from '../common/fixtures.mjs';
import { basename, join } from 'node:path';
import { describe, it, run } from 'node:test';
import { dot, spec, tap } from 'node:test/reporters';
import consumers from 'node:stream/consumers';
import assert from 'node:assert';
import util from 'node:util';
const testFixtures = fixtures.path('test-runner');
const rerunStateFile = join(testFixtures, 'rerun-state.json');
describe('require(\'node:test\').run', { concurrency: true }, () => {
it('should run with no tests', async () => {
const stream = run({ files: [] });
stream.on('test:fail', common.mustNotCall());
stream.on('test:pass', common.mustNotCall());
// eslint-disable-next-line no-unused-vars
for await (const _ of stream);
});
it('should fail with non existing file', async () => {
const stream = run({ files: ['a-random-file-that-does-not-exist.js'] });
stream.on('test:fail', common.mustCall(1));
stream.on('test:pass', common.mustNotCall());
// eslint-disable-next-line no-unused-vars
for await (const _ of stream);
});
it('should succeed with a file', async () => {
const stream = run({ files: [join(testFixtures, 'default-behavior/test/random.cjs')] });
stream.on('test:fail', common.mustNotCall());
stream.on('test:pass', common.mustCall(1));
// eslint-disable-next-line no-unused-vars
for await (const _ of stream);
});
it('should emit diagnostic events with level parameter', async () => {
const diagnosticEvents = [];
const stream = run({
files: [join(testFixtures, 'coverage.js')],
reporter: 'spec',
});
stream.on('test:diagnostic', (event) => {
diagnosticEvents.push(event);
});
// eslint-disable-next-line no-unused-vars
for await (const _ of stream);
assert(diagnosticEvents.length > 0, 'No diagnostic events were emitted');
const infoEvent = diagnosticEvents.find((e) => e.level === 'info');
assert(infoEvent, 'No diagnostic events with level "info" were emitted');
});
const argPrintingFile = join(testFixtures, 'print-arguments.js');
it('should allow custom arguments via execArgv', async () => {
const result = await run({ files: [argPrintingFile], execArgv: ['-p', '"Printed"'] }).compose(spec).toArray();
assert.strictEqual(result[0].toString(), 'Printed\n');
});
it('should allow custom arguments via argv', async () => {
const stream = run({ files: [argPrintingFile], argv: ['--a-custom-argument'] });
stream.on('test:fail', common.mustNotCall());
stream.on('test:pass', common.mustCall());
// eslint-disable-next-line no-unused-vars
for await (const _ of stream);
});
it('should run same file twice', async () => {
const stream = run({
files: [
join(testFixtures, 'default-behavior/test/random.cjs'),
join(testFixtures, 'default-behavior/test/random.cjs'),
]
});
stream.on('test:fail', common.mustNotCall());
stream.on('test:pass', common.mustCall(2));
// eslint-disable-next-line no-unused-vars
for await (const _ of stream);
});
it('should run a failed test', async () => {
const stream = run({ files: [testFixtures] });
stream.on('test:fail', common.mustCall(1));
stream.on('test:pass', common.mustNotCall());
// eslint-disable-next-line no-unused-vars
for await (const _ of stream);
});
it('should support timeout', async () => {
const stream = run({ timeout: 50, files: [
fixtures.path('test-runner', 'plan', 'timeout-basic.mjs'),
] });
stream.on('test:fail', common.mustCall((data) => {
assert.strictEqual(data.details.error.failureType, 'testTimeoutFailure');
}, 2));
stream.on('test:pass', common.mustNotCall());
// eslint-disable-next-line no-unused-vars
for await (const _ of stream);
});
for (const isolation of ['process', 'none']) {
it(`should preserve cancelled queued subtest order without randomization (${isolation})`, async () => {
const stream = run({
files: [fixtures.path('test-runner', 'cancelled-report-order.mjs')],
isolation,
});
const cancelledFailures = [];
stream.on('test:fail', ({ name, details }) => {
if (name === 'second' || name === 'third') {
cancelledFailures.push({
name,
failureType: details.error.failureType,
});
}
});
// eslint-disable-next-line no-unused-vars
for await (const _ of stream);
assert.deepStrictEqual(cancelledFailures, [
{ name: 'second', failureType: 'cancelledByParent' },
{ name: 'third', failureType: 'cancelledByParent' },
]);
});
}
it('should be piped with dot', async () => {
const result = await run({
files: [join(testFixtures, 'default-behavior/test/random.cjs')]
}).compose(dot).toArray();
assert.strictEqual(result.length, 2);
assert.strictEqual(util.stripVTControlCharacters(result[0]), '.');
assert.strictEqual(result[1], '\n');
});
describe('should be piped with spec reporter', () => {
it('new spec', async () => {
const specReporter = new spec();
const result = await consumers.text(run({
files: [join(testFixtures, 'default-behavior/test/random.cjs')]
}).compose(specReporter));
assert.match(result, /this should pass/);
assert.match(result, /tests 1/);
assert.match(result, /pass 1/);
});
it('spec()', async () => {
const specReporter = spec();
const result = await consumers.text(run({
files: [join(testFixtures, 'default-behavior/test/random.cjs')]
}).compose(specReporter));
assert.match(result, /this should pass/);
assert.match(result, /tests 1/);
assert.match(result, /pass 1/);
});
it('spec', async () => {
const result = await consumers.text(run({
files: [join(testFixtures, 'default-behavior/test/random.cjs')]
}).compose(spec));
assert.match(result, /this should pass/);
assert.match(result, /tests 1/);
assert.match(result, /pass 1/);
});
});
it('should be piped with tap', async () => {
const result = await run({
files: [join(testFixtures, 'default-behavior/test/random.cjs')]
}).compose(tap).toArray();
assert.strictEqual(result.length, 13);
assert.strictEqual(result[0], 'TAP version 13\n');
assert.strictEqual(result[1], '# Subtest: this should pass\n');
assert.strictEqual(result[2], 'ok 1 - this should pass\n');
assert.match(result[3], /duration_ms: \d+\.?\d*/);
assert.strictEqual(result[4], '1..1\n');
assert.strictEqual(result[5], '# tests 1\n');
assert.strictEqual(result[6], '# suites 0\n');
assert.strictEqual(result[7], '# pass 1\n');
assert.strictEqual(result[8], '# fail 0\n');
assert.strictEqual(result[9], '# cancelled 0\n');
assert.strictEqual(result[10], '# skipped 0\n');
assert.strictEqual(result[11], '# todo 0\n');
assert.match(result[12], /# duration_ms \d+\.?\d*/);
});
it('should skip tests not matching testNamePatterns - RegExp', async () => {
const result = await run({
files: [join(testFixtures, 'default-behavior/test/skip_by_name.cjs')],
testNamePatterns: [/executed/]
})
.compose(tap)
.toArray();
assert.strictEqual(result[2], 'ok 1 - this should be executed\n');
assert.strictEqual(result[4], '1..1\n');
assert.strictEqual(result[5], '# tests 1\n');
});
it('should skip tests not matching testNamePatterns - string', async () => {
const result = await run({
files: [join(testFixtures, 'default-behavior/test/skip_by_name.cjs')],
testNamePatterns: ['executed']
})
.compose(tap)
.toArray();
assert.strictEqual(result[2], 'ok 1 - this should be executed\n');
assert.strictEqual(result[4], '1..1\n');
assert.strictEqual(result[5], '# tests 1\n');
});
it('should pass only to children', async () => {
const result = await run({
files: [join(testFixtures, 'test_only.js')],
only: true
})
.compose(tap)
.toArray();
assert.strictEqual(result[2], 'ok 1 - this should be executed\n');
assert.strictEqual(result[4], '1..1\n');
assert.strictEqual(result[5], '# tests 1\n');
});
it('should emit "test:watch:drained" event on watch mode', async () => {
const controller = new AbortController();
await run({
files: [join(testFixtures, 'default-behavior/test/random.cjs')],
watch: true,
signal: controller.signal,
}).on('data', function({ type }) {
if (type === 'test:watch:drained') {
controller.abort();
}
});
});
it('should include test type in enqueue, dequeue events', async (t) => {
const stream = await run({
files: [join(testFixtures, 'default-behavior/test/suite_and_test.cjs')],
});
t.plan(4);
stream.on('test:enqueue', common.mustCall((data) => {
if (data.name === 'this is a suite') {
t.assert.strictEqual(data.type, 'suite');
}
if (data.name === 'this is a test') {
t.assert.strictEqual(data.type, 'test');
}
}, 3));
stream.on('test:dequeue', common.mustCall((data) => {
if (data.name === 'this is a suite') {
t.assert.strictEqual(data.type, 'suite');
}
if (data.name === 'this is a test') {
t.assert.strictEqual(data.type, 'test');
}
}, 3));
// eslint-disable-next-line no-unused-vars
for await (const _ of stream);
});
describe('AbortSignal', () => {
it('should accept a signal', async () => {
const stream = run({ signal: AbortSignal.timeout(50), files: [
fixtures.path('test-runner', 'never_ending_sync.js'),
fixtures.path('test-runner', 'never_ending_async.js'),
] });
stream.on('test:fail', common.mustCall(2));
stream.on('test:pass', common.mustNotCall());
// eslint-disable-next-line no-unused-vars
for await (const _ of stream);
});
it('should stop watch mode when abortSignal aborts', async () => {
const controller = new AbortController();
const result = await run({
files: [join(testFixtures, 'default-behavior/test/random.cjs')],
watch: true,
signal: controller.signal,
})
.compose(async function* (source) {
let waitForCancel = 2;
for await (const chunk of source) {
if (chunk.type === 'test:watch:drained' ||
(chunk.type === 'test:diagnostic' && chunk.data.message.startsWith('duration_ms'))) {
waitForCancel--;
}
if (waitForCancel === 0) {
controller.abort();
}
if (chunk.type === 'test:pass') {
yield chunk.data.name;
}
}
})
.toArray();
assert.deepStrictEqual(result, ['this should pass']);
});
it('should abort when test succeeded', async () => {
const stream = run({
files: [
fixtures.path(
'test-runner',
'aborts',
'successful-test-still-call-abort.js'
),
],
});
let passedTestCount = 0;
let failedTestCount = 0;
let output = '';
for await (const data of stream) {
if (data.type === 'test:stdout') {
output += data.data.message.toString();
}
if (data.type === 'test:fail') {
failedTestCount++;
}
if (data.type === 'test:pass') {
passedTestCount++;
}
}
assert.match(output, /abort called for test 1/);
assert.match(output, /abort called for test 2/);
assert.strictEqual(failedTestCount, 0, new Error('no tests should fail'));
assert.strictEqual(passedTestCount, 2);
});
it('should abort when test failed', async () => {
const stream = run({
files: [
fixtures.path(
'test-runner',
'aborts',
'failed-test-still-call-abort.js'
),
],
});
let passedTestCount = 0;
let failedTestCount = 0;
let output = '';
for await (const data of stream) {
if (data.type === 'test:stdout') {
output += data.data.message.toString();
}
if (data.type === 'test:fail') {
failedTestCount++;
}
if (data.type === 'test:pass') {
passedTestCount++;
}
}
assert.match(output, /abort called for test 1/);
assert.match(output, /abort called for test 2/);
assert.strictEqual(passedTestCount, 0, new Error('no tests should pass'));
assert.strictEqual(failedTestCount, 2);
});
});
const shardsTestsFixtures = fixtures.path('test-runner', 'shards');
const internalOrderTestFile = join(testFixtures, 'randomize', 'internal-order.cjs');
const shardFileNames = [
'a.cjs',
'b.cjs',
'c.cjs',
'd.cjs',
'e.cjs',
'f.cjs',
'g.cjs',
'h.cjs',
'i.cjs',
'j.cjs',
];
const internalTestNames = ['a', 'b', 'c', 'd', 'e'];
const shardsTestsFiles = shardFileNames.map((file) => join(shardsTestsFixtures, file));
async function getExecutedShardOrder(options = {}) {
const stream = run({
files: shardsTestsFiles,
concurrency: false,
...options,
});
const executedTestFiles = [];
stream.on('test:fail', common.mustNotCall());
stream.on('test:pass', (passedTest) => {
if (passedTest.nesting === 0) {
executedTestFiles.push(basename(passedTest.file));
}
});
// eslint-disable-next-line no-unused-vars
for await (const _ of stream) ;
return executedTestFiles;
}
async function getExecutedInternalOrder(options = {}) {
const stream = run({
files: [internalOrderTestFile],
concurrency: false,
...options,
});
const executionOrder = [];
stream.on('test:fail', common.mustNotCall());
stream.on('test:pass', (passedTest) => {
if (passedTest.file === internalOrderTestFile && passedTest.name.startsWith('internal ')) {
executionOrder.push(passedTest.name.slice('internal '.length));
}
});
// eslint-disable-next-line no-unused-vars
for await (const _ of stream) ;
return executionOrder;
}
describe('sharding', () => {
describe('validation', () => {
it('should require shard.total when having shard option', () => {
assert.throws(() => run({ files: shardsTestsFiles, shard: {} }), {
name: 'TypeError',
code: 'ERR_INVALID_ARG_TYPE',
message: 'The "options.shard.total" property must be of type number. Received undefined'
});
});
it('should require shard.index when having shards option', () => {
assert.throws(() => run({
files: shardsTestsFiles,
shard: {
total: 5
}
}), {
name: 'TypeError',
code: 'ERR_INVALID_ARG_TYPE',
message: 'The "options.shard.index" property must be of type number. Received undefined'
});
});
it('should require shard.total to be greater than 0 when having shard option', () => {
assert.throws(() => run({
files: shardsTestsFiles,
shard: {
total: 0,
index: 1
}
}), {
name: 'RangeError',
code: 'ERR_OUT_OF_RANGE',
message:
'The value of "options.shard.total" is out of range. It must be >= 1 && <= 9007199254740991. Received 0'
});
});
it('should require shard.index to be greater than 0 when having shard option', () => {
assert.throws(() => run({
files: shardsTestsFiles,
shard: {
total: 6,
index: 0
}
}), {
name: 'RangeError',
code: 'ERR_OUT_OF_RANGE',
message: 'The value of "options.shard.index" is out of range. It must be >= 1 && <= 6. Received 0'
});
});
it('should require shard.index to not be greater than the shards total when having shard option', () => {
assert.throws(() => run({
files: shardsTestsFiles,
shard: {
total: 6,
index: 7
}
}), {
name: 'RangeError',
code: 'ERR_OUT_OF_RANGE',
message: 'The value of "options.shard.index" is out of range. It must be >= 1 && <= 6. Received 7'
});
});
it('should require watch mode to be disabled when having shard option', () => {
assert.throws(() => run({
files: shardsTestsFiles,
watch: true,
shard: {
total: 6,
index: 1
}
}), {
name: 'TypeError',
code: 'ERR_INVALID_ARG_VALUE',
message: 'The property \'options.shard\' shards not supported with watch mode. Received true'
});
});
});
it('should run only the tests files matching the shard index', async () => {
const stream = run({
files: shardsTestsFiles,
shard: {
total: 5,
index: 1
}
});
const executedTestFiles = [];
stream.on('test:fail', common.mustNotCall());
stream.on('test:pass', (passedTest) => {
executedTestFiles.push(passedTest.file);
});
// eslint-disable-next-line no-unused-vars
for await (const _ of stream) ;
assert.deepStrictEqual(executedTestFiles, [
join(shardsTestsFixtures, 'a.cjs'),
join(shardsTestsFixtures, 'f.cjs'),
]);
});
it('different shards should not run the same file', async () => {
const executedTestFiles = [];
const testStreams = [];
const shards = 5;
for (let i = 1; i <= shards; i++) {
const stream = run({
files: shardsTestsFiles,
shard: {
total: shards,
index: i
}
});
stream.on('test:fail', common.mustNotCall());
stream.on('test:pass', (passedTest) => {
executedTestFiles.push(passedTest.file);
});
testStreams.push(stream);
}
await Promise.all(testStreams.map(async (stream) => {
// eslint-disable-next-line no-unused-vars
for await (const _ of stream) ;
}));
assert.deepStrictEqual(executedTestFiles, [...new Set(executedTestFiles)]);
});
it('combination of all shards should be all the tests', async () => {
const executedTestFiles = [];
const testStreams = [];
const shards = 5;
for (let i = 1; i <= shards; i++) {
const stream = run({
files: shardsTestsFiles,
shard: {
total: shards,
index: i
}
});
stream.on('test:fail', common.mustNotCall());
stream.on('test:pass', (passedTest) => {
executedTestFiles.push(passedTest.file);
});
testStreams.push(stream);
}
await Promise.all(testStreams.map(async (stream) => {
// eslint-disable-next-line no-unused-vars
for await (const _ of stream) ;
}));
assert.deepStrictEqual(executedTestFiles.sort(), [...shardsTestsFiles].sort());
});
});
describe('randomization', () => {
it('should randomize file order deterministically when using the same seed', async () => {
const firstOrder = await getExecutedShardOrder({ randomize: true, randomSeed: 12345 });
const secondOrder = await getExecutedShardOrder({ randomize: true, randomSeed: 12345 });
assert.deepStrictEqual(firstOrder, secondOrder);
assert.deepStrictEqual([...firstOrder].sort(), [...shardFileNames].sort());
});
it('should randomize file order differently with different seeds', async () => {
const firstOrder = await getExecutedShardOrder({ randomize: true, randomSeed: 11111 });
const secondOrder = await getExecutedShardOrder({ randomize: true, randomSeed: 22222 });
assert.notDeepStrictEqual(firstOrder, secondOrder);
assert.deepStrictEqual([...firstOrder].sort(), [...shardFileNames].sort());
assert.deepStrictEqual([...secondOrder].sort(), [...shardFileNames].sort());
});
it('should randomize file order when only randomSeed is provided', async () => {
const firstOrder = await getExecutedShardOrder({ randomSeed: 24680 });
const secondOrder = await getExecutedShardOrder({ randomSeed: 24680 });
assert.deepStrictEqual(firstOrder, secondOrder);
assert.deepStrictEqual([...firstOrder].sort(), [...shardFileNames].sort());
});
it('should randomize internal test order deterministically when using the same seed', async () => {
const firstOrder = await getExecutedInternalOrder({ randomSeed: 12345 });
const secondOrder = await getExecutedInternalOrder({ randomSeed: 12345 });
assert.deepStrictEqual(firstOrder, secondOrder);
assert.deepStrictEqual([...firstOrder].sort(), [...internalTestNames].sort());
});
it('should randomize internal test order differently across seeds', async () => {
const orders = [];
for (const seed of [11111, 22222, 33333, 44444]) {
const order = await getExecutedInternalOrder({ randomSeed: seed });
assert.deepStrictEqual([...order].sort(), [...internalTestNames].sort());
orders.push(order.join(','));
}
assert.notStrictEqual(new Set(orders).size, 1);
});
it('should emit the randomization seed as a diagnostic message', async () => {
const stream = run({
files: shardsTestsFiles,
concurrency: false,
randomize: true,
});
const diagnostics = [];
stream.on('test:diagnostic', ({ message }) => {
diagnostics.push(message);
});
// eslint-disable-next-line no-unused-vars
for await (const _ of stream) ;
assert(
diagnostics.some((message) => /Randomized test order seed: \d+/.test(message)),
`Missing randomization seed diagnostic. Received diagnostics: ${diagnostics.join(', ')}`,
);
});
});
describe('validation', () => {
it('should only allow array in options.files', async () => {
[Symbol(), {}, () => {}, 0, 1, 0n, 1n, '', '1', Promise.resolve([]), true, false]
.forEach((files) => assert.throws(() => run({ files }), {
code: 'ERR_INVALID_ARG_TYPE'
}));
});
it('should only allow array in options.globPatterns', async () => {
[Symbol(), {}, () => {}, 0, 1, 0n, 1n, '', '1', Promise.resolve([]), true, false]
.forEach((globPatterns) => assert.throws(() => run({ globPatterns }), {
code: 'ERR_INVALID_ARG_TYPE'
}));
});
it('should not allow files and globPatterns used together', () => {
assert.throws(() => run({ files: ['a.js'], globPatterns: ['*.js'] }), {
code: 'ERR_INVALID_ARG_VALUE'
});
});
it('should not allow randomize with watch mode', () => {
assert.throws(() => run({ watch: true, randomize: true }), {
code: 'ERR_INVALID_ARG_VALUE',
message: /The property 'options\.randomize' is not supported with watch mode\./,
});
});
it('should not allow randomSeed with watch mode', () => {
assert.throws(() => run({ watch: true, randomSeed: 12345 }), {
code: 'ERR_INVALID_ARG_VALUE',
message: /The property 'options\.randomSeed' is not supported with watch mode\./,
});
});
it('should not allow randomize with rerunFailuresFilePath', () => {
assert.throws(() => run({ randomize: true, rerunFailuresFilePath: rerunStateFile }), {
code: 'ERR_INVALID_ARG_VALUE',
message: /The property 'options\.randomize' is not supported with rerun failures mode\./,
});
});
it('should not allow randomSeed with rerunFailuresFilePath', () => {
assert.throws(() => run({ randomSeed: 12345, rerunFailuresFilePath: rerunStateFile }), {
code: 'ERR_INVALID_ARG_VALUE',
message: /The property 'options\.randomSeed' is not supported with rerun failures mode\./,
});
});
it('should not allow decimal randomSeed values', () => {
assert.throws(() => run({ randomSeed: 1.5 }), {
code: 'ERR_OUT_OF_RANGE',
});
});
it('should only allow a string in options.cwd', async () => {
[Symbol(), {}, [], () => {}, 0, 1, 0n, 1n, true, false]
.forEach((cwd) => assert.throws(() => run({ cwd }), {
code: 'ERR_INVALID_ARG_TYPE'
}));
});
it('should only allow object as options', () => {
[Symbol(), [], () => {}, 0, 1, 0n, 1n, '', '1', true, false]
.forEach((options) => assert.throws(() => run(options), {
code: 'ERR_INVALID_ARG_TYPE'
}));
});
it('should pass instance of stream to setup', async () => {
const stream = run({
files: [join(testFixtures, 'default-behavior/test/random.cjs')],
setup: common.mustCall((root) => {
assert.strictEqual(root.constructor.name, 'TestsStream');
}),
});
stream.on('test:fail', common.mustNotCall());
stream.on('test:pass', common.mustCall());
// eslint-disable-next-line no-unused-vars
for await (const _ of stream);
});
});
it('should avoid running recursively', async () => {
const stream = run({ files: [join(testFixtures, 'recursive_run.js')] });
let stderr = '';
stream.on('test:fail', common.mustNotCall());
stream.on('test:pass', common.mustCall(1));
stream.on('test:stderr', (c) => { stderr += c.message; });
// eslint-disable-next-line no-unused-vars
for await (const _ of stream);
assert.match(stderr, /Warning: node:test run\(\) is being called recursively/);
});
it('should run with different cwd', async () => {
const stream = run({
cwd: fixtures.path('test-runner', 'cwd')
});
stream.on('test:fail', common.mustNotCall());
stream.on('test:pass', common.mustCall(1));
// eslint-disable-next-line no-unused-vars
for await (const _ of stream);
});
it('should handle a non-existent directory being provided as cwd', async () => {
const diagnostics = [];
const stream = run({
cwd: fixtures.path('test-runner', 'cwd', 'non-existing')
});
stream.on('test:fail', common.mustNotCall());
stream.on('test:pass', common.mustNotCall());
stream.on('test:stderr', common.mustNotCall());
stream.on('test:diagnostic', ({ message }) => {
diagnostics.push(message);
});
// eslint-disable-next-line no-unused-vars
for await (const _ of stream);
for (const entry of [
'tests 0',
'suites 0',
'pass 0',
'fail 0',
'cancelled 0',
'skipped 0',
'todo 0',
]
) {
assert.strictEqual(diagnostics.includes(entry), true);
}
});
it('should handle a non-existent file being provided as cwd', async () => {
const diagnostics = [];
const stream = run({
cwd: fixtures.path('test-runner', 'default-behavior', 'test', 'random.cjs')
});
stream.on('test:fail', common.mustNotCall());
stream.on('test:pass', common.mustNotCall());
stream.on('test:stderr', common.mustNotCall());
stream.on('test:diagnostic', ({ message }) => {
diagnostics.push(message);
});
// eslint-disable-next-line no-unused-vars
for await (const _ of stream);
for (const entry of [
'tests 0',
'suites 0',
'pass 0',
'fail 0',
'cancelled 0',
'skipped 0',
'todo 0',
]
) {
assert.strictEqual(diagnostics.includes(entry), true);
}
});
});
describe('env', () => {
it('should allow env variables to be configured', async () => {
// Need to inherit some process.env variables so it runs reliably across different environments.
const env = { ...process.env, FOOBAR: 'FUZZBUZZ' };
// Set a variable on main process env and test it does not exist within test env.
process.env.ABC = 'XYZ';
const stream = run({ files: [join(testFixtures, 'process-env.js')], env });
stream.on('test:fail', common.mustNotCall());
stream.on('test:pass', common.mustCall(1));
// eslint-disable-next-line no-unused-vars
for await (const _ of stream);
delete process.env.ABC;
});
it('should throw error when env is specified with isolation=none', async () => {
assert.throws(() => run({ env: { foo: 'bar' }, isolation: 'none' }), {
code: 'ERR_INVALID_ARG_VALUE',
message: /The property 'options\.env' is not supported with isolation='none'\. Received { foo: 'bar' }/
});
});
});
describe('forceExit', () => {
it('throws for non-boolean values', () => {
[Symbol(), {}, 0, 1, '1', Promise.resolve([])].forEach((forceExit) => {
assert.throws(() => run({ forceExit }), {
code: 'ERR_INVALID_ARG_TYPE',
message: /The "options\.forceExit" property must be of type boolean\./
});
});
});
it('throws if enabled with watch mode', () => {
assert.throws(() => run({ forceExit: true, watch: true }), {
code: 'ERR_INVALID_ARG_VALUE',
message: /The property 'options\.forceExit' is not supported with watch mode\./
});
});
});
// exitHandler doesn't run until after the tests / after hooks finish.
process.on('exit', () => {
assert.strictEqual(process.listeners('uncaughtException').length, 0);
assert.strictEqual(process.listeners('unhandledRejection').length, 0);
assert.strictEqual(process.listeners('beforeExit').length, 0);
assert.strictEqual(process.listeners('SIGINT').length, 0);
assert.strictEqual(process.listeners('SIGTERM').length, 0);
});