Skip to content

Commit 8a94ca7

Browse files
GeoffreyBoothtargos
authored andcommitted
esm: refactor esm tests out of test/message
PR-URL: #41352 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Michaël Zasso <[email protected]>
1 parent 3e2154d commit 8a94ca7

37 files changed

+210
-241
lines changed

test/common/fixtures.js

+6
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,18 @@
22

33
const path = require('path');
44
const fs = require('fs');
5+
const { pathToFileURL } = require('url');
56

67
const fixturesDir = path.join(__dirname, '..', 'fixtures');
78

89
function fixturesPath(...args) {
910
return path.join(fixturesDir, ...args);
1011
}
1112

13+
function fixturesFileURL(...args) {
14+
return pathToFileURL(fixturesPath(...args));
15+
}
16+
1217
function readFixtureSync(args, enc) {
1318
if (Array.isArray(args))
1419
return fs.readFileSync(fixturesPath(...args), enc);
@@ -26,6 +31,7 @@ function readFixtureKeys(enc, ...names) {
2631
module.exports = {
2732
fixturesDir,
2833
path: fixturesPath,
34+
fileURL: fixturesFileURL,
2935
readSync: readFixtureSync,
3036
readKey: readFixtureKey,
3137
readKeys: readFixtureKeys,

test/common/fixtures.mjs

+2
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ import fixtures from './fixtures.js';
33
const {
44
fixturesDir,
55
path,
6+
fileURL,
67
readSync,
78
readKey,
89
} = fixtures;
910

1011
export {
1112
fixturesDir,
1213
path,
14+
fileURL,
1315
readSync,
1416
readKey,
1517
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
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';
6+
7+
const importStatement =
8+
'import { foo, notfound } from \'./module-named-exports.mjs\';';
9+
const importStatementMultiline = `import {
10+
foo,
11+
notfound
12+
} from './module-named-exports.mjs';
13+
`;
14+
15+
[importStatement, importStatementMultiline].forEach((input) => {
16+
const child = spawn(execPath, [
17+
'--input-type=module',
18+
'--eval',
19+
input,
20+
], {
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);
31+
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+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
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';
6+
7+
const child = spawn(execPath, [
8+
'--experimental-json-modules',
9+
path('es-modules', 'import-json-named-export.mjs'),
10+
]);
11+
12+
let stderr = '';
13+
child.stderr.setEncoding('utf8');
14+
child.stderr.on('data', (data) => {
15+
stderr += data;
16+
});
17+
child.on('close', mustCall((code, _signal) => {
18+
notStrictEqual(code, 0);
19+
20+
// SyntaxError: The requested module '../experimental.json'
21+
// does not provide an export named 'ofLife'
22+
match(stderr, /SyntaxError:/);
23+
match(stderr, /'\.\.\/experimental\.json'/);
24+
match(stderr, /'ofLife'/);
25+
}));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { mustCall } from '../common/index.mjs';
2+
import { path } from '../common/fixtures.mjs';
3+
import { match, ok, notStrictEqual } from 'assert';
4+
import { spawn } from 'child_process';
5+
import { execPath } from 'process';
6+
7+
const child = spawn(execPath, [
8+
'--experimental-loader',
9+
'i-dont-exist',
10+
path('print-error-message.js'),
11+
]);
12+
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);
20+
21+
// Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'i-dont-exist'
22+
// imported from
23+
match(stderr, /ERR_MODULE_NOT_FOUND/);
24+
match(stderr, /'i-dont-exist'/);
25+
26+
ok(!stderr.includes('Bad command or file name'));
27+
}));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { mustCall } from '../common/index.mjs';
2+
import { fileURL, path } from '../common/fixtures.mjs';
3+
import { match, notStrictEqual } from 'assert';
4+
import { spawn } from 'child_process';
5+
import { execPath } from 'process';
6+
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+
]);
14+
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);
22+
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+
}));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { mustCall } from '../common/index.mjs';
2+
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';
6+
7+
const child = spawn(execPath, [
8+
'--experimental-loader',
9+
fileURL('es-module-loaders', 'syntax-error.mjs').href,
10+
path('print-error-message.js'),
11+
]);
12+
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);
20+
21+
match(stderr, /SyntaxError:/);
22+
23+
ok(!stderr.includes('Bad command or file name'));
24+
}));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { mustCall } from '../common/index.mjs';
2+
import { fixturesDir } from '../common/fixtures.mjs';
3+
import { match, notStrictEqual } from 'assert';
4+
import { spawn } from 'child_process';
5+
import { execPath } from 'process';
6+
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+
});
25+
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);
33+
match(stderr, expected);
34+
}));
35+
});
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
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';
6+
7+
const child = spawn(execPath, [
8+
path('es-module-loaders', 'syntax-error.mjs'),
9+
]);
10+
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);
18+
match(stderr, /SyntaxError:/);
19+
}));

test/fixtures/es-module-loaders/syntax-error-import.mjs

-1
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/* eslint-disable no-unused-vars */
2+
import { ofLife } from '../experimental.json' assert { type: 'json' };

test/fixtures/esm_loader_not_found_cjs_hint_bare.mjs

-5
This file was deleted.

test/fixtures/print-error-message.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.error('Bad command or file name');

test/message/esm_display_syntax_error_import.mjs

-6
This file was deleted.

test/message/esm_display_syntax_error_import.out

-12
This file was deleted.

test/message/esm_display_syntax_error_import_json_named_export.mjs

-4
This file was deleted.

test/message/esm_display_syntax_error_import_json_named_export.out

-12
This file was deleted.

test/message/esm_display_syntax_error_import_module.mjs

-2
This file was deleted.

test/message/esm_display_syntax_error_import_module.out

-12
This file was deleted.

test/message/esm_display_syntax_error_module.mjs

-2
This file was deleted.

test/message/esm_display_syntax_error_module.out

-9
This file was deleted.

test/message/esm_import_assertion_failed.mjs

-2
This file was deleted.

test/message/esm_import_assertion_failed.out

-18
This file was deleted.

test/message/esm_import_assertion_missing.mjs

-3
This file was deleted.

test/message/esm_import_assertion_missing.out

-19
This file was deleted.

test/message/esm_import_assertion_unsupported.mjs

-2
This file was deleted.

test/message/esm_import_assertion_unsupported.out

-19
This file was deleted.

0 commit comments

Comments
 (0)