1- import { spawnSync } from 'node:child_process' ;
1+ import { spawn } from 'node:child_process' ;
22import { join } from 'node:path' ;
33import { describe , expect , test } from 'vitest' ;
44
@@ -9,38 +9,62 @@ const NODE_MAJOR_VERSION = parseInt(process.versions.node.split('.')[0], 10);
99// macOS emulated x64 in CI is very slow!
1010const timeout = process . env . CI && process . platform === 'darwin' ? 60000 : 20000 ;
1111
12- function runTest ( ...paths ) {
12+ async function runTest ( ...paths ) {
1313 console . time ( 'Test Run' ) ;
1414 const file = join ( ...paths ) ;
1515 const args = NODE_MAJOR_VERSION === 22 ? [ '--experimental-async-context-frame' , file ] : [ file ] ;
16- const result = spawnSync ( 'node' , args ) ;
17- const stdout = result . stdout ?. toString ( ) . split ( '\n' ) . filter ( line => line . trim ( ) !== '' ) ;
18- const stderr = result . stderr ?. toString ( ) . split ( '\n' ) . filter ( line => line . trim ( ) !== '' ) ;
19-
20- let trace ;
21- for ( const line of stdout ) {
22- try {
23- trace = JSON . parse ( line ) ;
24- break ;
25- } catch ( _ ) {
26- //
27- }
28- }
29-
30- console . timeEnd ( 'Test Run' ) ;
31- if ( stdout . length > 0 ) {
32- console . log ( 'stdout:' , stdout ) ;
33- }
34- if ( stderr . length > 0 ) {
35- console . log ( 'stderr:' , stderr ) ;
36- }
3716
38- return { status : result . status , stdout, trace } ;
17+ return new Promise ( ( resolve , reject ) => {
18+ const child = spawn ( 'node' , args , { stdio : [ 'ignore' , 'pipe' , 'pipe' ] } ) ;
19+
20+ let stdoutBuf = '' ;
21+ let stderrBuf = '' ;
22+
23+ child . stdout ?. on ( 'data' , chunk => {
24+ stdoutBuf += chunk . toString ( ) ;
25+ } ) ;
26+ child . stderr ?. on ( 'data' , chunk => {
27+ stderrBuf += chunk . toString ( ) ;
28+ } ) ;
29+
30+ child . on ( 'error' , err => reject ( err ) ) ;
31+
32+ child . on ( 'close' , code => {
33+ const stdout = stdoutBuf
34+ . split ( '\n' )
35+ . map ( line => line . trim ( ) )
36+ . filter ( line => line !== '' ) ;
37+ const stderr = stderrBuf
38+ . split ( '\n' )
39+ . map ( line => line . trim ( ) )
40+ . filter ( line => line !== '' ) ;
41+
42+ let trace ;
43+ for ( const line of stdout ) {
44+ try {
45+ trace = JSON . parse ( line ) ;
46+ break ;
47+ } catch ( _ ) {
48+ // ignore non-JSON lines
49+ }
50+ }
51+
52+ console . timeEnd ( 'Test Run' ) ;
53+ if ( stdout . length > 0 ) {
54+ console . log ( 'stdout:' , stdout ) ;
55+ }
56+ if ( stderr . length > 0 ) {
57+ console . log ( 'stderr:' , stderr ) ;
58+ }
59+
60+ resolve ( { status : code , stdout, trace } ) ;
61+ } ) ;
62+ } ) ;
3963}
4064
4165describe ( 'e2e Tests' , { timeout } , ( ) => {
42- test ( 'Capture stack trace from multiple threads' , ( ) => {
43- const result = runTest ( __dirname , 'stack-traces.js' )
66+ test ( 'Capture stack trace from multiple threads' , async ( ) => {
67+ const result = await runTest ( __dirname , 'stack-traces.js' )
4468
4569 expect ( result . status ) . toEqual ( 0 ) ;
4670
@@ -92,8 +116,8 @@ describe('e2e Tests', { timeout }, () => {
92116 } ) ) ;
93117 } ) ;
94118
95- test ( 'detect stalled thread' , ( ) => {
96- const result = runTest ( __dirname , 'stalled.js' ) ;
119+ test ( 'detect stalled thread' , async ( ) => {
120+ const result = await runTest ( __dirname , 'stalled.js' ) ;
97121
98122 expect ( result . status ) . toEqual ( 0 ) ;
99123
@@ -127,13 +151,13 @@ describe('e2e Tests', { timeout }, () => {
127151 } ) ) ;
128152 } ) ;
129153
130- test ( 'async storage state' , ( ctx ) => {
154+ test ( 'async storage state' , async ( ctx ) => {
131155 if ( NODE_MAJOR_VERSION < 22 ) {
132156 ctx . skip ( ) ;
133157 return ;
134158 }
135159
136- const result = runTest ( __dirname , 'async-storage.mjs' ) ;
160+ const result = await runTest ( __dirname , 'async-storage.mjs' ) ;
137161
138162 expect ( result . status ) . toEqual ( 0 ) ;
139163
@@ -164,8 +188,8 @@ describe('e2e Tests', { timeout }, () => {
164188 } ) ) ;
165189 } ) ;
166190
167- test ( 'can be disabled' , ( ) => {
168- const result = runTest ( __dirname , 'stalled-disabled.js' ) ;
191+ test ( 'can be disabled' , async ( ) => {
192+ const result = await runTest ( __dirname , 'stalled-disabled.js' ) ;
169193
170194 expect ( result . status ) . toEqual ( 0 ) ;
171195 expect ( result . stdout ) . toContain ( 'complete' ) ;
0 commit comments