1- // https://github.com/nodejs/node/blob/2fd4c013c221653da2a7921d08fe1aa96aaba504/test/parallel/test-runner-exit-code.js
2-
1+ // https://github.com/nodejs/node/blob/26e27424ad91c60a44d3d4c58b62a39b555ba75d/test/parallel/test-runner-exit-code.js
32'use strict'
4-
53const common = require ( '../common' )
64const fixtures = require ( '../common/fixtures' )
75const assert = require ( 'assert' )
8- const { spawnSync } = require ( 'child_process' )
9- const { promisify } = require ( 'util' )
10- const setTimeout = promisify ( require ( 'timers' ) . setTimeout )
6+ const { spawnSync, spawn } = require ( 'child_process' )
7+ const { once } = require ( 'events' )
8+ const finished = require ( 'util' ) . promisify ( require ( 'stream' ) . finished )
9+
10+ async function runAndKill ( file ) {
11+ if ( common . isWindows ) {
12+ common . printSkipMessage ( `signals are not supported in windows, skipping ${ file } ` )
13+ return
14+ }
15+ let stdout = ''
16+ const child = spawn ( process . execPath , [ '--test' , file ] )
17+ child . stdout . setEncoding ( 'utf8' )
18+ child . stdout . on ( 'data' , ( chunk ) => {
19+ if ( ! stdout . length ) child . kill ( 'SIGINT' )
20+ stdout += chunk
21+ } )
22+ const [ code , signal ] = await once ( child , 'exit' )
23+ await finished ( child . stdout )
24+ assert . match ( stdout , / n o t o k 1 / )
25+ assert . match ( stdout , / # c a n c e l l e d 1 \n / )
26+ assert . strictEqual ( signal , null )
27+ assert . strictEqual ( code , 1 )
28+ }
1129
1230if ( process . argv [ 2 ] === 'child' ) {
1331 const test = require ( '#node:test' )
@@ -21,12 +39,6 @@ if (process.argv[2] === 'child') {
2139 test ( 'failing test' , ( ) => {
2240 assert . strictEqual ( true , false )
2341 } )
24- } else if ( process . argv [ 3 ] === 'never_ends' ) {
25- assert . strictEqual ( process . argv [ 3 ] , 'never_ends' )
26- test ( 'never ending test' , ( ) => {
27- return setTimeout ( 100_000_000 )
28- } )
29- process . kill ( process . pid , 'SIGINT' )
3042 } else assert . fail ( 'unreachable' )
3143} else {
3244 let child = spawnSync ( process . execPath , [ __filename , 'child' , 'pass' ] )
@@ -41,14 +53,6 @@ if (process.argv[2] === 'child') {
4153 assert . strictEqual ( child . status , 1 )
4254 assert . strictEqual ( child . signal , null )
4355
44- child = spawnSync ( process . execPath , [ __filename , 'child' , 'never_ends' ] )
45- assert . strictEqual ( child . status , 1 )
46- assert . strictEqual ( child . signal , null )
47- if ( common . isWindows ) {
48- common . printSkipMessage ( 'signals are not supported in windows' )
49- } else {
50- const stdout = child . stdout . toString ( )
51- assert . match ( stdout , / n o t o k 1 - n e v e r e n d i n g t e s t / )
52- assert . match ( stdout , / # c a n c e l l e d 1 / )
53- }
56+ runAndKill ( fixtures . path ( 'test-runner' , 'never_ending_sync.js' ) ) . then ( common . mustCall ( ) )
57+ runAndKill ( fixtures . path ( 'test-runner' , 'never_ending_async.js' ) ) . then ( common . mustCall ( ) )
5458}
0 commit comments