@@ -6,6 +6,9 @@ import { dot, spec, tap } from 'node:test/reporters';
6
6
import assert from 'node:assert' ;
7
7
8
8
const testFixtures = fixtures . path ( 'test-runner' ) ;
9
+ const skipIfNoInspector = {
10
+ skip : ! process . features . inspector ? 'inspector disabled' : false
11
+ } ;
9
12
10
13
describe ( 'require(\'node:test\').run' , { concurrency : true } , ( ) => {
11
14
it ( 'should run with no tests' , async ( ) => {
@@ -502,6 +505,125 @@ describe('require(\'node:test\').run', { concurrency: true }, () => {
502
505
} ) ;
503
506
} ) ;
504
507
508
+ describe ( 'coverage' , ( ) => {
509
+ describe ( 'validation' , ( ) => {
510
+
511
+ it ( 'should only allow boolean in options.coverage' , async ( ) => {
512
+ [ Symbol ( ) , { } , ( ) => { } , 0 , 1 , 0n , 1n , '' , '1' , Promise . resolve ( true ) , [ ] ]
513
+ . forEach ( ( coverage ) => assert . throws ( ( ) => run ( { coverage } ) , {
514
+ code : 'ERR_INVALID_ARG_TYPE'
515
+ } ) ) ;
516
+ } ) ;
517
+
518
+ it ( 'should only allow coverageExcludePatterns and coverageIncludePatterns when coverage is true' , async ( ) => {
519
+ assert . throws (
520
+ ( ) => run ( { coverage : false , coverageIncludePatterns : [ ] } ) ,
521
+ { code : 'ERR_INVALID_ARG_VALUE' } ,
522
+ ) ;
523
+ assert . throws (
524
+ ( ) => run ( { coverage : false , coverageExcludePatterns : [ ] } ) ,
525
+ { code : 'ERR_INVALID_ARG_VALUE' } ,
526
+ ) ;
527
+ } ) ;
528
+
529
+ it ( 'should only allow string|string[] in options.coverageExcludePatterns' , async ( ) => {
530
+ [ Symbol ( ) , { } , ( ) => { } , 0 , 1 , 0n , 1n , Promise . resolve ( [ ] ) , true , false ]
531
+ . forEach ( ( coverageExcludePatterns ) => {
532
+ assert . throws ( ( ) => run ( { coverage : true , coverageExcludePatterns } ) , {
533
+ code : 'ERR_INVALID_ARG_TYPE'
534
+ } ) ;
535
+ assert . throws ( ( ) => run ( { coverage : true , coverageExcludePatterns : [ coverageExcludePatterns ] } ) , {
536
+ code : 'ERR_INVALID_ARG_TYPE'
537
+ } ) ;
538
+ } ) ;
539
+ run ( { files : [ ] , signal : AbortSignal . abort ( ) , coverage : true , coverageExcludePatterns : [ '' ] } ) ;
540
+ run ( { files : [ ] , signal : AbortSignal . abort ( ) , coverage : true , coverageExcludePatterns : '' } ) ;
541
+ } ) ;
542
+
543
+ it ( 'should only allow string|string[] in options.coverageIncludePatterns' , async ( ) => {
544
+ [ Symbol ( ) , { } , ( ) => { } , 0 , 1 , 0n , 1n , Promise . resolve ( [ ] ) , true , false ]
545
+ . forEach ( ( coverageIncludePatterns ) => {
546
+ assert . throws ( ( ) => run ( { coverage : true , coverageIncludePatterns } ) , {
547
+ code : 'ERR_INVALID_ARG_TYPE'
548
+ } ) ;
549
+ assert . throws ( ( ) => run ( { coverage : true , coverageIncludePatterns : [ coverageIncludePatterns ] } ) , {
550
+ code : 'ERR_INVALID_ARG_TYPE'
551
+ } ) ;
552
+ } ) ;
553
+
554
+ run ( { files : [ ] , signal : AbortSignal . abort ( ) , coverage : true , coverageIncludePatterns : [ '' ] } ) ;
555
+ run ( { files : [ ] , signal : AbortSignal . abort ( ) , coverage : true , coverageIncludePatterns : '' } ) ;
556
+ } ) ;
557
+ } ) ;
558
+
559
+ const files = [ fixtures . path ( 'test-runner' , 'coverage.js' ) ] ;
560
+ it ( 'should run with coverage' , skipIfNoInspector , async ( ) => {
561
+ const stream = run ( { files, coverage : true } ) ;
562
+ stream . on ( 'test:fail' , common . mustNotCall ( ) ) ;
563
+ stream . on ( 'test:pass' , common . mustCall ( 1 ) ) ;
564
+ stream . on ( 'test:coverage' , common . mustCall ( ) ) ;
565
+ // eslint-disable-next-line no-unused-vars
566
+ for await ( const _ of stream ) ;
567
+ } ) ;
568
+
569
+ it ( 'should run with coverage and exclude by glob' , skipIfNoInspector , async ( ) => {
570
+ const stream = run ( { files, coverage : true , coverageExcludePatterns : [ 'test/*/test-runner/invalid-tap.js' ] } ) ;
571
+ stream . on ( 'test:fail' , common . mustNotCall ( ) ) ;
572
+ stream . on ( 'test:pass' , common . mustCall ( 1 ) ) ;
573
+ stream . on ( 'test:coverage' , common . mustCall ( ( { summary : { files } } ) => {
574
+ const filesPaths = files . map ( ( { path } ) => path ) ;
575
+ assert . strictEqual ( filesPaths . some ( ( path ) => path . includes ( 'test-runner/invalid-tap.js' ) ) , false ) ;
576
+ } ) ) ;
577
+ // eslint-disable-next-line no-unused-vars
578
+ for await ( const _ of stream ) ;
579
+ } ) ;
580
+
581
+ it ( 'should run with coverage and include by glob' , skipIfNoInspector , async ( ) => {
582
+ const stream = run ( { files, coverage : true , coverageIncludePatterns : [ 'test/*/test-runner/invalid-tap.js' ] } ) ;
583
+ stream . on ( 'test:fail' , common . mustNotCall ( ) ) ;
584
+ stream . on ( 'test:pass' , common . mustCall ( 1 ) ) ;
585
+ stream . on ( 'test:coverage' , common . mustCall ( ( { summary : { files } } ) => {
586
+ const filesPaths = files . map ( ( { path } ) => path ) ;
587
+ assert . strictEqual ( filesPaths . some ( ( path ) => path . includes ( 'test-runner/invalid-tap.js' ) ) , true ) ;
588
+ } ) ) ;
589
+ // eslint-disable-next-line no-unused-vars
590
+ for await ( const _ of stream ) ;
591
+ } ) ;
592
+ } ) ;
593
+
594
+ it ( 'should run with no files' , async ( ) => {
595
+ const stream = run ( {
596
+ files : undefined
597
+ } ) . compose ( tap ) ;
598
+ stream . on ( 'test:fail' , common . mustNotCall ( ) ) ;
599
+ stream . on ( 'test:pass' , common . mustNotCall ( ) ) ;
600
+
601
+ // eslint-disable-next-line no-unused-vars
602
+ for await ( const _ of stream ) ;
603
+ } ) ;
604
+
605
+ it ( 'should run with no files and use spec reporter' , async ( ) => {
606
+ const stream = run ( {
607
+ files : undefined
608
+ } ) . compose ( spec ) ;
609
+ stream . on ( 'test:fail' , common . mustNotCall ( ) ) ;
610
+ stream . on ( 'test:pass' , common . mustNotCall ( ) ) ;
611
+
612
+ // eslint-disable-next-line no-unused-vars
613
+ for await ( const _ of stream ) ;
614
+ } ) ;
615
+
616
+ it ( 'should run with no files and use dot reporter' , async ( ) => {
617
+ const stream = run ( {
618
+ files : undefined
619
+ } ) . compose ( dot ) ;
620
+ stream . on ( 'test:fail' , common . mustNotCall ( ) ) ;
621
+ stream . on ( 'test:pass' , common . mustNotCall ( ) ) ;
622
+
623
+ // eslint-disable-next-line no-unused-vars
624
+ for await ( const _ of stream ) ;
625
+ } ) ;
626
+
505
627
it ( 'should avoid running recursively' , async ( ) => {
506
628
const stream = run ( { files : [ join ( testFixtures , 'recursive_run.js' ) ] } ) ;
507
629
let stderr = '' ;
0 commit comments