Skip to content

Commit

Permalink
fix: use transformer config in config:init instead of globals (#3829
Browse files Browse the repository at this point in the history
)
  • Loading branch information
ahnpnl authored Sep 23, 2022
1 parent c53a2a8 commit 214cb8c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 26 deletions.
21 changes: 11 additions & 10 deletions src/cli/cli.spec.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit 214cb8c

Please sign in to comment.