@@ -33,12 +33,6 @@ const runCli = async (
33
33
let mockedProcess : any
34
34
const FAKE_CWD = normalize ( '/foo/bar' )
35
35
const FAKE_PKG = normalize ( `${ FAKE_CWD } /package.json` )
36
- fs . existsSync . mockImplementation ( ( f ) => f === FAKE_PKG )
37
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
38
- fs . readFileSync . mockImplementation ( ( f ) : any => {
39
- if ( f === FAKE_PKG ) return JSON . stringify ( { name : 'mock' , version : '0.0.0-mock.0' } )
40
- throw new Error ( 'ENOENT' )
41
- } )
42
36
43
37
// === test ===================================================================
44
38
@@ -65,7 +59,14 @@ afterEach(() => {
65
59
66
60
describe ( 'cli' , ( ) => {
67
61
it ( 'should output usage' , async ( ) => {
62
+ fs . existsSync . mockImplementation ( ( f ) => f === FAKE_PKG )
63
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
64
+ fs . readFileSync . mockImplementation ( ( f ) : any => {
65
+ if ( f === FAKE_PKG ) return JSON . stringify ( { name : 'mock' , version : '0.0.0-mock.0' } )
66
+ throw new Error ( 'ENOENT' )
67
+ } )
68
68
expect . assertions ( 2 )
69
+
69
70
await expect ( runCli ( ) ) . resolves . toMatchInlineSnapshot ( `
70
71
{
71
72
"exitCode": 0,
@@ -121,9 +122,17 @@ describe('config', () => {
121
122
'ts' ,
122
123
'--babel' ,
123
124
]
125
+
124
126
it ( 'should create a jest.config.json (without options)' , async ( ) => {
127
+ fs . existsSync . mockImplementation ( ( f ) => f === FAKE_PKG )
128
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
129
+ fs . readFileSync . mockImplementation ( ( f ) : any => {
130
+ if ( f === FAKE_PKG ) return JSON . stringify ( { name : 'mock' , version : '0.0.0-mock.0' } )
131
+ throw new Error ( 'ENOENT' )
132
+ } )
125
133
expect . assertions ( 2 )
126
134
const res = await runCli ( ...noOption )
135
+
127
136
expect ( res ) . toEqual ( {
128
137
exitCode : 0 ,
129
138
log : '' ,
@@ -143,9 +152,17 @@ module.exports = {
143
152
] ,
144
153
] )
145
154
} )
155
+
146
156
it ( 'should create a jest.config.foo.json (with all options set)' , async ( ) => {
157
+ fs . existsSync . mockImplementation ( ( f ) => f === FAKE_PKG )
158
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
159
+ fs . readFileSync . mockImplementation ( ( f ) : any => {
160
+ if ( f === FAKE_PKG ) return JSON . stringify ( { name : 'mock' , version : '0.0.0-mock.0' } )
161
+ throw new Error ( 'ENOENT' )
162
+ } )
147
163
expect . assertions ( 2 )
148
164
const res = await runCli ( ...fullOptions , 'jest.config.foo.js' )
165
+
149
166
expect ( res ) . toEqual ( {
150
167
exitCode : 0 ,
151
168
log : '' ,
@@ -172,9 +189,17 @@ module.exports = {
172
189
] ,
173
190
] )
174
191
} )
192
+
175
193
it ( 'should update package.json (without options)' , async ( ) => {
194
+ fs . existsSync . mockImplementation ( ( f ) => f === FAKE_PKG )
195
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
196
+ fs . readFileSync . mockImplementation ( ( f ) : any => {
197
+ if ( f === FAKE_PKG ) return JSON . stringify ( { name : 'mock' , version : '0.0.0-mock.0' } )
198
+ throw new Error ( 'ENOENT' )
199
+ } )
176
200
expect . assertions ( 2 )
177
201
const res = await runCli ( ...noOption , 'package.json' )
202
+
178
203
expect ( res ) . toEqual ( {
179
204
exitCode : 0 ,
180
205
log : '' ,
@@ -199,8 +224,15 @@ Jest configuration written to "${normalize('/foo/bar/package.json')}".
199
224
} )
200
225
201
226
it ( 'should update package.json (with all options set)' , async ( ) => {
227
+ fs . existsSync . mockImplementation ( ( f ) => f === FAKE_PKG )
228
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
229
+ fs . readFileSync . mockImplementation ( ( f ) : any => {
230
+ if ( f === FAKE_PKG ) return JSON . stringify ( { name : 'mock' , version : '0.0.0-mock.0' } )
231
+ throw new Error ( 'ENOENT' )
232
+ } )
202
233
expect . assertions ( 2 )
203
234
const res = await runCli ( ...fullOptions , 'package.json' )
235
+
204
236
expect ( res ) . toEqual ( {
205
237
exitCode : 0 ,
206
238
log : '' ,
@@ -230,8 +262,16 @@ Jest configuration written to "${normalize('/foo/bar/package.json')}".
230
262
] ,
231
263
] )
232
264
} )
265
+
233
266
it ( 'should output help' , async ( ) => {
267
+ fs . existsSync . mockImplementation ( ( f ) => f === FAKE_PKG )
268
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
269
+ fs . readFileSync . mockImplementation ( ( f ) : any => {
270
+ if ( f === FAKE_PKG ) return JSON . stringify ( { name : 'mock' , version : '0.0.0-mock.0' } )
271
+ throw new Error ( 'ENOENT' )
272
+ } )
234
273
const res = await runCli ( 'help' , noOption [ 0 ] )
274
+
235
275
expect ( res ) . toMatchInlineSnapshot ( `
236
276
{
237
277
"exitCode": 0,
@@ -259,7 +299,37 @@ Jest configuration written to "${normalize('/foo/bar/package.json')}".
259
299
}
260
300
` )
261
301
} )
262
- } ) // init
302
+
303
+ it ( 'should create jest config with type "module" package.json' , async ( ) => {
304
+ fs . existsSync . mockImplementation ( ( f ) => f === FAKE_PKG )
305
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
306
+ fs . readFileSync . mockImplementation ( ( f ) : any => {
307
+ if ( f === FAKE_PKG ) return JSON . stringify ( { name : 'mock' , version : '0.0.0-mock.0' , type : 'module' } )
308
+ throw new Error ( 'ENOENT' )
309
+ } )
310
+ expect . assertions ( 2 )
311
+ const res = await runCli ( ...noOption )
312
+
313
+ expect ( res ) . toEqual ( {
314
+ exitCode : 0 ,
315
+ log : '' ,
316
+ stderr : `
317
+ Jest configuration written to "${ normalize ( '/foo/bar/jest.config.js' ) } ".
318
+ ` ,
319
+ stdout : '' ,
320
+ } )
321
+ expect ( fs . writeFileSync . mock . calls ) . toEqual ( [
322
+ [
323
+ normalize ( '/foo/bar/jest.config.js' ) ,
324
+ `/** @type {import('ts-jest').JestConfigWithTsJest} */
325
+ export default {
326
+ preset: 'ts-jest',
327
+ testEnvironment: 'node',
328
+ };` ,
329
+ ] ,
330
+ ] )
331
+ } )
332
+ } )
263
333
264
334
describe ( 'migrate' , ( ) => {
265
335
const pkgPaths = {
0 commit comments