Skip to content

Commit 03d1f1d

Browse files
esm: tidy tests
1 parent dab492f commit 03d1f1d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+852
-959
lines changed

test/common/index.mjs

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

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

Lines changed: 0 additions & 21 deletions
This file was deleted.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { mustCall } from '../common/index.mjs';
2+
import * as fixtures from '../common/fixtures.mjs';
3+
import assert from 'node:assert';
4+
import { execPath } from 'node:process';
5+
6+
import spawn from './helper.spawnAsPromised.mjs';
7+
8+
const entry = fixtures.path('/es-modules/builtin-imports-case.mjs');
9+
10+
spawn(execPath, [entry])
11+
.then(mustCall(({ code, signal, stdout }) => {
12+
assert.strictEqual(code, 0);
13+
assert.strictEqual(signal, null);
14+
assert.strictEqual(stdout, 'ok\n');
15+
}));

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

Lines changed: 0 additions & 35 deletions
This file was deleted.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { mustCall } from '../common/index.mjs';
2+
import * as fixtures from '../common/fixtures.mjs';
3+
import assert from 'node:assert';
4+
import { execPath } from 'node:process';
5+
6+
import spawn from './helper.spawnAsPromised.mjs';
7+
8+
9+
const validEntry = fixtures.path('/es-modules/cjs-exports.mjs');
10+
spawn(execPath, [validEntry])
11+
.then(mustCall(({ code, signal, stdout }) => {
12+
assert.strictEqual(code, 0);
13+
assert.strictEqual(signal, null);
14+
assert.strictEqual(stdout, 'ok\n');
15+
}));
16+
17+
const invalidEntry = fixtures.path('/es-modules/cjs-exports-invalid.mjs');
18+
spawn(execPath, [invalidEntry])
19+
.then(mustCall(({ code, signal, stderr }) => {
20+
assert.strictEqual(code, 1);
21+
assert.strictEqual(signal, null);
22+
assert.ok(stderr.includes('Warning: To load an ES module'));
23+
assert.ok(stderr.includes('Unexpected token \'export\''));
24+
}));

0 commit comments

Comments
 (0)