Skip to content

stdout and stderr not inherited when runtime: 'child_process' #103

@AriPerkkio

Description

@AriPerkkio

Vitest's reporters go crazy when we cannot control how to print worker's process.stdout and process.stderr in main thread. These can be for example NodeJS deprecation warnings.

// main.mjs
import { URL } from 'node:url'
import Tinypool from 'tinypool'

const write = process.stdout.write.bind(process.stdout)

process.stdout.write = (data) => {
  write(`Captured stdout: ${data}\n`)
}

process.stderr.write = (data) => {
  write(`Captured stderr: ${data}\n`)
}

const pool = new Tinypool({
  runtime: 'child_process',
  filename: new URL('./worker.mjs', import.meta.url).href,
})

await pool.run()

// worker.mjs
export default function run() {
  process.stdout.write('Message')
  process.stderr.write('Error message')
}
$ node main.mjs 
Message
Error message

With runtime: 'worker_threads':

$ node main.mjs 
Captured stdout: Message

Captured stderr: Error message

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions