@@ -117,20 +117,21 @@ describe('config', () => {
117
117
'--tsconfig' ,
118
118
'tsconfig.test.json' ,
119
119
'--jsdom' ,
120
- '--no- jest-preset' ,
120
+ '--jest-preset' ,
121
121
'--js' ,
122
122
'ts' ,
123
123
'--babel' ,
124
124
]
125
125
126
- it ( 'should create a jest.config.json (without options)' , async ( ) => {
126
+ it ( 'should create a jest.config.js (without options)' , async ( ) => {
127
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
- } )
133
- expect . assertions ( 2 )
128
+ fs . readFileSync
129
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
130
+ . mockImplementationOnce ( ( f ) : any => {
131
+ if ( f === FAKE_PKG ) return JSON . stringify ( { name : 'mock' , version : '0.0.0-mock.0' } )
132
+ throw new Error ( 'ENOENT' )
133
+ } )
134
+ expect . assertions ( 3 )
134
135
const res = await runCli ( ...noOption )
135
136
136
137
expect ( res ) . toEqual ( {
@@ -141,26 +142,26 @@ Jest configuration written to "${normalize('/foo/bar/jest.config.js')}".
141
142
` ,
142
143
stdout : '' ,
143
144
} )
144
- expect ( fs . writeFileSync . mock . calls ) . toEqual ( [
145
- [
146
- normalize ( '/foo/bar/ jest.config.js' ) ,
147
- `/** @type {import('ts-jest').JestConfigWithTsJest} */
148
- module.exports = {
149
- preset: 'ts-jest',
150
- testEnvironment : 'node ',
151
- };` ,
152
- ] ,
153
- ] )
145
+ expect ( fs . writeFileSync . mock . calls [ 0 ] [ 0 ] ) . toBe ( normalize ( '/foo/bar/jest.config.js' ) )
146
+ expect ( fs . writeFileSync . mock . calls [ 0 ] [ 1 ] ) . toMatchInlineSnapshot ( `
147
+ "/** @type {import('ts- jest').JestConfigWithTsJest} **/
148
+ module.exports = {
149
+ testEnvironment: 'node',
150
+ transform: {
151
+ '^.+.tsx?$' : 'ts-jest ',
152
+ } ,
153
+ };"
154
+ ` )
154
155
} )
155
156
156
- it ( 'should create a jest.config.foo.json (with all options set)' , async ( ) => {
157
+ it ( 'should create a jest.config.foo.js (with all options set)' , async ( ) => {
157
158
fs . existsSync . mockImplementation ( ( f ) => f === FAKE_PKG )
158
159
// eslint-disable-next-line @typescript-eslint/no-explicit-any
159
- fs . readFileSync . mockImplementation ( ( f ) : any => {
160
+ fs . readFileSync . mockImplementationOnce ( ( f ) : any => {
160
161
if ( f === FAKE_PKG ) return JSON . stringify ( { name : 'mock' , version : '0.0.0-mock.0' } )
161
162
throw new Error ( 'ENOENT' )
162
163
} )
163
- expect . assertions ( 2 )
164
+ expect . assertions ( 3 )
164
165
const res = await runCli ( ...fullOptions , 'jest.config.foo.js' )
165
166
166
167
expect ( res ) . toEqual ( {
@@ -171,29 +172,60 @@ Jest configuration written to "${normalize('/foo/bar/jest.config.foo.js')}".
171
172
` ,
172
173
stdout : '' ,
173
174
} )
174
- expect ( fs . writeFileSync . mock . calls ) . toEqual ( [
175
- [
176
- normalize ( '/foo/bar/jest.config.foo.js' ) ,
177
- `const { jsWithTs: tsjPreset } = require('ts-jest/presets');
178
-
179
- /** @type {import('ts-jest').JestConfigWithTsJest} */
180
- module.exports = {
181
- ...tsjPreset,
182
- transform: {
183
- '^.+\\\\.[tj]sx?$': ['ts-jest', {
184
- tsconfig: 'tsconfig.test.json',
185
- babelConfig: true,
186
- }],
187
- },
188
- };` ,
189
- ] ,
190
- ] )
175
+ expect ( fs . writeFileSync . mock . calls [ 0 ] [ 0 ] ) . toBe ( normalize ( '/foo/bar/jest.config.foo.js' ) )
176
+ expect ( fs . writeFileSync . mock . calls [ 0 ] [ 1 ] ) . toMatchInlineSnapshot ( `
177
+ "/** @type {import('ts-jest').JestConfigWithTsJest} **/
178
+ module.exports = {
179
+ testEnvironment: 'jsdom',
180
+ transform: {
181
+ '^.+.[tj]sx?$':
182
+ [
183
+ 'ts-jest',
184
+ {
185
+ tsconfig: 'tsconfig.test.json'
186
+ }
187
+ ]
188
+ ,
189
+ },
190
+ };"
191
+ ` )
192
+ } )
193
+
194
+ it ( 'should create jest config with type "module" package.json' , async ( ) => {
195
+ fs . existsSync . mockImplementation ( ( f ) => f === FAKE_PKG )
196
+ fs . readFileSync
197
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
198
+ . mockImplementationOnce ( ( f ) : any => {
199
+ if ( f === FAKE_PKG ) return JSON . stringify ( { name : 'mock' , version : '0.0.0-mock.0' , type : 'module' } )
200
+ throw new Error ( 'ENOENT' )
201
+ } )
202
+ expect . assertions ( 3 )
203
+ const res = await runCli ( ...noOption )
204
+
205
+ expect ( res ) . toEqual ( {
206
+ exitCode : 0 ,
207
+ log : '' ,
208
+ stderr : `
209
+ Jest configuration written to "${ normalize ( '/foo/bar/jest.config.js' ) } ".
210
+ ` ,
211
+ stdout : '' ,
212
+ } )
213
+ expect ( fs . writeFileSync . mock . calls [ 0 ] [ 0 ] ) . toBe ( normalize ( '/foo/bar/jest.config.js' ) )
214
+ expect ( fs . writeFileSync . mock . calls [ 0 ] [ 1 ] ) . toMatchInlineSnapshot ( `
215
+ "/** @type {import('ts-jest').JestConfigWithTsJest} **/
216
+ export default {
217
+ testEnvironment: 'node',
218
+ transform: {
219
+ '^.+.tsx?$': 'ts-jest',
220
+ },
221
+ };"
222
+ ` )
191
223
} )
192
224
193
225
it ( 'should update package.json (without options)' , async ( ) => {
194
226
fs . existsSync . mockImplementation ( ( f ) => f === FAKE_PKG )
195
227
// eslint-disable-next-line @typescript-eslint/no-explicit-any
196
- fs . readFileSync . mockImplementation ( ( f ) : any => {
228
+ fs . readFileSync . mockImplementationOnce ( ( f ) : any => {
197
229
if ( f === FAKE_PKG ) return JSON . stringify ( { name : 'mock' , version : '0.0.0-mock.0' } )
198
230
throw new Error ( 'ENOENT' )
199
231
} )
@@ -225,12 +257,13 @@ Jest configuration written to "${normalize('/foo/bar/package.json')}".
225
257
226
258
it ( 'should update package.json (with all options set)' , async ( ) => {
227
259
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
- } )
233
- expect . assertions ( 2 )
260
+ fs . readFileSync
261
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
262
+ . mockImplementationOnce ( ( f ) : any => {
263
+ if ( f === FAKE_PKG ) return JSON . stringify ( { name : 'mock' , version : '0.0.0-mock.0' } )
264
+ throw new Error ( 'ENOENT' )
265
+ } )
266
+ expect . assertions ( 3 )
234
267
const res = await runCli ( ...fullOptions , 'package.json' )
235
268
236
269
expect ( res ) . toEqual ( {
@@ -241,26 +274,16 @@ Jest configuration written to "${normalize('/foo/bar/package.json')}".
241
274
` ,
242
275
stdout : '' ,
243
276
} )
244
- expect ( fs . writeFileSync . mock . calls ) . toEqual ( [
245
- [
246
- normalize ( '/foo/bar/package.json' ) ,
247
- `{
248
- "name": "mock",
249
- "version": "0.0.0-mock.0",
250
- "jest": {
251
- "transform": {
252
- "^.+\\\\.[tj]sx?$": [
253
- "ts-jest",
254
- {
255
- "tsconfig": "tsconfig.test.json",
256
- "babelConfig": true
257
- }
258
- ]
259
- }
260
- }
261
- }` ,
262
- ] ,
263
- ] )
277
+ expect ( fs . writeFileSync . mock . calls [ 0 ] [ 0 ] ) . toBe ( normalize ( '/foo/bar/package.json' ) )
278
+ expect ( fs . writeFileSync . mock . calls [ 0 ] [ 1 ] ) . toMatchInlineSnapshot ( `
279
+ "{
280
+ "name": "mock",
281
+ "version": "0.0.0-mock.0",
282
+ "jest": {
283
+ "preset": "ts-jest/presets/js-with-ts"
284
+ }
285
+ }"
286
+ ` )
264
287
} )
265
288
266
289
it ( 'should output help' , async ( ) => {
@@ -289,46 +312,16 @@ Jest configuration written to "${normalize('/foo/bar/package.json')}".
289
312
290
313
Options:
291
314
--force Discard any existing Jest config
292
- --js ts|babel Process .js files with ts-jest if 'ts' or with
315
+ --js ts|babel Process ' .js' files with ts-jest if 'ts' or with
293
316
babel-jest if 'babel'
294
- --no- jest-preset Disable the use of Jest presets
317
+ --jest-preset Toggle using preset
295
318
--tsconfig <file> Path to the tsconfig.json file
296
- --babel Pipe babel-jest after ts-jest
297
- --jsdom Use jsdom as test environment instead of node
319
+ --babel Enable using Babel to process 'js' resulted content from ' ts-jest' processing
320
+ --jsdom Use ' jsdom' as test environment instead of ' node'
298
321
",
299
322
}
300
323
` )
301
324
} )
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
325
} )
333
326
334
327
describe ( 'migrate' , ( ) => {
0 commit comments