Skip to content

Commit 8dd3924

Browse files
squash me: extract spawnAsPromised into helper & apply to more tests
1 parent c8ff2ce commit 8dd3924

File tree

5 files changed

+140
-146
lines changed

5 files changed

+140
-146
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import cp from 'node:child_process';
2+
3+
4+
export default function spawn(...args) {
5+
let stderr = '';
6+
let stdout = '';
7+
8+
const child = cp.spawn(...args);
9+
child.stderr.setEncoding('utf8');
10+
child.stderr.on('data', (data) => { stderr += data; });
11+
child.stdout.setEncoding('utf8');
12+
child.stdout.on('data', (data) => { stdout += data; });
13+
14+
return new Promise((resolve, reject) => {
15+
child.on('close', (code, signal) => {
16+
resolve({
17+
code,
18+
signal,
19+
stderr,
20+
stdout,
21+
});
22+
});
23+
child.on('error', (code, signal) => {
24+
reject({
25+
code,
26+
signal,
27+
stderr,
28+
stdout,
29+
});
30+
});
31+
});
32+
}

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

Lines changed: 0 additions & 64 deletions
This file was deleted.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { mustCall } from '../common/index.mjs';
2+
import * as fixtures from '../common/fixtures.mjs';
3+
import assert from 'node:assert';
4+
import path from 'node:path';
5+
6+
import spawn from './helper.spawnAsPromised.mjs';
7+
8+
const requiringCjsAsEsm = path.resolve(fixtures.path('/es-modules/cjs-esm.js'));
9+
const requiringEsm = path.resolve(fixtures.path('/es-modules/cjs-esm-esm.js'));
10+
const pjson = path.resolve(
11+
fixtures.path('/es-modules/package-type-module/package.json')
12+
);
13+
14+
{
15+
const required = path.resolve(
16+
fixtures.path('/es-modules/package-type-module/cjs.js')
17+
);
18+
const basename = 'cjs.js';
19+
spawn(process.execPath, [requiringCjsAsEsm])
20+
.then(mustCall(({ code, signal, stderr }) => {
21+
assert.ok(
22+
stderr.replaceAll('\r', '').includes(
23+
`Error [ERR_REQUIRE_ESM]: require() of ES Module ${required} from ${requiringCjsAsEsm} not supported.\n`
24+
)
25+
);
26+
assert.ok(
27+
stderr.replaceAll('\r', '').includes(
28+
`Instead rename ${basename} to end in .cjs, change the requiring ` +
29+
'code to use dynamic import() which is available in all CommonJS ' +
30+
`modules, or change "type": "module" to "type": "commonjs" in ${pjson} to ` +
31+
'treat all .js files as CommonJS (using .mjs for all ES modules ' +
32+
'instead).\n'
33+
)
34+
);
35+
36+
assert.strictEqual(code, 1);
37+
assert.strictEqual(signal, null);
38+
}));
39+
}
40+
41+
{
42+
const required = path.resolve(
43+
fixtures.path('/es-modules/package-type-module/esm.js')
44+
);
45+
const basename = 'esm.js';
46+
spawn(process.execPath, [requiringEsm])
47+
.then(mustCall(({ code, signal, stderr }) => {
48+
assert.ok(
49+
stderr.replace(/\r/g, '').includes(
50+
`Error [ERR_REQUIRE_ESM]: require() of ES Module ${required} from ${requiringEsm} not supported.\n`
51+
)
52+
);
53+
assert.ok(
54+
stderr.replace(/\r/g, '').includes(
55+
`Instead change the require of ${basename} in ${requiringEsm} to` +
56+
' a dynamic import() which is available in all CommonJS modules.\n'
57+
)
58+
);
59+
60+
assert.strictEqual(code, 1);
61+
assert.strictEqual(signal, null);
62+
}));
63+
}
Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,26 @@
11
import { mustCall } from '../common/index.mjs';
22
import { fileURL } from '../common/fixtures.mjs';
33
import { doesNotMatch, match, strictEqual } from 'assert';
4-
import { spawn } from 'child_process';
5-
import { execPath } from 'process';
4+
import { execPath } from 'node:process';
65

7-
// Verify no warnings are printed when no experimental features are enabled or used
8-
{
9-
const input = `import ${JSON.stringify(fileURL('es-module-loaders', 'module-named-exports.mjs'))}`;
10-
const child = spawn(execPath, [
11-
'--input-type=module',
12-
'--eval',
13-
input,
14-
]);
6+
import spawn from './helper.spawnAsPromised.mjs';
157

16-
let stderr = '';
17-
child.stderr.setEncoding('utf8');
18-
child.stderr.on('data', (data) => { stderr += data; });
19-
child.on('close', mustCall((code, signal) => {
20-
strictEqual(code, 0);
21-
strictEqual(signal, null);
8+
9+
// Verify no warnings are printed when no experimental features are enabled or used
10+
spawn(execPath, [
11+
'--input-type=module',
12+
'--eval',
13+
`import ${JSON.stringify(fileURL('es-module-loaders', 'module-named-exports.mjs'))}`,
14+
])
15+
.then(mustCall(({ code, signal, stderr }) => {
2216
doesNotMatch(
2317
stderr,
2418
/ExperimentalWarning/,
2519
new Error('No experimental warning(s) should be emitted when no experimental feature is enabled')
2620
);
21+
strictEqual(code, 0);
22+
strictEqual(signal, null);
2723
}));
28-
}
2924

3025
// Verify experimental warning is printed when experimental feature is enabled
3126
for (
@@ -35,21 +30,16 @@ for (
3530
[/specifier resolution/, '--experimental-specifier-resolution=node'],
3631
]
3732
) {
38-
const input = `import ${JSON.stringify(fileURL('es-module-loaders', 'module-named-exports.mjs'))}`;
39-
const child = spawn(execPath, [
33+
spawn(execPath, [
4034
arg,
4135
'--input-type=module',
4236
'--eval',
43-
input,
44-
]);
45-
46-
let stderr = '';
47-
child.stderr.setEncoding('utf8');
48-
child.stderr.on('data', (data) => { stderr += data; });
49-
child.on('close', mustCall((code, signal) => {
50-
strictEqual(code, 0);
51-
strictEqual(signal, null);
52-
match(stderr, /ExperimentalWarning/);
53-
match(stderr, experiment);
54-
}));
37+
`import ${JSON.stringify(fileURL('es-module-loaders', 'module-named-exports.mjs'))}`,
38+
])
39+
.then(mustCall(({ code, signal, stderr }) => {
40+
match(stderr, /ExperimentalWarning/);
41+
match(stderr, experiment);
42+
strictEqual(code, 0);
43+
strictEqual(signal, null);
44+
}));
5545
}

0 commit comments

Comments
 (0)