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

fix: use transformer config in config:init instead of globals #3829

Merged
merged 1 commit into from Sep 23, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 11 additions & 10 deletions src/cli/cli.spec.ts
Expand Up @@ -162,11 +162,11 @@ Jest configuration written to "${normalize('/foo/bar/jest.config.foo.js')}".
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
...tsjPreset,
globals: {
'ts-jest': {
transform: {
'^.+\\\\.[tj]sx?$': ['ts-jest', {
tsconfig: 'tsconfig.test.json',
babelConfig: true,
},
}],
},
};`,
],
Expand Down Expand Up @@ -197,6 +197,7 @@ Jest configuration written to "${normalize('/foo/bar/package.json')}".
],
])
})

it('should update package.json (with all options set)', async () => {
expect.assertions(2)
const res = await runCli(...fullOptions, 'package.json')
Expand All @@ -216,13 +217,13 @@ Jest configuration written to "${normalize('/foo/bar/package.json')}".
"version": "0.0.0-mock.0",
"jest": {
"transform": {
"^.+\\\\.[tj]sx?$": "ts-jest"
},
"globals": {
"ts-jest": {
"tsconfig": "tsconfig.test.json",
"babelConfig": true
}
"^.+\\\\.[tj]sx?$": [
"ts-jest",
{
"tsconfig": "tsconfig.test.json",
"babelConfig": true
}
]
}
}
}`,
Expand Down
51 changes: 36 additions & 15 deletions src/cli/config/init.ts
Expand Up @@ -10,7 +10,8 @@ import { basename, join } from 'path'
import { stringify as stringifyJson5 } from 'json5'

import type { CliCommand, CliCommandArgs } from '..'
import { TsJestPresetDescriptor, defaults, jsWIthBabel, jsWithTs } from '../helpers/presets'
import type { JestConfigWithTsJest, TsJestTransformerOptions } from '../../types'
import { type TsJestPresetDescriptor, defaults, jsWIthBabel, jsWithTs } from '../helpers/presets'

/**
* @internal
Expand All @@ -25,7 +26,8 @@ export const run: CliCommand = async (args: CliCommandArgs /* , logger: Logger *
const hasPackage = isPackage || existsSync(pkgFile)
// read config
const { jestPreset = true, tsconfig: askedTsconfig, force, jsdom } = args
const tsconfig = askedTsconfig === 'tsconfig.json' ? undefined : askedTsconfig
const tsconfig =
askedTsconfig === 'tsconfig.json' ? undefined : (askedTsconfig as TsJestTransformerOptions['tsconfig'])
// read package
const pkgJson = hasPackage ? JSON.parse(readFileSync(pkgFile, 'utf8')) : {}

Expand Down Expand Up @@ -72,17 +74,36 @@ export const run: CliCommand = async (args: CliCommandArgs /* , logger: Logger *

if (isPackage) {
// package.json config
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const base: any = jestPreset ? { preset: preset.name } : { ...preset.value }
if (!jsdom) base.testEnvironment = 'node'
if (tsconfig || shouldPostProcessWithBabel) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const tsJestConf: any = {}
base.globals = { 'ts-jest': tsJestConf }
if (tsconfig) tsJestConf.tsconfig = tsconfig
if (shouldPostProcessWithBabel) tsJestConf.babelConfig = true
const jestConfig: JestConfigWithTsJest = jestPreset ? { preset: preset.name } : { ...preset.value }
if (!jsdom) jestConfig.testEnvironment = 'node'
const transformerConfig = Object.entries(jestConfig.transform ?? {}).reduce((acc, [fileRegex, transformerName]) => {
if (transformerName === 'ts-jest') {
if (tsconfig || shouldPostProcessWithBabel) {
const tsJestConf: TsJestTransformerOptions = {}
if (tsconfig) tsJestConf.tsconfig = tsconfig
if (shouldPostProcessWithBabel) tsJestConf.babelConfig = true

return {
...acc,
[fileRegex]: [transformerName, tsJestConf],
}
}

return {
...acc,
[fileRegex]: transformerName,
}
}

return acc
}, {})
if (Object.keys(transformerConfig).length) {
jestConfig.transform = {
...jestConfig.transform,
...transformerConfig,
}
}
body = JSON.stringify({ ...pkgJson, jest: base }, undefined, ' ')
body = JSON.stringify({ ...pkgJson, jest: jestConfig }, undefined, ' ')
} else {
// js config
const content = []
Expand All @@ -99,11 +120,11 @@ export const run: CliCommand = async (args: CliCommandArgs /* , logger: Logger *
if (!jsdom) content.push(" testEnvironment: 'node',")

if (tsconfig || shouldPostProcessWithBabel) {
content.push(' globals: {')
content.push(" 'ts-jest': {")
content.push(' transform: {')
content.push(" '^.+\\\\.[tj]sx?$': ['ts-jest', {")
if (tsconfig) content.push(` tsconfig: ${stringifyJson5(tsconfig)},`)
if (shouldPostProcessWithBabel) content.push(' babelConfig: true,')
content.push(' },')
content.push(' }],')
content.push(' },')
}
content.push('};')
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Expand Up @@ -192,7 +192,7 @@ export interface GlobalConfigTsJest extends Config.ConfigGlobals {
export interface InitialOptionsTsJest extends Config.InitialOptions {
globals?: GlobalConfigTsJest
}
type TsJestTransformerOptions = TsJestGlobalOptions
export type TsJestTransformerOptions = TsJestGlobalOptions
export interface JestConfigWithTsJest extends Omit<Config.InitialOptions, 'transform'> {
transform?: {
[regex: string]: 'ts-jest' | ['ts-jest', TsJestTransformerOptions] | string | [string, Record<string, unknown>]
Expand Down