@@ -3,6 +3,7 @@ import { test } from 'node:test';
3
3
import * as fixtures from '../common/fixtures.mjs' ;
4
4
import { spawnSync } from 'node:child_process' ;
5
5
import assert from 'node:assert' ;
6
+ import { EOL } from 'node:os' ;
6
7
7
8
test ( 'correctly reports errors when an ESM module is required with --no-experimental-require-module' , ( ) => {
8
9
// The following regex matches the error message that is expected to be thrown
@@ -11,14 +12,17 @@ test('correctly reports errors when an ESM module is required with --no-experime
11
12
// const app = require('./app');
12
13
// ^
13
14
14
- const matchRegex = / p a c k a g e - t y p e - m o d u l e [ \\ / ] + r e q u i r e - e s m - e r r o r - a n n o t a t i o n [ \\ / ] + i n d e x \. c j s : 1 [ \r ? \n ] c o n s t a p p = r e q u i r e \( ' \. \/ a p p ' \) ; [ \r ? \n ] \s { 12 } \^ / ;
15
15
const fixture = fixtures . path ( 'es-modules/package-type-module/require-esm-error-annotation/index.cjs' ) ;
16
16
const args = [ '--no-experimental-require-module' , fixture ] ;
17
17
18
+ const lineNumber = 1 ;
19
+ const lineContent = `const app = require('./app');` ;
20
+ const fullMessage = `${ fixture } :${ lineNumber } ${ EOL } ${ lineContent } ${ EOL } ^${ EOL } ` ;
21
+
18
22
const result = spawnSync ( process . execPath , args ) ;
19
23
20
24
assert . strictEqual ( result . status , 1 ) ;
21
- assert ( result . stderr . toString ( ) . match ( matchRegex ) ) ;
25
+ assert ( result . stderr . toString ( ) . indexOf ( fullMessage ) > - 1 ) ;
22
26
assert . strictEqual ( result . stdout . toString ( ) , '' ) ;
23
27
} ) ;
24
28
@@ -29,13 +33,16 @@ test('correctly reports error for a longer stack trace', () => {
29
33
// require('./app.js')
30
34
// ^
31
35
32
- const matchRegex = / p a c k a g e - t y p e - m o d u l e [ \\ / ] + r e q u i r e - e s m - e r r o r - a n n o t a t i o n [ \\ / ] + l o n g e r - s t a c k \. c j s : 6 [ \r ? \n ] \s { 2 } r e q u i r e \( ' \. \/ a p p \. j s ' \) [ \r ? \n ] \s { 2 } \^ / ;
33
36
const fixture = fixtures . path ( 'es-modules/package-type-module/require-esm-error-annotation/longer-stack.cjs' ) ;
34
37
const args = [ '--no-experimental-require-module' , fixture ] ;
35
38
39
+ const lineNumber = 6 ;
40
+ const lineContent = "require('./app.js')" ;
41
+ const fullMessage = `${ fixture } :${ lineNumber } ${ EOL } ${ lineContent } ${ EOL } ^${ EOL } ` ;
42
+
36
43
const result = spawnSync ( process . execPath , args ) ;
37
44
38
45
assert . strictEqual ( result . status , 1 ) ;
39
- assert ( result . stderr . toString ( ) . match ( matchRegex ) ) ;
40
46
assert . strictEqual ( result . stdout . toString ( ) , '' ) ;
47
+ assert ( result . stderr . toString ( ) . indexOf ( fullMessage ) > - 1 ) ;
41
48
} ) ;
0 commit comments