Skip to content

Commit

Permalink
esm: refactor esm tests out of test/message
Browse files Browse the repository at this point in the history
PR-URL: #41352
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
  • Loading branch information
GeoffreyBooth authored and aduh95 committed Jan 30, 2022
1 parent b8cacb6 commit b5efa9b
Show file tree
Hide file tree
Showing 37 changed files with 210 additions and 219 deletions.
6 changes: 6 additions & 0 deletions test/common/fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@

const path = require('path');
const fs = require('fs');
const { pathToFileURL } = require('url');

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

function fixturesPath(...args) {
return path.join(fixturesDir, ...args);
}

function fixturesFileURL(...args) {
return pathToFileURL(fixturesPath(...args));
}

function readFixtureSync(args, enc) {
if (Array.isArray(args))
return fs.readFileSync(fixturesPath(...args), enc);
Expand All @@ -26,6 +31,7 @@ function readFixtureKeys(enc, ...names) {
module.exports = {
fixturesDir,
path: fixturesPath,
fileURL: fixturesFileURL,
readSync: readFixtureSync,
readKey: readFixtureKey,
readKeys: readFixtureKeys,
Expand Down
2 changes: 2 additions & 0 deletions test/common/fixtures.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ import fixtures from './fixtures.js';
const {
fixturesDir,
path,
fileURL,
readSync,
readKey,
} = fixtures;

export {
fixturesDir,
path,
fileURL,
readSync,
readKey,
};
39 changes: 39 additions & 0 deletions test/es-module/test-esm-export-not-found.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { mustCall } from '../common/index.mjs';
import { path } from '../common/fixtures.mjs';
import { match, notStrictEqual } from 'assert';
import { spawn } from 'child_process';
import { execPath } from 'process';

const importStatement =
'import { foo, notfound } from \'./module-named-exports.mjs\';';
const importStatementMultiline = `import {
foo,
notfound
} from './module-named-exports.mjs';
`;

[importStatement, importStatementMultiline].forEach((input) => {
const child = spawn(execPath, [
'--input-type=module',
'--eval',
input,
], {
cwd: path('es-module-loaders'),
});

let stderr = '';
child.stderr.setEncoding('utf8');
child.stderr.on('data', (data) => {
stderr += data;
});
child.on('close', mustCall((code, _signal) => {
notStrictEqual(code, 0);

// SyntaxError: The requested module './module-named-exports.mjs'
// does not provide an export named 'notfound'
match(stderr, /SyntaxError:/);
// The quotes ensure that the path starts with ./ and not ../
match(stderr, /'\.\/module-named-exports\.mjs'/);
match(stderr, /notfound/);
}));
});
25 changes: 25 additions & 0 deletions test/es-module/test-esm-import-json-named-export.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { mustCall } from '../common/index.mjs';
import { path } from '../common/fixtures.mjs';
import { match, notStrictEqual } from 'assert';
import { spawn } from 'child_process';
import { execPath } from 'process';

const child = spawn(execPath, [
'--experimental-json-modules',
path('es-modules', 'import-json-named-export.mjs'),
]);

let stderr = '';
child.stderr.setEncoding('utf8');
child.stderr.on('data', (data) => {
stderr += data;
});
child.on('close', mustCall((code, _signal) => {
notStrictEqual(code, 0);

// SyntaxError: The requested module '../experimental.json'
// does not provide an export named 'ofLife'
match(stderr, /SyntaxError:/);
match(stderr, /'\.\.\/experimental\.json'/);
match(stderr, /'ofLife'/);
}));
27 changes: 27 additions & 0 deletions test/es-module/test-esm-loader-not-found.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { mustCall } from '../common/index.mjs';
import { path } from '../common/fixtures.mjs';
import { match, ok, notStrictEqual } from 'assert';
import { spawn } from 'child_process';
import { execPath } from 'process';

const child = spawn(execPath, [
'--experimental-loader',
'i-dont-exist',
path('print-error-message.js'),
]);

let stderr = '';
child.stderr.setEncoding('utf8');
child.stderr.on('data', (data) => {
stderr += data;
});
child.on('close', mustCall((code, _signal) => {
notStrictEqual(code, 0);

// Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'i-dont-exist'
// imported from
match(stderr, /ERR_MODULE_NOT_FOUND/);
match(stderr, /'i-dont-exist'/);

ok(!stderr.includes('Bad command or file name'));
}));
30 changes: 30 additions & 0 deletions test/es-module/test-esm-loader-obsolete-hooks.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { mustCall } from '../common/index.mjs';
import { fileURL, path } from '../common/fixtures.mjs';
import { match, notStrictEqual } from 'assert';
import { spawn } from 'child_process';
import { execPath } from 'process';

const child = spawn(execPath, [
'--no-warnings',
'--throw-deprecation',
'--experimental-loader',
fileURL('es-module-loaders', 'hooks-obsolete.mjs').href,
path('print-error-message.js'),
]);

let stderr = '';
child.stderr.setEncoding('utf8');
child.stderr.on('data', (data) => {
stderr += data;
});
child.on('close', mustCall((code, _signal) => {
notStrictEqual(code, 0);

// DeprecationWarning: Obsolete loader hook(s) supplied and will be ignored:
// dynamicInstantiate, getFormat, getSource, transformSource
match(stderr, /DeprecationWarning:/);
match(stderr, /dynamicInstantiate/);
match(stderr, /getFormat/);
match(stderr, /getSource/);
match(stderr, /transformSource/);
}));
24 changes: 24 additions & 0 deletions test/es-module/test-esm-loader-with-syntax-error.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { mustCall } from '../common/index.mjs';
import { fileURL, path } from '../common/fixtures.mjs';
import { match, ok, notStrictEqual } from 'assert';
import { spawn } from 'child_process';
import { execPath } from 'process';

const child = spawn(execPath, [
'--experimental-loader',
fileURL('es-module-loaders', 'syntax-error.mjs').href,
path('print-error-message.js'),
]);

let stderr = '';
child.stderr.setEncoding('utf8');
child.stderr.on('data', (data) => {
stderr += data;
});
child.on('close', mustCall((code, _signal) => {
notStrictEqual(code, 0);

match(stderr, /SyntaxError:/);

ok(!stderr.includes('Bad command or file name'));
}));
35 changes: 35 additions & 0 deletions test/es-module/test-esm-module-not-found-commonjs-hint.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { mustCall } from '../common/index.mjs';
import { fixturesDir } from '../common/fixtures.mjs';
import { match, notStrictEqual } from 'assert';
import { spawn } from 'child_process';
import { execPath } from 'process';

[
{
input: 'import "./print-error-message"',
// Did you mean to import ../print-error-message.js?
expected: / \.\.\/print-error-message\.js\?/,
},
{
input: 'import obj from "some_module/obj"',
expected: / some_module\/obj\.js\?/,
},
].forEach(({ input, expected }) => {
const child = spawn(execPath, [
'--input-type=module',
'--eval',
input,
], {
cwd: fixturesDir,
});

let stderr = '';
child.stderr.setEncoding('utf8');
child.stderr.on('data', (data) => {
stderr += data;
});
child.on('close', mustCall((code, _signal) => {
notStrictEqual(code, 0);
match(stderr, expected);
}));
});
19 changes: 19 additions & 0 deletions test/es-module/test-esm-syntax-error.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { mustCall } from '../common/index.mjs';
import { path } from '../common/fixtures.mjs';
import { match, notStrictEqual } from 'assert';
import { spawn } from 'child_process';
import { execPath } from 'process';

const child = spawn(execPath, [
path('es-module-loaders', 'syntax-error.mjs'),
]);

let stderr = '';
child.stderr.setEncoding('utf8');
child.stderr.on('data', (data) => {
stderr += data;
});
child.on('close', mustCall((code, _signal) => {
notStrictEqual(code, 0);
match(stderr, /SyntaxError:/);
}));
1 change: 0 additions & 1 deletion test/fixtures/es-module-loaders/syntax-error-import.mjs

This file was deleted.

2 changes: 2 additions & 0 deletions test/fixtures/es-modules/import-json-named-export.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/* eslint-disable no-unused-vars */
import { ofLife } from '../experimental.json' assert { type: 'json' };
5 changes: 0 additions & 5 deletions test/fixtures/esm_loader_not_found_cjs_hint_bare.mjs

This file was deleted.

1 change: 1 addition & 0 deletions test/fixtures/print-error-message.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.error('Bad command or file name');
6 changes: 0 additions & 6 deletions test/message/esm_display_syntax_error_import.mjs

This file was deleted.

10 changes: 0 additions & 10 deletions test/message/esm_display_syntax_error_import.out

This file was deleted.

This file was deleted.

10 changes: 0 additions & 10 deletions test/message/esm_display_syntax_error_import_json_named_export.out

This file was deleted.

2 changes: 0 additions & 2 deletions test/message/esm_display_syntax_error_import_module.mjs

This file was deleted.

10 changes: 0 additions & 10 deletions test/message/esm_display_syntax_error_import_module.out

This file was deleted.

2 changes: 0 additions & 2 deletions test/message/esm_display_syntax_error_module.mjs

This file was deleted.

7 changes: 0 additions & 7 deletions test/message/esm_display_syntax_error_module.out

This file was deleted.

2 changes: 0 additions & 2 deletions test/message/esm_import_assertion_failed.mjs

This file was deleted.

17 changes: 0 additions & 17 deletions test/message/esm_import_assertion_failed.out

This file was deleted.

3 changes: 0 additions & 3 deletions test/message/esm_import_assertion_missing.mjs

This file was deleted.

17 changes: 0 additions & 17 deletions test/message/esm_import_assertion_missing.out

This file was deleted.

2 changes: 0 additions & 2 deletions test/message/esm_import_assertion_unsupported.mjs

This file was deleted.

17 changes: 0 additions & 17 deletions test/message/esm_import_assertion_unsupported.out

This file was deleted.

3 changes: 0 additions & 3 deletions test/message/esm_loader_not_found.mjs

This file was deleted.

Loading

0 comments on commit b5efa9b

Please sign in to comment.