Skip to content

Commit 2489090

Browse files
authored
chore: Update experimental module detection test and pin exact Node versions (#5417)
1 parent b2bc769 commit 2489090

5 files changed

Lines changed: 77 additions & 15 deletions

File tree

.github/workflows/mocha.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ jobs:
2323
smoke:
2424
uses: ./.github/workflows/npm-script.yml
2525
with:
26-
# The 22.11.0 is instead of lts per https://github.com/mochajs/mocha/issues/5278
27-
node-versions: '18,20,22.11.0,24'
26+
node-versions: '18,20,22,24'
2827
npm-script: test-smoke
2928

3029
test-node-lts:
@@ -66,9 +65,10 @@ jobs:
6665
coverage: false
6766
with:
6867
os: 'ubuntu-latest,windows-latest'
69-
# The 20.18.3 is instead of 20 per https://github.com/mochajs/mocha/issues/5052
70-
# The 22.11.0 is instead of 22 per https://github.com/mochajs/mocha/issues/5278
71-
node-versions: '18,20.18.3,22.11.0,24'
68+
# We pin exact versions here per https://github.com/mochajs/mocha/issues/5052
69+
# Use 20.18.3 per https://github.com/mochajs/mocha/issues/5278
70+
# Ref https://nodejs.org/en/about/previous-releases
71+
node-versions: '18.20.8,20.18.3,22.17.1,24.4.1'
7272
npm-script: test-node:${{ matrix.test-part }}
7373
coverage: ${{ matrix.coverage }}
7474

.github/workflows/npm-script.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ jobs:
5454
strategy:
5555
fail-fast: false
5656
matrix:
57-
# The 22.11.0 is instead of "lts/*" per https://github.com/mochajs/mocha/issues/5278
58-
node_version: ${{ fromJson(needs.resolve-inputs.outputs.nodeVersions || '["22.11.0"]') }}
57+
node_version: ${{ fromJson(needs.resolve-inputs.outputs.nodeVersions || '["22"]') }}
5958
os: ${{ fromJson(needs.resolve-inputs.outputs.os || '["ubuntu-latest"]') }}
6059
browser: ${{ fromJson(needs.resolve-inputs.outputs.browsers || '[""]') }}
6160
steps:

package-lock.json

Lines changed: 7 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@
166166
"rollup-plugin-node-globals": "^1.4.0",
167167
"rollup-plugin-polyfill-node": "^0.8.0",
168168
"rollup-plugin-visualizer": "^5.6.0",
169+
"semver": "^7.7.2",
169170
"sinon": "^9.0.3",
170171
"unexpected": "^11.14.0",
171172
"unexpected-eventemitter": "^2.2.0",

test/integration/plugins/root-hooks.spec.js

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
var invokeMochaAsync = require('../helpers').invokeMochaAsync;
4+
const semver = require('semver');
45

56
/**
67
* Extracts root hook log messages from run results
@@ -136,7 +137,25 @@ describe('root hooks', function () {
136137
});
137138

138139
describe('support ESM via .js extension w/o type=module', function () {
139-
describe('should fail due to ambiguous file type', function () {
140+
// --(no-)experimental-detect-module was experimental when these tests were written
141+
// https://nodejs.org/api/cli.html#--no-experimental-detect-module
142+
// https://nodejs.org/api/packages.html#syntax-detection
143+
// (introduced in Node 20.10.0, 21.1.0)
144+
// newer versions of Node no longer fail :)
145+
function isNewerVersion(vString) {
146+
// Latest versions considered "older": 18.20.8, 20.18.3, 22.11.0
147+
// (May update after writing)
148+
return semver.satisfies(vString, '^20.19.0 || ^22.12.0 || ^24.0.0');
149+
}
150+
151+
describe('on older versions, should fail due to ambiguous file type', function () {
152+
// --(no-)experimental-detect-module was experimental when these tests were written
153+
// (introduced in Node 20.10.0, 21.1.0)
154+
// newer versions of Node no longer fail :)
155+
if (isNewerVersion(process.versions.node)) {
156+
return true; // skip test on newer Node versions
157+
}
158+
140159
const filename =
141160
'../fixtures/plugins/root-hooks/root-hook-defs-esm-broken.fixture.js';
142161
const noDetectModuleRegex = /SyntaxError: Unexpected token/;
@@ -158,7 +177,8 @@ describe('root hooks', function () {
158177
});
159178

160179
it('with --experimental-detect-module', function () {
161-
// --experimental-detect-module was introduced in Node 21.1.0
180+
// --experimental-detect-module was introduced in Node 20.10.0, 21.1.0
181+
// adding the flag to older versions of Node does nothing
162182
const expectedRegex =
163183
process.version >= 'v21.1.0'
164184
? detectModuleRegex
@@ -177,6 +197,47 @@ describe('root hooks', function () {
177197
);
178198
});
179199
});
200+
201+
describe('on newer versions, should work', function () {
202+
if (!isNewerVersion(process.versions.node)) {
203+
return true; // skip test on older Node versions
204+
}
205+
206+
const filename =
207+
'../fixtures/plugins/root-hooks/root-hook-defs-esm-broken.fixture.js';
208+
const runSuccessRegex = /0 passing/;
209+
210+
it('with --no-experimental-detect-module', function () {
211+
return expect(
212+
invokeMochaAsync(
213+
[
214+
'--require=' + require.resolve(filename), // as object
215+
'--no-experimental-detect-module'
216+
],
217+
'pipe'
218+
)[1],
219+
'when fulfilled',
220+
'to contain output',
221+
runSuccessRegex
222+
);
223+
});
224+
225+
it('with --experimental-detect-module', function () {
226+
return expect(
227+
invokeMochaAsync(
228+
[
229+
'--require=' + require.resolve(filename), // as object
230+
// enabled by default in these newer versions, but clearer to use it explicitly
231+
'--experimental-detect-module'
232+
],
233+
'pipe'
234+
)[1],
235+
'when fulfilled',
236+
'to contain output',
237+
runSuccessRegex
238+
);
239+
});
240+
});
180241
});
181242
});
182243

0 commit comments

Comments
 (0)