7
7
8
8
import * as path from 'path' ;
9
9
import * as fs from 'graceful-fs' ;
10
+ import { onNodeVersions } from '@jest/test-utils' ;
10
11
import { cleanup , extractSummary , writeFiles } from '../Utils' ;
11
12
import runJest from '../runJest' ;
12
13
@@ -23,7 +24,9 @@ test('works with jest.config.ts', () => {
23
24
'package.json' : '{}' ,
24
25
} ) ;
25
26
26
- const { stderr, exitCode} = runJest ( DIR , [ '-w=1' , '--ci=false' ] ) ;
27
+ const { stderr, exitCode} = runJest ( DIR , [ '-w=1' , '--ci=false' ] , {
28
+ nodeOptions : '--no-warnings' ,
29
+ } ) ;
27
30
const { rest, summary} = extractSummary ( stderr ) ;
28
31
expect ( exitCode ) . toBe ( 0 ) ;
29
32
expect ( rest ) . toMatchSnapshot ( ) ;
@@ -39,7 +42,9 @@ test('works with tsconfig.json', () => {
39
42
'tsconfig.json' : '{ "compilerOptions": { "module": "esnext" } }' ,
40
43
} ) ;
41
44
42
- const { stderr, exitCode} = runJest ( DIR , [ '-w=1' , '--ci=false' ] ) ;
45
+ const { stderr, exitCode} = runJest ( DIR , [ '-w=1' , '--ci=false' ] , {
46
+ nodeOptions : '--no-warnings' ,
47
+ } ) ;
43
48
const { rest, summary} = extractSummary ( stderr ) ;
44
49
expect ( exitCode ) . toBe ( 0 ) ;
45
50
expect ( rest ) . toMatchSnapshot ( ) ;
@@ -62,62 +67,95 @@ test('traverses directory tree up until it finds jest.config', () => {
62
67
const { stderr, exitCode, stdout} = runJest (
63
68
path . join ( DIR , 'some' , 'nested' , 'directory' ) ,
64
69
[ '-w=1' , '--ci=false' ] ,
65
- { skipPkgJsonCheck : true } ,
70
+ { nodeOptions : '--no-warnings' , skipPkgJsonCheck : true } ,
66
71
) ;
67
72
68
73
// Snapshot the console.logged `process.cwd()` and make sure it stays the same
69
- expect ( stdout . replaceAll ( / ^ \W + ( .* ) e 2 e / gm, '<<REPLACED>>' ) ) . toMatchSnapshot ( ) ;
74
+ expect (
75
+ stdout
76
+ . replaceAll ( / ^ \W + ( .* ) e 2 e / gm, '<<REPLACED>>' )
77
+ // slightly different log in node versions >= 23
78
+ . replace ( 'at Object.log' , 'at Object.<anonymous>' ) ,
79
+ ) . toMatchSnapshot ( ) ;
70
80
71
81
const { rest, summary} = extractSummary ( stderr ) ;
72
82
expect ( exitCode ) . toBe ( 0 ) ;
73
83
expect ( rest ) . toMatchSnapshot ( ) ;
74
84
expect ( summary ) . toMatchSnapshot ( ) ;
75
85
} ) ;
76
86
77
- const jestPath = require . resolve ( 'jest' ) ;
78
- const jestTypesPath = jestPath . replace ( / \. j s $ / , '.d.ts' ) ;
79
- const jestTypesExists = fs . existsSync ( jestTypesPath ) ;
87
+ onNodeVersions ( '<23.6' , ( ) => {
88
+ const jestPath = require . resolve ( 'jest' ) ;
89
+ const jestTypesPath = jestPath . replace ( / \. j s $ / , '.d.ts' ) ;
90
+ const jestTypesExists = fs . existsSync ( jestTypesPath ) ;
91
+
92
+ ( jestTypesExists ? test : test . skip ) . each ( [ true , false ] ) (
93
+ 'check the config disabled (skip type check: %p)' ,
94
+ skipTypeCheck => {
95
+ writeFiles ( DIR , {
96
+ '__tests__/a-giraffe.js' : "test('giraffe', () => expect(1).toBe(1));" ,
97
+ 'jest.config.ts' : `
98
+ /**@jest-config-loader-options {"transpileOnly":${ ! ! skipTypeCheck } }*/
99
+ import {Config} from 'jest';
100
+ const config: Config = { testTimeout: "10000" };
101
+ export default config;
102
+ ` ,
103
+ 'package.json' : '{}' ,
104
+ } ) ;
105
+
106
+ const typeErrorString =
107
+ "TS2322: Type 'string' is not assignable to type 'number'." ;
108
+ const runtimeErrorString = 'Option "testTimeout" must be of type:' ;
109
+
110
+ const { stderr, exitCode} = runJest ( DIR , [ '-w=1' , '--ci=false' ] ) ;
111
+
112
+ if ( skipTypeCheck ) {
113
+ expect ( stderr ) . not . toMatch ( typeErrorString ) ;
114
+ expect ( stderr ) . toMatch ( runtimeErrorString ) ;
115
+ } else {
116
+ expect ( stderr ) . toMatch ( typeErrorString ) ;
117
+ expect ( stderr ) . not . toMatch ( runtimeErrorString ) ;
118
+ }
119
+
120
+ expect ( exitCode ) . toBe ( 1 ) ;
121
+ } ,
122
+ ) ;
80
123
81
- ( jestTypesExists ? test : test . skip ) . each ( [ true , false ] ) (
82
- 'check the config disabled (skip type check: %p)' ,
83
- skipTypeCheck => {
124
+ test ( 'invalid JS in jest.config.ts' , ( ) => {
84
125
writeFiles ( DIR , {
85
126
'__tests__/a-giraffe.js' : "test('giraffe', () => expect(1).toBe(1));" ,
86
- 'jest.config.ts' : `
87
- /**@jest-config-loader-options {"transpileOnly":${ ! ! skipTypeCheck } }*/
88
- import {Config} from 'jest';
89
- const config: Config = { testTimeout: "10000" };
90
- export default config;
91
- ` ,
127
+ 'jest.config.ts' : "export default i'll break this file yo" ,
92
128
'package.json' : '{}' ,
93
129
} ) ;
94
130
95
- const typeErrorString =
96
- "TS2322: Type 'string' is not assignable to type 'number'." ;
97
- const runtimeErrorString = 'Option "testTimeout" must be of type:' ;
98
-
99
131
const { stderr, exitCode} = runJest ( DIR , [ '-w=1' , '--ci=false' ] ) ;
132
+ expect ( stderr ) . toMatch ( 'TSError: ⨯ Unable to compile TypeScript:' ) ;
133
+ expect ( exitCode ) . toBe ( 1 ) ;
134
+ } ) ;
135
+ } ) ;
100
136
101
- if ( skipTypeCheck ) {
102
- expect ( stderr ) . not . toMatch ( typeErrorString ) ;
103
- expect ( stderr ) . toMatch ( runtimeErrorString ) ;
104
- } else {
105
- expect ( stderr ) . toMatch ( typeErrorString ) ;
106
- expect ( stderr ) . not . toMatch ( runtimeErrorString ) ;
107
- }
137
+ onNodeVersions ( '>=23.6' , ( ) => {
138
+ test ( 'invalid JS in jest.config.ts (node with native TS support)' , ( ) => {
139
+ writeFiles ( DIR , {
140
+ '__tests__/a-giraffe.js' : "test('giraffe', () => expect(1).toBe(1));" ,
141
+ 'jest.config.ts' : "export default i'll break this file yo" ,
142
+ 'package.json' : '{}' ,
143
+ } ) ;
108
144
145
+ const { stderr, exitCode} = runJest ( DIR , [ '-w=1' , '--ci=false' ] , {
146
+ nodeOptions : '--no-warnings' ,
147
+ } ) ;
148
+ expect (
149
+ stderr
150
+ // Remove the stack trace from the error message
151
+ . slice ( 0 , Math . max ( 0 , stderr . indexOf ( 'Caused by' ) ) )
152
+ . trim ( )
153
+ // Replace the path to the config file with a placeholder
154
+ . replace (
155
+ / ( E r r o r : J e s t : F a i l e d t o p a r s e t h e T y p e S c r i p t c o n f i g f i l e ) .* $ / m,
156
+ '$1 <<REPLACED>>' ,
157
+ ) ,
158
+ ) . toMatchSnapshot ( ) ;
109
159
expect ( exitCode ) . toBe ( 1 ) ;
110
- } ,
111
- ) ;
112
-
113
- test ( 'invalid JS in jest.config.ts' , ( ) => {
114
- writeFiles ( DIR , {
115
- '__tests__/a-giraffe.js' : "test('giraffe', () => expect(1).toBe(1));" ,
116
- 'jest.config.ts' : "export default i'll break this file yo" ,
117
- 'package.json' : '{}' ,
118
160
} ) ;
119
-
120
- const { stderr, exitCode} = runJest ( DIR , [ '-w=1' , '--ci=false' ] ) ;
121
- expect ( stderr ) . toMatch ( 'TSError: ⨯ Unable to compile TypeScript:' ) ;
122
- expect ( exitCode ) . toBe ( 1 ) ;
123
161
} ) ;
0 commit comments