@@ -5,6 +5,54 @@ import { describe, it, run } from 'node:test';
55import { dot , spec , tap } from 'node:test/reporters' ;
66import assert from 'node:assert' ;
77import util from 'node:util' ;
8+ import { spawn } from 'node:child_process' ;
9+
10+ import { writeFileSync , unlinkSync } from 'node:fs' ;
11+ import tmpdir from '../common/tmpdir.js' ;
12+ import { randomBytes } from 'node:crypto' ;
13+
14+ async function runTestInSubprocess ( testFile , options = { } ) {
15+ tmpdir . refresh ( ) ;
16+
17+ const tmpFile = tmpdir . resolve ( `test-subprocess-${ randomBytes ( 8 ) . toString ( 'hex' ) } .mjs` ) ;
18+
19+ const testNamePatterns = options . testNamePatterns ? JSON . stringify ( options . testNamePatterns ) : undefined ;
20+ const testSkipPatterns = options . testSkipPatterns ? JSON . stringify ( options . testSkipPatterns ) : undefined ;
21+ const isolation = options . isolation ? JSON . stringify ( options . isolation ) : undefined ;
22+
23+ const code = `
24+ import { run } from 'node:test';
25+ import { tap } from 'node:test/reporters';
26+ import { join } from 'node:path';
27+ const files = [${ JSON . stringify ( testFile ) } ];
28+ const opts = {
29+ files,
30+ reporter: 'tap',
31+ ${ testNamePatterns ? `testNamePatterns: ${ testNamePatterns } ,` : '' }
32+ ${ testSkipPatterns ? `testSkipPatterns: ${ testSkipPatterns } ,` : '' }
33+ ${ isolation ? `isolation: ${ isolation } ,` : '' }
34+ };
35+ const result = await run(opts).compose(tap).toArray();
36+ for (const line of result) {
37+ process.stdout.write(typeof line === 'string' ? line : String(line));
38+ }
39+ ` ;
40+ writeFileSync ( tmpFile , code ) ;
41+ return new Promise ( ( resolve , reject ) => {
42+ const child = spawn ( process . execPath , [ tmpFile ] , { stdio : [ 'ignore' , 'pipe' , 'pipe' ] } ) ;
43+ let stdout = '' ;
44+ let stderr = '' ;
45+ child . stdout . on ( 'data' , ( data ) => { stdout += data ; } ) ;
46+ child . stderr . on ( 'data' , ( data ) => { stderr += data ; } ) ;
47+ child . on ( 'close' , ( code ) => {
48+ unlinkSync ( tmpFile ) ;
49+ if ( code !== 0 ) {
50+ return reject ( new Error ( `Test failed with exit code ${ code } \n${ stderr } ` ) ) ;
51+ }
52+ resolve ( stdout ) ;
53+ } ) ;
54+ } ) ;
55+ }
856
957const testFixtures = fixtures . path ( 'test-runner' ) ;
1058
@@ -206,7 +254,7 @@ describe('require(\'node:test\').run', { concurrency: true }, () => {
206254 files : [ join ( testFixtures , 'default-behavior/test/random.cjs' ) ] ,
207255 watch : true ,
208256 signal : controller . signal ,
209- } ) . on ( 'data' , function ( { type } ) {
257+ } ) . on ( 'data' , function ( { type } ) {
210258 if ( type === 'test:watch:drained' ) {
211259 controller . abort ( ) ;
212260 }
@@ -650,45 +698,27 @@ describe('require(\'node:test\').run', { concurrency: true }, () => {
650698 } ) ;
651699} ) ;
652700
653- describe ( "with isolation set to 'none'" , ( ) => {
701+ describe ( "with isolation set to 'none'" , ( ) => {
654702 it ( 'should skip tests not matching testNamePatterns - string' , async ( ) => {
655- const result = await run ( {
656- files : [ join ( testFixtures , 'default-behavior/test/skip_by_name.cjs' ) ] ,
657- testNamePatterns : [ 'executed' ] ,
658- isolation : 'none' ,
659- } )
660- . compose ( tap )
661- . toArray ( ) ;
662- assert . strictEqual ( result [ 2 ] , 'ok 1 - this should be executed\n' ) ;
663- assert . strictEqual ( result [ 4 ] , '1..1\n' ) ;
664- assert . strictEqual ( result [ 5 ] , '# tests 1\n' ) ;
665- } ) ;
666-
667- it ( 'should skip tests not matching testNamePatterns - RegExp' , async ( ) => {
668- const result = await run ( {
669- files : [ join ( testFixtures , 'default-behavior/test/skip_by_name.cjs' ) ] ,
670- testNamePatterns : [ / e x e c u t e d / ] ,
671- isolation : 'none' ,
672- } )
673- . compose ( tap )
674- . toArray ( ) ;
675- assert . strictEqual ( result [ 2 ] , 'ok 1 - this should be executed\n' ) ;
676- assert . strictEqual ( result [ 4 ] , '1..1\n' ) ;
677- assert . strictEqual ( result [ 5 ] , '# tests 1\n' ) ;
703+ const result = await runTestInSubprocess (
704+ join ( testFixtures , 'default-behavior/test/skip_by_name.cjs' ) ,
705+ { isolation : 'none' , testNamePatterns : [ 'executed' ] }
706+ ) ;
707+
708+ assert . match ( result , / o k 1 - t h i s s h o u l d b e e x e c u t e d / ) ;
709+ assert . match ( result , / 1 \. \. 1 / ) ;
710+ assert . match ( result , / # t e s t s 1 / ) ;
678711 } ) ;
679712
680713 it ( 'should skip tests matching testSkipPatterns' , async ( ) => {
681- const result = await run ( {
682- files : [ join ( testFixtures , 'default-behavior/test/skip_by_name.cjs' ) ] ,
683- testSkipPatterns : [ 'skipped' ] ,
684- isolation : 'none' ,
685- } )
686- . compose ( tap )
687- . toArray ( ) ;
688-
689- assert . strictEqual ( result [ 2 ] , 'ok 1 - this should be executed\n' ) ;
690- assert . strictEqual ( result [ 4 ] , '1..1\n' ) ;
691- assert . strictEqual ( result [ 5 ] , '# tests 1\n' ) ;
714+ const result = await runTestInSubprocess (
715+ join ( testFixtures , 'default-behavior/test/skip_by_name.cjs' ) ,
716+ { isolation : 'none' , testSkipPatterns : [ 'skipped' ] }
717+ ) ;
718+
719+ assert . match ( result , / o k 1 - t h i s s h o u l d b e e x e c u t e d / ) ;
720+ assert . match ( result , / 1 \. \. 1 / ) ;
721+ assert . match ( result , / # t e s t s 1 / ) ;
692722 } ) ;
693723} ) ;
694724
0 commit comments