@@ -33,12 +33,6 @@ const runCli = async (
3333let mockedProcess : any
3434const FAKE_CWD = normalize ( '/foo/bar' )
3535const 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- } )
4236
4337// === test ===================================================================
4438
@@ -65,7 +59,14 @@ afterEach(() => {
6559
6660describe ( 'cli' , ( ) => {
6761 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+ } )
6868 expect . assertions ( 2 )
69+
6970 await expect ( runCli ( ) ) . resolves . toMatchInlineSnapshot ( `
7071 {
7172 "exitCode": 0,
@@ -121,9 +122,17 @@ describe('config', () => {
121122 'ts' ,
122123 '--babel' ,
123124 ]
125+
124126 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+ } )
125133 expect . assertions ( 2 )
126134 const res = await runCli ( ...noOption )
135+
127136 expect ( res ) . toEqual ( {
128137 exitCode : 0 ,
129138 log : '' ,
@@ -143,9 +152,17 @@ module.exports = {
143152 ] ,
144153 ] )
145154 } )
155+
146156 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+ } )
147163 expect . assertions ( 2 )
148164 const res = await runCli ( ...fullOptions , 'jest.config.foo.js' )
165+
149166 expect ( res ) . toEqual ( {
150167 exitCode : 0 ,
151168 log : '' ,
@@ -172,9 +189,17 @@ module.exports = {
172189 ] ,
173190 ] )
174191 } )
192+
175193 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+ } )
176200 expect . assertions ( 2 )
177201 const res = await runCli ( ...noOption , 'package.json' )
202+
178203 expect ( res ) . toEqual ( {
179204 exitCode : 0 ,
180205 log : '' ,
@@ -199,8 +224,15 @@ Jest configuration written to "${normalize('/foo/bar/package.json')}".
199224 } )
200225
201226 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+ } )
202233 expect . assertions ( 2 )
203234 const res = await runCli ( ...fullOptions , 'package.json' )
235+
204236 expect ( res ) . toEqual ( {
205237 exitCode : 0 ,
206238 log : '' ,
@@ -230,8 +262,16 @@ Jest configuration written to "${normalize('/foo/bar/package.json')}".
230262 ] ,
231263 ] )
232264 } )
265+
233266 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+ } )
234273 const res = await runCli ( 'help' , noOption [ 0 ] )
274+
235275 expect ( res ) . toMatchInlineSnapshot ( `
236276 {
237277 "exitCode": 0,
@@ -259,7 +299,37 @@ Jest configuration written to "${normalize('/foo/bar/package.json')}".
259299 }
260300 ` )
261301 } )
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+ } )
263333
264334 describe ( 'migrate' , ( ) => {
265335 const pkgPaths = {
0 commit comments