22const common = require ( '../common' ) ;
33const fixtures = require ( '../common/fixtures' ) ;
44const assert = require ( 'assert' ) ;
5- const { spawnSync } = require ( 'child_process' ) ;
6- const { setTimeout } = require ( 'timers/promises' ) ;
5+ const { spawnSync, spawn } = require ( 'child_process' ) ;
6+ const { once } = require ( 'events' ) ;
7+ const { finished } = require ( 'stream/promises' ) ;
8+
9+ async function runAndKill ( file ) {
10+ if ( common . isWindows ) {
11+ common . printSkipMessage ( `signals are not supported in windows, skipping ${ file } ` ) ;
12+ return ;
13+ }
14+ let stdout = '' ;
15+ const child = spawn ( process . execPath , [ '--test' , file ] ) ;
16+ child . stdout . setEncoding ( 'utf8' ) ;
17+ child . stdout . on ( 'data' , ( chunk ) => {
18+ if ( ! stdout . length ) child . kill ( 'SIGINT' ) ;
19+ stdout += chunk ;
20+ } ) ;
21+ const [ code , signal ] = await once ( child , 'exit' ) ;
22+ await finished ( child . stdout ) ;
23+ assert . match ( stdout , / n o t o k 1 / ) ;
24+ assert . match ( stdout , / # c a n c e l l e d 1 \n / ) ;
25+ assert . strictEqual ( signal , null ) ;
26+ assert . strictEqual ( code , 1 ) ;
27+ }
728
829if ( process . argv [ 2 ] === 'child' ) {
930 const test = require ( 'node:test' ) ;
@@ -17,12 +38,6 @@ if (process.argv[2] === 'child') {
1738 test ( 'failing test' , ( ) => {
1839 assert . strictEqual ( true , false ) ;
1940 } ) ;
20- } else if ( process . argv [ 3 ] === 'never_ends' ) {
21- assert . strictEqual ( process . argv [ 3 ] , 'never_ends' ) ;
22- test ( 'never ending test' , ( ) => {
23- return setTimeout ( 100_000_000 ) ;
24- } ) ;
25- process . kill ( process . pid , 'SIGINT' ) ;
2641 } else assert . fail ( 'unreachable' ) ;
2742} else {
2843 let child = spawnSync ( process . execPath , [ __filename , 'child' , 'pass' ] ) ;
@@ -37,14 +52,6 @@ if (process.argv[2] === 'child') {
3752 assert . strictEqual ( child . status , 1 ) ;
3853 assert . strictEqual ( child . signal , null ) ;
3954
40- child = spawnSync ( process . execPath , [ __filename , 'child' , 'never_ends' ] ) ;
41- assert . strictEqual ( child . status , 1 ) ;
42- assert . strictEqual ( child . signal , null ) ;
43- if ( common . isWindows ) {
44- common . printSkipMessage ( 'signals are not supported in windows' ) ;
45- } else {
46- const stdout = child . stdout . toString ( ) ;
47- assert . match ( stdout , / n o t o k 1 - n e v e r e n d i n g t e s t / ) ;
48- assert . match ( stdout , / # c a n c e l l e d 1 / ) ;
49- }
55+ runAndKill ( fixtures . path ( 'test-runner' , 'never_ending_sync.js' ) ) . then ( common . mustCall ( ) ) ;
56+ runAndKill ( fixtures . path ( 'test-runner' , 'never_ending_async.js' ) ) . then ( common . mustCall ( ) ) ;
5057}
0 commit comments