Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

237
Views
Spying on stdout of stdio: inherit spawn process

Have a simple function I'm trying to unit test:

const {spawn} = require('child_process');

function exec (command, args) {
  return spawn(command, args, {
    stdio: `inherit`
  });
}

Since this is using inherit, echo.stdout.on will not work, though I would presume that with the stdio being shared with the parent process, and the parent process presumably being the running script, that I could just override process.stdout.write (or console.log), but this did not work (neither did overwriting global.process.stdout.write). How can I spy on write using the above exec with the following not working?


let str = '';
const {write} = process.stdout;
process.stdout.write = (s) => {
  str += s; // Does not run
};
const echo = exec('echo', ['hello', 'world']);
echo.on('exit', () => {
  process.stdout.write = write;
  throw (new Error('Result ' + str));
});
setTimeout(() => {
  echo.kill();
}, 1000);
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!