Skip to content

Commit

Permalink
fix: update test case to handle ESM files without type=module since t…
Browse files Browse the repository at this point in the history
…hey are no longer considered ambiguous after Node.js 22.7.
  • Loading branch information
Donbasilpeter committed Feb 7, 2025
1 parent 3e476fe commit 982a7c8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 29 deletions.
2 changes: 1 addition & 1 deletion lib/nodejs/esm-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ exports.requireOrImport = async (file, esmDecorator) => {
return require(file);
} catch (requireErr) {
if (
requireErr.code === 'ERR_REQUIRE_ESM' ||
requireErr.code === 'ERR_REQUIRE_ESM' || requireErr.code === 'ERR_INTERNAL_ASSERTION' ||
(requireErr instanceof SyntaxError &&
requireErr
.toString()
Expand Down
47 changes: 19 additions & 28 deletions test/integration/plugins/root-hooks.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,47 +136,38 @@ describe('root hooks', function () {
});

describe('support ESM via .js extension w/o type=module', function () {
describe('should fail due to ambiguous file type', function () {
const filename =
'../fixtures/plugins/root-hooks/root-hook-defs-esm-broken.fixture.js';
const noDetectModuleRegex = /SyntaxError: Unexpected token/;
const detectModuleRegex = /Cannot require\(\) ES Module/;

it('with --no-experimental-detect-module', function () {
const filename =
'../fixtures/plugins/root-hooks/root-hook-defs-esm-broken.fixture.js';
const [major, minor] = process.version.slice(1).split('.').map(Number);
if (major > 22 || (major === 22 && minor >= 7)) {
it('It should not fail since nodejs can recognize the file based on syntax', function () {
return expect(
invokeMochaAsync(
[
'--require=' + require.resolve(filename), // as object
'--no-experimental-detect-module'
],
'pipe'
)[1],
'when fulfilled',
'to contain output',
noDetectModuleRegex
runMochaForHookOutput([
'--require=' + require.resolve(filename),
require.resolve(
'../fixtures/plugins/root-hooks/root-hook-test.fixture.js'
)
]),
'to be fulfilled with',
['mjs afterAll', 'mjs beforeAll']
);
});

it('with --experimental-detect-module', function () {
// --experimental-detect-module was introduced in Node 21.1.0
const expectedRegex =
process.version >= 'v21.1.0'
? detectModuleRegex
: noDetectModuleRegex;
} else {
const noModuleDetectedRegex = /SyntaxError: Unexpected token/;
it('should fail due to ambiguous file type', function () {
return expect(
invokeMochaAsync(
[
'--require=' + require.resolve(filename), // as object
'--experimental-detect-module'
'--require=' + require.resolve(filename) // as object
],
'pipe'
)[1],
'when fulfilled',
'to contain output',
expectedRegex
noModuleDetectedRegex
);
});
});
}
});
});

Expand Down

0 comments on commit 982a7c8

Please sign in to comment.