Skip to content

chore: Update experimental module detection test and pin exact Node versions #5407

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/mocha.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ jobs:
coverage: false
with:
os: 'ubuntu-latest,windows-latest'
# The 20.18.3 is instead of 20 per https://github.com/mochajs/mocha/issues/5052
# The 22.11.0 is instead of 22 per https://github.com/mochajs/mocha/issues/5278
node-versions: '18,20.18.3,22.11.0,24'
# We pin exact versions per https://github.com/mochajs/mocha/issues/5052
# Ref https://nodejs.org/en/about/previous-releases
node-versions: '18.20.8,20.19.4,22.17.1,24.4.1'
npm-script: test-node:${{ matrix.test-part }}
coverage: ${{ matrix.coverage }}

Expand Down
62 changes: 60 additions & 2 deletions test/integration/plugins/root-hooks.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,23 @@ describe('root hooks', function () {
});

describe('support ESM via .js extension w/o type=module', function () {
describe('should fail due to ambiguous file type', function () {
// --(no-)experimental-detect-module was experimental when these tests were written
// https://nodejs.org/api/cli.html#--no-experimental-detect-module
// https://nodejs.org/api/packages.html#syntax-detection
// (introduced in Node 21.1.0, 20.10.0)
// newer versions of Node no longer fail :)
function isNewerVersion(vString) {
return (vString > 'v20.18.3' || vString > 'v22.11.0' || vString >= 'v24.0.0');
}

describe('on older versions, should fail due to ambiguous file type', function () {
// --(no-)experimental-detect-module was experimental when these tests were written
// (introduced in Node 21.1.0, 20.10.0)
// newer versions of Node no longer fail :)
if (isNewerVersion(process.version)) {
return true; // skip test on newer Node versions
}

const filename =
'../fixtures/plugins/root-hooks/root-hook-defs-esm-broken.fixture.js';
const noDetectModuleRegex = /SyntaxError: Unexpected token/;
Expand All @@ -158,7 +174,8 @@ describe('root hooks', function () {
});

it('with --experimental-detect-module', function () {
// --experimental-detect-module was introduced in Node 21.1.0
// --experimental-detect-module was introduced in Node 21.1.0, 20.10.0
// adding the flag to older versions of Node does nothing
const expectedRegex =
process.version >= 'v21.1.0'
? detectModuleRegex
Expand All @@ -177,6 +194,47 @@ describe('root hooks', function () {
);
});
});

describe('on newer versions, should work', function () {
if (!isNewerVersion(process.version)) {
return true; // skip test on older Node versions
}

const filename =
'../fixtures/plugins/root-hooks/root-hook-defs-esm-broken.fixture.js';
const runSuccessRegex = /0 passing/;

it('with --no-experimental-detect-module', function () {
return expect(
invokeMochaAsync(
[
'--require=' + require.resolve(filename), // as object
'--no-experimental-detect-module'
],
'pipe'
)[1],
'when fulfilled',
'to contain output',
runSuccessRegex
);
});

it('with --experimental-detect-module', function () {
return expect(
invokeMochaAsync(
[
'--require=' + require.resolve(filename), // as object
// enabled by default in these newer versions, but clearer to use it explicitly
'--experimental-detect-module'
],
'pipe'
)[1],
'when fulfilled',
'to contain output',
runSuccessRegex
);
});
});
});
});

Expand Down
Loading