Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): config:init makes mjs config when using ESModules #4210

Merged
merged 1 commit into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 77 additions & 7 deletions src/cli/cli.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,6 @@ const runCli = async (
let mockedProcess: any
const FAKE_CWD = normalize('/foo/bar')
const FAKE_PKG = normalize(`${FAKE_CWD}/package.json`)
fs.existsSync.mockImplementation((f) => f === FAKE_PKG)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
fs.readFileSync.mockImplementation((f): any => {
if (f === FAKE_PKG) return JSON.stringify({ name: 'mock', version: '0.0.0-mock.0' })
throw new Error('ENOENT')
})

// === test ===================================================================

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

describe('cli', () => {
it('should output usage', async () => {
fs.existsSync.mockImplementation((f) => f === FAKE_PKG)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
fs.readFileSync.mockImplementation((f): any => {
if (f === FAKE_PKG) return JSON.stringify({ name: 'mock', version: '0.0.0-mock.0' })
throw new Error('ENOENT')
})
expect.assertions(2)

await expect(runCli()).resolves.toMatchInlineSnapshot(`
{
"exitCode": 0,
Expand Down Expand Up @@ -121,9 +122,17 @@ describe('config', () => {
'ts',
'--babel',
]

it('should create a jest.config.json (without options)', async () => {
fs.existsSync.mockImplementation((f) => f === FAKE_PKG)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
fs.readFileSync.mockImplementation((f): any => {
if (f === FAKE_PKG) return JSON.stringify({ name: 'mock', version: '0.0.0-mock.0' })
throw new Error('ENOENT')
})
expect.assertions(2)
const res = await runCli(...noOption)

expect(res).toEqual({
exitCode: 0,
log: '',
Expand All @@ -143,9 +152,17 @@ module.exports = {
],
])
})

it('should create a jest.config.foo.json (with all options set)', async () => {
fs.existsSync.mockImplementation((f) => f === FAKE_PKG)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
fs.readFileSync.mockImplementation((f): any => {
if (f === FAKE_PKG) return JSON.stringify({ name: 'mock', version: '0.0.0-mock.0' })
throw new Error('ENOENT')
})
expect.assertions(2)
const res = await runCli(...fullOptions, 'jest.config.foo.js')

expect(res).toEqual({
exitCode: 0,
log: '',
Expand All @@ -172,9 +189,17 @@ module.exports = {
],
])
})

it('should update package.json (without options)', async () => {
fs.existsSync.mockImplementation((f) => f === FAKE_PKG)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
fs.readFileSync.mockImplementation((f): any => {
if (f === FAKE_PKG) return JSON.stringify({ name: 'mock', version: '0.0.0-mock.0' })
throw new Error('ENOENT')
})
expect.assertions(2)
const res = await runCli(...noOption, 'package.json')

expect(res).toEqual({
exitCode: 0,
log: '',
Expand All @@ -199,8 +224,15 @@ Jest configuration written to "${normalize('/foo/bar/package.json')}".
})

it('should update package.json (with all options set)', async () => {
fs.existsSync.mockImplementation((f) => f === FAKE_PKG)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
fs.readFileSync.mockImplementation((f): any => {
if (f === FAKE_PKG) return JSON.stringify({ name: 'mock', version: '0.0.0-mock.0' })
throw new Error('ENOENT')
})
expect.assertions(2)
const res = await runCli(...fullOptions, 'package.json')

expect(res).toEqual({
exitCode: 0,
log: '',
Expand Down Expand Up @@ -230,8 +262,16 @@ Jest configuration written to "${normalize('/foo/bar/package.json')}".
],
])
})

it('should output help', async () => {
fs.existsSync.mockImplementation((f) => f === FAKE_PKG)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
fs.readFileSync.mockImplementation((f): any => {
if (f === FAKE_PKG) return JSON.stringify({ name: 'mock', version: '0.0.0-mock.0' })
throw new Error('ENOENT')
})
const res = await runCli('help', noOption[0])

expect(res).toMatchInlineSnapshot(`
{
"exitCode": 0,
Expand Down Expand Up @@ -259,7 +299,37 @@ Jest configuration written to "${normalize('/foo/bar/package.json')}".
}
`)
})
}) // init

it('should create jest config with type "module" package.json', async () => {
fs.existsSync.mockImplementation((f) => f === FAKE_PKG)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
fs.readFileSync.mockImplementation((f): any => {
if (f === FAKE_PKG) return JSON.stringify({ name: 'mock', version: '0.0.0-mock.0', type: 'module' })
throw new Error('ENOENT')
})
expect.assertions(2)
const res = await runCli(...noOption)

expect(res).toEqual({
exitCode: 0,
log: '',
stderr: `
Jest configuration written to "${normalize('/foo/bar/jest.config.js')}".
`,
stdout: '',
})
expect(fs.writeFileSync.mock.calls).toEqual([
[
normalize('/foo/bar/jest.config.js'),
`/** @type {import('ts-jest').JestConfigWithTsJest} */
export default {
preset: 'ts-jest',
testEnvironment: 'node',
};`,
],
])
})
})

describe('migrate', () => {
const pkgPaths = {
Expand Down
6 changes: 4 additions & 2 deletions src/cli/config/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,14 @@ export const run: CliCommand = async (args: CliCommandArgs /* , logger: Logger *
body = JSON.stringify({ ...pkgJson, jest: jestConfig }, undefined, ' ')
} else {
// js config
const content = []
const content: string[] = []
if (!jestPreset) {
content.push(`${preset.jsImport('tsjPreset')};`, '')
}
content.push(`/** @type {import('ts-jest').JestConfigWithTsJest} */`)
content.push('module.exports = {')
const usesModules = pkgJson.type === 'module'
content.push(usesModules ? 'export default {' : 'module.exports = {')

if (jestPreset) {
content.push(` preset: '${preset.name}',`)
} else {
Expand Down
Loading