Skip to content

Commit e0a8e2c

Browse files
.
1 parent 67326c9 commit e0a8e2c

12 files changed

+399
-389
lines changed
Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
1-
import { mustCall } from '../common/index.mjs';
1+
import '../common/index.mjs';
22
import { fileURL, path } from '../common/fixtures.mjs';
3-
import { match, notStrictEqual } from 'assert';
4-
import { spawn } from 'child_process';
5-
import { execPath } from 'process';
3+
import { match, notStrictEqual } from 'node:assert';
4+
import { execPath } from 'node:process';
5+
import { describe, it } from 'node:test';
66

7-
const child = spawn(execPath, [
8-
'--no-warnings',
9-
'--throw-deprecation',
10-
'--experimental-loader',
11-
fileURL('es-module-loaders', 'hooks-obsolete.mjs').href,
12-
path('print-error-message.js'),
13-
]);
7+
import spawn from './helper.spawnAsPromised.mjs';
148

15-
let stderr = '';
16-
child.stderr.setEncoding('utf8');
17-
child.stderr.on('data', (data) => {
18-
stderr += data;
19-
});
20-
child.on('close', mustCall((code, _signal) => {
21-
notStrictEqual(code, 0);
229

23-
// DeprecationWarning: Obsolete loader hook(s) supplied and will be ignored:
24-
// dynamicInstantiate, getFormat, getSource, transformSource
25-
match(stderr, /DeprecationWarning:/);
26-
match(stderr, /dynamicInstantiate/);
27-
match(stderr, /getFormat/);
28-
match(stderr, /getSource/);
29-
match(stderr, /transformSource/);
30-
}));
10+
describe('ESM: deprecation warnings for obsolete hooks', { concurrency: true }, () => {
11+
it(async () => {
12+
const { code, stderr } = await spawn(execPath, [
13+
'--no-warnings',
14+
'--throw-deprecation',
15+
'--experimental-loader',
16+
fileURL('es-module-loaders', 'hooks-obsolete.mjs').href,
17+
path('print-error-message.js'),
18+
]);
19+
20+
// DeprecationWarning: Obsolete loader hook(s) supplied and will be ignored:
21+
// dynamicInstantiate, getFormat, getSource, transformSource
22+
match(stderr, /DeprecationWarning:/);
23+
match(stderr, /dynamicInstantiate/);
24+
match(stderr, /getFormat/);
25+
match(stderr, /getSource/);
26+
match(stderr, /transformSource/);
27+
28+
notStrictEqual(code, 0);
29+
});
30+
});
Lines changed: 28 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,45 @@
11
import { mustCall } from '../common/index.mjs';
22
import { fileURL, path } from '../common/fixtures.mjs';
33
import { match, ok, notStrictEqual, strictEqual } from 'assert';
4-
import { spawn } from 'child_process';
5-
import { execPath } from 'process';
4+
import { execPath } from 'node:process';
5+
import { describe, it } from 'node:test';
66

7-
{
8-
const child = spawn(execPath, [
9-
'--experimental-loader',
10-
fileURL('es-module-loaders', 'thenable-load-hook.mjs').href,
11-
path('es-modules', 'test-esm-ok.mjs'),
12-
]);
7+
import spawn from './helper.spawnAsPromised.mjs';
8+
9+
10+
describe('ESM: thenable loader hooks', { concurrency: true }, () => {
11+
it('should behave as a normal promise resolution', async () => {
12+
const { code, stderr} = await spawn(execPath, [
13+
'--experimental-loader',
14+
fileURL('es-module-loaders', 'thenable-load-hook.mjs').href,
15+
path('es-modules', 'test-esm-ok.mjs'),
16+
]);
1317

14-
let stderr = '';
15-
child.stderr.setEncoding('utf8');
16-
child.stderr.on('data', (data) => {
17-
stderr += data;
18-
});
19-
child.on('close', mustCall((code, _signal) => {
2018
strictEqual(code, 0);
2119
ok(!stderr.includes('must not call'));
22-
}));
23-
}
20+
});
2421

25-
{
26-
const child = spawn(execPath, [
27-
'--experimental-loader',
28-
fileURL('es-module-loaders', 'thenable-load-hook-rejected.mjs').href,
29-
path('es-modules', 'test-esm-ok.mjs'),
30-
]);
22+
it('should crash the node process rejection with an error', async () => {
23+
const { code, stderr } = await spawn(execPath, [
24+
'--experimental-loader',
25+
fileURL('es-module-loaders', 'thenable-load-hook-rejected.mjs').href,
26+
path('es-modules', 'test-esm-ok.mjs'),
27+
]);
3128

32-
let stderr = '';
33-
child.stderr.setEncoding('utf8');
34-
child.stderr.on('data', (data) => {
35-
stderr += data;
36-
});
37-
child.on('close', mustCall((code, _signal) => {
3829
notStrictEqual(code, 0);
39-
4030
match(stderr, /\sError: must crash the process\r?\n/);
41-
4231
ok(!stderr.includes('must not call'));
43-
}));
44-
}
32+
});
4533

46-
{
47-
const child = spawn(execPath, [
48-
'--experimental-loader',
49-
fileURL('es-module-loaders', 'thenable-load-hook-rejected-no-arguments.mjs').href,
50-
path('es-modules', 'test-esm-ok.mjs'),
51-
]);
34+
it('should just reject without an error (but NOT crash the node process)', async () => {
35+
const { code, stderr } = await spawn(execPath, [
36+
'--experimental-loader',
37+
fileURL('es-module-loaders', 'thenable-load-hook-rejected-no-arguments.mjs').href,
38+
path('es-modules', 'test-esm-ok.mjs'),
39+
]);
5240

53-
let stderr = '';
54-
child.stderr.setEncoding('utf8');
55-
child.stderr.on('data', (data) => {
56-
stderr += data;
57-
});
58-
child.on('close', mustCall((code, _signal) => {
5941
notStrictEqual(code, 0);
60-
6142
match(stderr, /\sundefined\r?\n/);
62-
6343
ok(!stderr.includes('must not call'));
64-
}));
65-
}
44+
});
45+
});
Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
1-
import { mustCall } from '../common/index.mjs';
1+
import '../common/index.mjs';
22
import { fileURL, path } from '../common/fixtures.mjs';
3-
import { match, ok, notStrictEqual } from 'assert';
4-
import { spawn } from 'child_process';
5-
import { execPath } from 'process';
3+
import { match, ok, notStrictEqual } from 'node:assert';
4+
import { execPath } from 'node:process';
5+
import { describe, it } from 'node:test';
66

7-
const child = spawn(execPath, [
8-
'--experimental-loader',
9-
fileURL('es-module-loaders', 'syntax-error.mjs').href,
10-
path('print-error-message.js'),
11-
]);
7+
import spawn from './helper.spawnAsPromised.mjs';
128

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

21-
match(stderr, /SyntaxError:/);
10+
describe('ESM: loader with syntax error', { concurrency: true }, () => {
11+
it('should crash the node process', async () => {
12+
const { code, stderr, } = await spawn(execPath, [
13+
'--experimental-loader',
14+
fileURL('es-module-loaders', 'syntax-error.mjs').href,
15+
path('print-error-message.js'),
16+
]);
2217

23-
ok(!stderr.includes('Bad command or file name'));
24-
}));
18+
match(stderr, /SyntaxError:/);
19+
ok(!stderr.includes('Bad command or file name'));
20+
notStrictEqual(code, 0);
21+
});
22+
});
Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,36 @@
1-
import { mustCall } from '../common/index.mjs';
1+
import '../common/index.mjs';
22
import { fixturesDir } from '../common/fixtures.mjs';
3-
import { match, notStrictEqual } from 'assert';
4-
import { spawn } from 'child_process';
5-
import { execPath } from 'process';
3+
import { match, notStrictEqual } from 'node:assert';
4+
import { execPath } from 'node:process';
5+
import { describe, it } from 'node:test';
66

