Navigation Menu

Skip to content

Commit

Permalink
test: refactor stdio handling in test-esm-cjs-main
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
richardlau authored and targos committed Jan 1, 2019
1 parent 99a5af6 commit 58af085
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions test/es-module/test-esm-cjs-main.js
Expand Up @@ -8,21 +8,20 @@ const assert = require('assert');
const entry = fixtures.path('/es-modules/cjs.js'); const entry = fixtures.path('/es-modules/cjs.js');


const child = spawn(process.execPath, ['--experimental-modules', entry]); const child = spawn(process.execPath, ['--experimental-modules', entry]);
let experimentalWarning = false; let stderr = '';
let validatedExecution = false; child.stderr.setEncoding('utf8');
child.stderr.on('data', (data) => { child.stderr.on('data', (data) => {
if (!experimentalWarning) { stderr += data;
experimentalWarning = true;
return;
}
throw new Error(data.toString());
}); });
let stdout = '';
child.stdout.setEncoding('utf8');
child.stdout.on('data', (data) => { child.stdout.on('data', (data) => {
assert.strictEqual(data.toString(), 'executed\n'); stdout += data;
validatedExecution = true;
}); });
child.on('close', common.mustCall((code, signal) => { child.on('close', common.mustCall((code, signal) => {
assert.strictEqual(validatedExecution, true);
assert.strictEqual(code, 0); assert.strictEqual(code, 0);
assert.strictEqual(signal, null); assert.strictEqual(signal, null);
assert.strictEqual(stdout, 'executed\n');
assert.strictEqual(stderr, `(node:${child.pid}) ` +
'ExperimentalWarning: The ESM module loader is experimental.\n');
})); }));

0 comments on commit 58af085

Please sign in to comment.