Skip to content

Commit 58af085

Browse files
richardlautargos
authored andcommitted
test: refactor stdio handling in test-esm-cjs-main
Set encoding on the stderr/stdout streams instead of calling data.toString(). Don't assume the complete expected messages arrive in a single event. PR-URL: #25169 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com>
1 parent 99a5af6 commit 58af085

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

test/es-module/test-esm-cjs-main.js

+9-10
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,20 @@ const assert = require('assert');
88
const entry = fixtures.path('/es-modules/cjs.js');
99

1010
const child = spawn(process.execPath, ['--experimental-modules', entry]);
11-
let experimentalWarning = false;
12-
let validatedExecution = false;
11+
let stderr = '';
12+
child.stderr.setEncoding('utf8');
1313
child.stderr.on('data', (data) => {
14-
if (!experimentalWarning) {
15-
experimentalWarning = true;
16-
return;
17-
}
18-
throw new Error(data.toString());
14+
stderr += data;
1915
});
16+
let stdout = '';
17+
child.stdout.setEncoding('utf8');
2018
child.stdout.on('data', (data) => {
21-
assert.strictEqual(data.toString(), 'executed\n');
22-
validatedExecution = true;
19+
stdout += data;
2320
});
2421
child.on('close', common.mustCall((code, signal) => {
25-
assert.strictEqual(validatedExecution, true);
2622
assert.strictEqual(code, 0);
2723
assert.strictEqual(signal, null);
24+
assert.strictEqual(stdout, 'executed\n');
25+
assert.strictEqual(stderr, `(node:${child.pid}) ` +
26+
'ExperimentalWarning: The ESM module loader is experimental.\n');
2827
}));

0 commit comments

Comments
 (0)