Skip to content

Commit 2839aaf

Browse files
MoLowaduh95
authored andcommitted
fix: wait for stderr and stdout to complete
PR-URL: nodejs/node#43666 Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
1 parent 56d86fa commit 2839aaf

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

lib/internal/main/test_runner.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@ const {
88
ArrayPrototypeSlice,
99
ArrayPrototypeSort,
1010
Promise,
11+
PromiseAll,
12+
SafeArrayIterator,
1113
SafeSet
1214
} = require('#internal/per_context/primordials')
1315
const {
1416
prepareMainThreadExecution
1517
} = require('#internal/bootstrap/pre_execution')
1618
const { spawn } = require('child_process')
1719
const { readdirSync, statSync } = require('fs')
20+
const { finished } = require('#internal/streams/end-of-stream')
1821
const console = require('#internal/console/global')
1922
const {
2023
codes: {
@@ -128,9 +131,10 @@ function runTestFile (path) {
128131
stderr += chunk
129132
})
130133

131-
child.once('exit', (code, signal) => {
134+
child.once('exit', async (code, signal) => {
132135
if (code !== 0 || signal !== null) {
133136
if (!err) {
137+
await PromiseAll(new SafeArrayIterator([finished(child.stderr), finished(child.stdout)]))
134138
err = new ERR_TEST_FAILURE('test failed', kSubtestsFailed)
135139
err.exitCode = code
136140
err.signal = signal

lib/internal/per_context/primordials.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ exports.ObjectIsExtensible = obj => Object.isExtensible(obj)
2828
exports.ObjectPrototypeHasOwnProperty = (obj, property) => Object.prototype.hasOwnProperty.call(obj, property)
2929
exports.ReflectApply = (target, self, args) => Reflect.apply(target, self, args)
3030
exports.Promise = Promise
31+
exports.PromiseAll = iterator => Promise.all(iterator)
3132
exports.PromiseResolve = val => Promise.resolve(val)
33+
exports.SafeArrayIterator = class ArrayIterator {constructor (array) { this.array = array }[Symbol.iterator] () { return this.array.values() }}
3234
exports.SafeMap = Map
3335
exports.SafeSet = Set
3436
exports.SafeWeakMap = WeakMap

lib/internal/streams/end-of-stream.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict'
2+
3+
let finished
4+
try {
5+
({ finished } = require('node:stream/promises'))
6+
} catch {
7+
// node:stream/promises is not available on Node.js 14.x
8+
const { finished: eos } = require('node:stream')
9+
finished = function finished (stream, opts) {
10+
return new Promise((resolve, reject) => {
11+
eos(stream, opts, (err) => {
12+
if (err) {
13+
reject(err)
14+
} else {
15+
resolve()
16+
}
17+
})
18+
})
19+
}
20+
}
21+
22+
module.exports = { finished }

0 commit comments

Comments
 (0)