diff --git a/lib/internal/main/test_runner.js b/lib/internal/main/test_runner.js index cc6fb5b..3955d78 100644 --- a/lib/internal/main/test_runner.js +++ b/lib/internal/main/test_runner.js @@ -8,6 +8,8 @@ const { ArrayPrototypeSlice, ArrayPrototypeSort, Promise, + PromiseAll, + SafeArrayIterator, SafeSet } = require('#internal/per_context/primordials') const { @@ -15,6 +17,7 @@ const { } = require('#internal/bootstrap/pre_execution') const { spawn } = require('child_process') const { readdirSync, statSync } = require('fs') +const { finished } = require('#internal/streams/end-of-stream') const console = require('#internal/console/global') const { codes: { @@ -128,9 +131,10 @@ function runTestFile (path) { stderr += chunk }) - child.once('exit', (code, signal) => { + child.once('exit', async (code, signal) => { if (code !== 0 || signal !== null) { if (!err) { + await PromiseAll(new SafeArrayIterator([finished(child.stderr), finished(child.stdout)])) err = new ERR_TEST_FAILURE('test failed', kSubtestsFailed) err.exitCode = code err.signal = signal diff --git a/lib/internal/per_context/primordials.js b/lib/internal/per_context/primordials.js index 30ec71f..d11555a 100644 --- a/lib/internal/per_context/primordials.js +++ b/lib/internal/per_context/primordials.js @@ -28,7 +28,9 @@ exports.ObjectIsExtensible = obj => Object.isExtensible(obj) exports.ObjectPrototypeHasOwnProperty = (obj, property) => Object.prototype.hasOwnProperty.call(obj, property) exports.ReflectApply = (target, self, args) => Reflect.apply(target, self, args) exports.Promise = Promise +exports.PromiseAll = iterator => Promise.all(iterator) exports.PromiseResolve = val => Promise.resolve(val) +exports.SafeArrayIterator = class ArrayIterator {constructor (array) { this.array = array }[Symbol.iterator] () { return this.array.values() }} exports.SafeMap = Map exports.SafeSet = Set exports.SafeWeakMap = WeakMap diff --git a/lib/internal/streams/end-of-stream.js b/lib/internal/streams/end-of-stream.js new file mode 100644 index 0000000..e981861 --- /dev/null +++ b/lib/internal/streams/end-of-stream.js @@ -0,0 +1,22 @@ +'use strict' + +let finished +try { + ({ finished } = require('node:stream/promises')) +} catch { + // node:stream/promises is not available on Node.js 14.x + const { finished: eos } = require('node:stream') + finished = function finished (stream, opts) { + return new Promise((resolve, reject) => { + eos(stream, opts, (err) => { + if (err) { + reject(err) + } else { + resolve() + } + }) + }) + } +} + +module.exports = { finished }