@@ -164,11 +164,17 @@ describe('spawn', () => {
164164 } )
165165
166166 it ( 'should handle options with env' , async ( ) => {
167- const result = await spawn ( 'echo' , [ '$TEST_VAR' ] , {
168- env : { TEST_VAR : 'test-value' } ,
169- shell : true ,
170- } )
167+ // Test that custom env is passed to spawned process
168+ // Use node to read env var directly without shell expansion
169+ const result = await spawn (
170+ 'node' ,
171+ [ '-e' , 'console.log(process.env.TEST_VAR)' ] ,
172+ {
173+ env : { ...process . env , TEST_VAR : 'test-value' } ,
174+ } ,
175+ )
171176 expect ( result . code ) . toBe ( 0 )
177+ expect ( result . stdout ?. toString ( ) . trim ( ) ) . toBe ( 'test-value' )
172178 } )
173179
174180 it ( 'should handle stdio: pipe (default)' , async ( ) => {
@@ -211,20 +217,28 @@ describe('spawn', () => {
211217
212218 it ( 'should strip ANSI codes by default' , async ( ) => {
213219 // Test with a command that outputs ANSI codes
214- const result = await spawn ( 'echo' , [ '-e' , '\x1b[31mred\x1b[0m' ] , {
215- shell : true ,
216- } )
220+ const result = await spawn (
221+ 'node' ,
222+ [ '-e' , 'console.log("\\x1b[31mred\\x1b[0m")' ] ,
223+ { } ,
224+ )
217225 expect ( result . code ) . toBe ( 0 )
218- // ANSI codes should be stripped
226+ // ANSI codes should be stripped (default behavior)
219227 expect ( result . stdout ) . not . toContain ( '\x1b[31m' )
228+ expect ( result . stdout ) . toContain ( 'red' )
220229 } )
221230
222231 it ( 'should not strip ANSI codes when stripAnsi: false' , async ( ) => {
223- const result = await spawn ( 'echo' , [ '-e' , '\x1b[31mred\x1b[0m' ] , {
224- shell : true ,
225- stripAnsi : false ,
226- } )
232+ const result = await spawn (
233+ 'node' ,
234+ [ '-e' , 'console.log("\\x1b[31mred\\x1b[0m")' ] ,
235+ {
236+ stripAnsi : false ,
237+ } ,
238+ )
227239 expect ( result . code ) . toBe ( 0 )
240+ // ANSI codes should NOT be stripped
241+ expect ( result . stdout ) . toContain ( '\x1b[31m' )
228242 } )
229243
230244 it ( 'should handle readonly args array' , async ( ) => {
@@ -281,13 +295,6 @@ describe('spawn', () => {
281295 }
282296 } )
283297
284- it ( 'should handle shell option' , async ( ) => {
285- const result = await spawn ( 'echo' , [ '$HOME' ] , {
286- shell : true ,
287- } )
288- expect ( result . code ) . toBe ( 0 )
289- } )
290-
291298 it . skipIf ( process . platform === 'win32' ) (
292299 'should handle shell as string path' ,
293300 async ( ) => {
@@ -350,11 +357,20 @@ describe('spawn', () => {
350357 } )
351358
352359 it ( 'should handle options with env' , ( ) => {
353- const result = spawnSync ( 'echo' , [ '$TEST_VAR' ] , {
354- env : { TEST_VAR : 'test-value' } ,
355- shell : true ,
356- } )
360+ // Test that custom env is passed to spawned process
361+ const result = spawnSync (
362+ 'node' ,
363+ [ '-e' , 'console.log(process.env.TEST_VAR)' ] ,
364+ {
365+ env : { ...process . env , TEST_VAR : 'test-value' } ,
366+ } ,
367+ )
357368 expect ( result . status ) . toBe ( 0 )
369+ const output =
370+ typeof result . stdout === 'string'
371+ ? result . stdout . trim ( )
372+ : result . stdout ?. toString ( ) . trim ( )
373+ expect ( output ) . toBe ( 'test-value' )
358374 } )
359375
360376 it ( 'should handle stdioString: true (default)' , ( ) => {
@@ -374,18 +390,36 @@ describe('spawn', () => {
374390 } )
375391
376392 it ( 'should strip ANSI codes by default' , ( ) => {
377- const result = spawnSync ( 'echo' , [ '-e' , '\x1b[31mred\x1b[0m' ] , {
378- shell : true ,
379- } )
393+ const result = spawnSync (
394+ 'node' ,
395+ [ '-e' , 'console.log("\\x1b[31mred\\x1b[0m")' ] ,
396+ { } ,
397+ )
380398 expect ( result . status ) . toBe ( 0 )
399+ // ANSI codes should be stripped (default behavior)
400+ const output =
401+ typeof result . stdout === 'string'
402+ ? result . stdout
403+ : result . stdout ?. toString ( )
404+ expect ( output ) . not . toContain ( '\x1b[31m' )
405+ expect ( output ) . toContain ( 'red' )
381406 } )
382407
383408 it ( 'should not strip ANSI codes when stripAnsi: false' , ( ) => {
384- const result = spawnSync ( 'echo' , [ '-e' , '\x1b[31mred\x1b[0m' ] , {
385- shell : true ,
386- stripAnsi : false ,
387- } )
409+ const result = spawnSync (
410+ 'node' ,
411+ [ '-e' , 'console.log("\\x1b[31mred\\x1b[0m")' ] ,
412+ {
413+ stripAnsi : false ,
414+ } ,
415+ )
388416 expect ( result . status ) . toBe ( 0 )
417+ // ANSI codes should NOT be stripped
418+ const output =
419+ typeof result . stdout === 'string'
420+ ? result . stdout
421+ : result . stdout ?. toString ( )
422+ expect ( output ) . toContain ( '\x1b[31m' )
389423 } )
390424
391425 it ( 'should handle readonly args array' , ( ) => {
@@ -420,13 +454,6 @@ describe('spawn', () => {
420454 }
421455 } )
422456
423- it ( 'should handle shell option' , ( ) => {
424- const result = spawnSync ( 'echo' , [ '$HOME' ] , {
425- shell : true ,
426- } )
427- expect ( result . status ) . toBe ( 0 )
428- } )
429-
430457 it . skipIf ( process . platform === 'win32' ) (
431458 'should handle shell as string path' ,
432459 ( ) => {
0 commit comments