7-
[
8-
{
9-
input: 'import "./print-error-message"',
10-
// Did you mean to import ../print-error-message.js?
11-
expected: / \.\.\/print-error-message\.js\?/,
12-
},
13-
{
14-
input: 'import obj from "some_module/obj"',
15-
expected: / some_module\/obj\.js\?/,
16-
},
17-
].forEach(({ input, expected }) => {
18-
const child = spawn(execPath, [
19-
'--input-type=module',
20-
'--eval',
21-
input,
22-
], {
23-
cwd: fixturesDir,
24-
});
7+
import spawn from './helper.spawnAsPromised.mjs';
8+
9+
10+
describe('ESM: module not found hint', { concurrency: true }, () => {
11+
for (
12+
const { input, expected }
13+
of [
14+
{
15+
input: 'import "./print-error-message"',
16+
// Did you mean to import ../print-error-message.js?
17+
expected: / \.\.\/print-error-message\.js\?/,
18+
},
19+
{
20+
input: 'import obj from "some_module/obj"',
21+
expected: / some_module\/obj\.js\?/,
22+
},
23+
]
24+
) it('should cite a variant form', async () => {
25+
const { code, stderr } = await spawn(execPath, [
26+
'--input-type=module',
27+
'--eval',
28+
input,
29+
], {
30+
cwd: fixturesDir,
31+
});
2532

26-
let stderr = '';
27-
child.stderr.setEncoding('utf8');
28-
child.stderr.on('data', (data) => {
29-
stderr += data;
30-
});
31-
child.on('close', mustCall((code, _signal) => {
32-
notStrictEqual(code, 0);
3333
match(stderr, expected);
34-
}));
34+
notStrictEqual(code, 0);
35+
});
3536
});

test/es-module/test-esm-non-js.mjs

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
1-
import { mustCall } from '../common/index.mjs';
1+
import '../common/index.mjs';
22
import { fileURL } from '../common/fixtures.mjs';
3-
import { match, strictEqual } from 'assert';
4-
import { spawn } from 'child_process';
5-
import { execPath } from 'process';
3+
import { match, strictEqual } from 'node:assert';
4+
import { execPath } from 'node:process';
5+
import { describe, it } from 'node:test';
66

7-
// Verify non-js extensions fail for ESM
8-
const child = spawn(execPath, [
9-
'--input-type=module',
10-
'--eval',
11-
`import ${JSON.stringify(fileURL('es-modules', 'file.unknown'))}`,
12-
]);
7+
import spawn from './helper.spawnAsPromised.mjs';
138

14-
let stderr = '';
15-
child.stderr.setEncoding('utf8');
16-
child.stderr.on('data', (data) => {
17-
stderr += data;
9+
describe('ESM: non-js extensions fail', { concurrency: true}, () => {
10+
it(async () => {
11+
const { code, stderr, signal } = await spawn(execPath, [
12+
'--input-type=module',
13+
'--eval',
14+
`import ${JSON.stringify(fileURL('es-modules', 'file.unknown'))}`,
15+
]);
16+
17+
match(stderr, /ERR_UNKNOWN_FILE_EXTENSION/);
18+
strictEqual(code, 1);
19+
strictEqual(signal, null);
20+
});
1821
});
19-
child.on('close', mustCall((code, signal) => {
20-
strictEqual(code, 1);
21-
strictEqual(signal, null);
22-
match(stderr, /ERR_UNKNOWN_FILE_EXTENSION/);
23-
}));
22+
Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,21 @@
11
import '../common/index.mjs';
22
import { path } from '../common/fixtures.mjs';
3-
import { strictEqual, ok } from 'assert';
4-
import { spawn } from 'child_process';
3+
import { strictEqual } from 'node:assert';
4+
import { execPath } from 'node:process';
5+
import { describe, it } from 'node:test';
56

6-
const child = spawn(process.execPath, [
7-
'--experimental-import-meta-resolve',
8-
path('/es-modules/import-resolve-exports.mjs'),
9-
]);
7+
import spawn from './helper.spawnAsPromised.mjs';
108

11-
let stderr = '';
12-
child.stderr.setEncoding('utf8');
13-
child.stderr.on('data', (data) => {
14-
stderr += data;
15-
});
16-
child.on('close', (code, signal) => {
17-
strictEqual(code, 0);
18-
strictEqual(signal, null);
19-
ok(!stderr.toString().includes(
20-
'ExperimentalWarning: The ESM module loader is experimental'
21-
));
22-
ok(!stderr.toString().includes(
23-
'ExperimentalWarning: Conditional exports'
24-
));
9+
10+
describe('ESM: experiemental warning for import.meta.resolve', { concurrency: true }, () => {
11+
it('should not warn when caught', async () => {
12+
const { code, signal, stderr } = await spawn(execPath, [
13+
'--experimental-import-meta-resolve',
14+
path('es-modules/import-resolve-exports.mjs'),
15+
]);
16+
17+
strictEqual(stderr, '');
18+
strictEqual(code, 0);
19+
strictEqual(signal, null);
20+
});
2521
});
Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
11
'use strict';
2+
23
const { mustCall } = require('../common');
3-
const assert = require('assert');
44
const fixtures = require('../common/fixtures');
5-
const { spawn } = require('child_process');
5+
const assert = require('node:assert');
6+
const { spawn } = require('node:child_process');
7+
const { execPath } = require('node:process');
8+
const { describe, it } = require('node:test');
69

7-
const child = spawn(process.execPath, [
8-
'--interactive',
9-
], {
10-
cwd: fixtures.path('es-modules', 'pkgimports'),
11-
});
1210

13-
child.stdin.end(
14-
'try{require("#test");await import("#test")}catch{process.exit(-1)}'
15-
);
11+
describe('ESM: REPL runs', { concurrency: true }, () => {
12+
it(() => {
13+
const child = spawn(execPath, [
14+
'--interactive',
15+
], {
16+
cwd: fixtures.path('es-modules', 'pkgimports'),
17+
});
1618

17-
child.on('exit', mustCall((code) => {
18-
assert.strictEqual(code, 0);
19-
}));
19+
child.stdin.end(
20+
'try{require("#test");await import("#test")}catch{process.exit(-1)}'
21+
);
22+
23+
child.on('exit', mustCall((code) => {
24+
assert.strictEqual(code, 0);
25+
}));
26+
});
27+
});

0 commit comments

Comments
 (0)