11'use strict' ;
22
33var 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 = / S y n t a x E r r o r : U n e x p e c t e d t o k e n / ;
@@ -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 p a s s i n g / ;
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