Skip to content

Commit 81a5f64

Browse files
feat: make cli generate esm config based on type: "module" (#4210)
Closes #4012
1 parent b8f6eaa commit 81a5f64

File tree

2 files changed

+81
-9
lines changed

2 files changed

+81
-9
lines changed

src/cli/cli.spec.ts

Lines changed: 77 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,6 @@ const runCli = async (
3333
let mockedProcess: any
3434
const FAKE_CWD = normalize('/foo/bar')
3535
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-
})
4236

4337
// === test ===================================================================
4438

@@ -65,7 +59,14 @@ afterEach(() => {
6559

6660
describe('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 = {

src/cli/config/init.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,14 @@ export const run: CliCommand = async (args: CliCommandArgs /* , logger: Logger *
108108
body = JSON.stringify({ ...pkgJson, jest: jestConfig }, undefined, ' ')
109109
} else {
110110
// js config
111-
const content = []
111+
const content: string[] = []
112112
if (!jestPreset) {
113113
content.push(`${preset.jsImport('tsjPreset')};`, '')
114114
}
115115
content.push(`/** @type {import('ts-jest').JestConfigWithTsJest} */`)
116-
content.push('module.exports = {')
116+
const usesModules = pkgJson.type === 'module'
117+
content.push(usesModules ? 'export default {' : 'module.exports = {')
118+
117119
if (jestPreset) {
118120
content.push(` preset: '${preset.name}',`)
119121
} else {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy