Skip to content

Commit 728fad4

Browse files
.
1 parent 915eeb6 commit 728fad4

11 files changed

+136
-155
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,

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { execPath } from 'node:process';
66

77
import spawn from './helper.spawnAsPromised.mjs';
88

9+
910
const requiringCjsAsEsm = path.resolve(fixtures.path('/es-modules/cjs-esm.js'));
1011
const requiringEsm = path.resolve(fixtures.path('/es-modules/cjs-esm-esm.js'));
1112
const pjson = path.resolve(

test/es-module/test-esm-encoded-path-native.js

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
spawn(execPath, [fixtures.path('es-module-url/native.mjs')])
10+
.then(mustCall(({ code }) => {
11+
assert.strictEqual(code, 1);
12+
}));
Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,39 @@
11
import { mustCall } from '../common/index.mjs';
2-
import { path } from '../common/fixtures.mjs';
3-
import { match, notStrictEqual } from 'assert';
4-
import { spawn } from 'child_process';
5-
import { execPath } from 'process';
2+
import * as fixtures from '../common/fixtures.mjs';
3+
import assert from 'node:assert';
4+
import { execPath } from 'node:process';
65

7-
const importStatement =
8-
'import { foo, notfound } from \'./module-named-exports.mjs\';';
6+
import spawn from './helper.spawnAsPromised.mjs';
7+
8+
const importStatement = 'import { foo, notfound } from \'./module-named-exports.mjs\';';
99
const importStatementMultiline = `import {
1010
foo,
1111
notfound
1212
} from './module-named-exports.mjs';
1313
`;
1414

15-
[importStatement, importStatementMultiline].forEach((input) => {
16-
const child = spawn(execPath, [
15+
for (
16+
const input
17+
of [
18+
importStatement,
19+
importStatementMultiline,
20+
]
21+
) {
22+
spawn(execPath, [
1723
'--input-type=module',
1824
'--eval',
1925
input,
2026
], {
21-
cwd: path('es-module-loaders'),
22-
});
23-
24-
let stderr = '';
25-
child.stderr.setEncoding('utf8');
26-
child.stderr.on('data', (data) => {
27-
stderr += data;
28-
});
29-
child.on('close', mustCall((code, _signal) => {
30-
notStrictEqual(code, 0);
27+
cwd: fixtures.path('es-module-loaders'),
28+
})
29+
.then(mustCall(({ code, stderr, stdout }) => {
30+
assert.notStrictEqual(code, 0);
3131

32-
// SyntaxError: The requested module './module-named-exports.mjs'
33-
// does not provide an export named 'notfound'
34-
match(stderr, /SyntaxError:/);
35-
// The quotes ensure that the path starts with ./ and not ../
36-
match(stderr, /'\.\/module-named-exports\.mjs'/);
37-
match(stderr, /notfound/);
38-
}));
39-
});
32+
// SyntaxError: The requested module './module-named-exports.mjs'
33+
// does not provide an export named 'notfound'
34+
assert.match(stderr, /SyntaxError:/);
35+
// The quotes ensure that the path starts with ./ and not ../
36+
assert.match(stderr, /'\.\/module-named-exports\.mjs'/);
37+
assert.match(stderr, /notfound/);
38+
}));
39+
}
Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,20 @@
11
import { mustCall } from '../common/index.mjs';
2-
import { path } from '../common/fixtures.mjs';
3-
import { match, notStrictEqual } from 'assert';
4-
import { spawn } from 'child_process';
5-
import { execPath } from 'process';
2+
import * as fixtures from '../common/fixtures.mjs';
3+
import assert from 'node:assert';
4+
import { execPath } from 'node:process';
65

7-
const child = spawn(execPath, [
8-
path('es-modules', 'import-json-named-export.mjs'),
9-
]);
6+
import spawn from './helper.spawnAsPromised.mjs';
107

11-
let stderr = '';
12-
child.stderr.setEncoding('utf8');
13-
child.stderr.on('data', (data) => {
14-
stderr += data;
15-
});
16-
child.on('close', mustCall((code, _signal) => {
17-
notStrictEqual(code, 0);
188

19-
// SyntaxError: The requested module '../experimental.json'
20-
// does not provide an export named 'ofLife'
21-
match(stderr, /SyntaxError:/);
22-
match(stderr, /'\.\.\/experimental\.json'/);
23-
match(stderr, /'ofLife'/);
24-
}));
9+
spawn(execPath, [
10+
fixtures.path('es-modules', 'import-json-named-export.mjs'),
11+
])
12+
.then(mustCall(({ code, stderr }) => {
13+
// SyntaxError: The requested module '../experimental.json'
14+
// does not provide an export named 'ofLife'
15+
assert.match(stderr, /SyntaxError:/);
16+
assert.match(stderr, /'\.\.\/experimental\.json'/);
17+
assert.match(stderr, /'ofLife'/);
18+
19+
assert.notStrictEqual(code, 0);
20+
}));

test/es-module/test-esm-invalid-pjson.js

Lines changed: 0 additions & 27 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 { checkoutEOL, 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 entry = fixtures.path('/es-modules/import-invalid-pjson.mjs');
10+
const invalidJson = fixtures.path('/node_modules/invalid-pjson/package.json');
11+
12+
spawn(execPath, [entry])
13+
.then(mustCall(({ code, signal, stderr }) => {
14+
assert.ok(
15+
stderr.includes(
16+
`[ERR_INVALID_PACKAGE_CONFIG]: Invalid package config ${invalidJson} ` +
17+
`while importing "invalid-pjson" from ${entry}. ` +
18+
`Unexpected token } in JSON at position ${12 + checkoutEOL.length * 2}`
19+
),
20+
stderr
21+
);
22+
assert.strictEqual(code, 1);
23+
assert.strictEqual(signal, null);
24+
}));

test/es-module/test-esm-json.mjs

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,23 @@
1-
import '../common/index.mjs';
2-
import { path } from '../common/fixtures.mjs';
3-
import { strictEqual, ok } from 'assert';
4-
import { spawn } from 'child_process';
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';
55

66
import secret from '../fixtures/experimental.json' assert { type: 'json' };
7+
import spawn from './helper.spawnAsPromised.mjs';
78

8-
strictEqual(secret.ofLife, 42);
99

10-
// Test warning message
11-
const child = spawn(process.execPath, [
12-
path('/es-modules/json-modules.mjs'),
13-
]);
10+
assert.strictEqual(secret.ofLife, 42);
1411

15-
let stderr = '';
16-
child.stderr.setEncoding('utf8');
17-
child.stderr.on('data', (data) => {
18-
stderr += data;
19-
});
20-
child.on('close', (code, signal) => {
21-
strictEqual(code, 0);
22-
strictEqual(signal, null);
23-
ok(stderr.toString().includes(
24-
'ExperimentalWarning: Importing JSON modules is an experimental feature. ' +
25-
'This feature could change at any time'
26-
));
27-
});
12+
// Test warning message
13+
spawn(execPath, [
14+
fixtures.path('/es-modules/json-modules.mjs'),
15+
])
16+
.then(mustCall(({ code, signal, stderr }) => {
17+
assert.ok(stderr.includes(
18+
'ExperimentalWarning: Importing JSON modules is an experimental feature. ' +
19+
'This feature could change at any time'
20+
));
21+
assert.strictEqual(code, 0);
22+
assert.strictEqual(signal, null);
23+
}));
Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { mustCall } from '../common/index.mjs';
22
import fixtures from '../common/fixtures.js';
3-
import { strictEqual } from 'node:assert';
4-
import { spawn } from 'node:child_process';
3+
import assert from 'node:assert';
54
import http from 'node:http';
65
import path from 'node:path';
6+
import { execPath } from 'node:process';
77
import { promisify } from 'node:util';
88

9+
import spawn from './helper.spawnAsPromised.mjs';
10+
911

1012
const files = {
1113
'main.mjs': 'export * from "./lib.mjs";',
@@ -40,33 +42,23 @@ const {
4042
port,
4143
} = server.address();
4244

43-
{ // Verify nested HTTP imports work
44-
const child = spawn( // ! `spawn` MUST be used (vs `spawnSync`) to avoid blocking the event loop
45-
process.execPath,
46-
[
47-
'--no-warnings',
48-
'--loader',
49-
fixtures.fileURL('es-module-loaders', 'http-loader.mjs'),
50-
'--input-type=module',
51-
'--eval',
52-
`import * as main from 'http://${host}:${port}/main.mjs'; console.log(main)`,
53-
]
54-
);
55-
56-
let stderr = '';
57-
let stdout = '';
58-
59-
child.stderr.setEncoding('utf8');
60-
child.stderr.on('data', (data) => stderr += data);
61-
child.stdout.setEncoding('utf8');
62-
child.stdout.on('data', (data) => stdout += data);
63-
64-
child.on('close', mustCall((code, signal) => {
65-
strictEqual(stderr, '');
66-
strictEqual(stdout, '[Module: null prototype] { sum: [Function: sum] }\n');
67-
strictEqual(code, 0);
68-
strictEqual(signal, null);
45+
// Verify nested HTTP imports work
46+
spawn( // ! `spawn` MUST be used (vs `spawnSync`) to avoid blocking the event loop
47+
execPath,
48+
[
49+
'--no-warnings',
50+
'--loader',
51+
fixtures.fileURL('es-module-loaders', 'http-loader.mjs'),
52+
'--input-type=module',
53+
'--eval',
54+
`import * as main from 'http://${host}:${port}/main.mjs'; console.log(main)`,
55+
]
56+
)
57+
.then(mustCall(({ code, signal, stderr, stdout }) => {
58+
assert.strictEqual(stderr, '');
59+
assert.strictEqual(stdout, '[Module: null prototype] { sum: [Function: sum] }\n');
60+
assert.strictEqual(code, 0);
61+
assert.strictEqual(signal, null);
6962

7063
server.close();
7164
}));
72-
}

0 commit comments

Comments
 (0)