diff --git a/e2e/__tests__/__snapshots__/logger.test.ts.snap b/e2e/__tests__/__snapshots__/logger.test.ts.snap index 7e17f048aa..54551edc59 100644 --- a/e2e/__tests__/__snapshots__/logger.test.ts.snap +++ b/e2e/__tests__/__snapshots__/logger.test.ts.snap @@ -9,12 +9,12 @@ Array [ "[level:20] created new transformer", "[level:20] created new transformer", "[level:30] no matching config-set found, creating a new one", + "[level:20] backporting config", + "[level:20] normalized jest config", "[level:20] loaded module typescript", "[level:20] patching typescript", "[level:20] checking version of typescript: OK", "[level:20] normalized compiler module config via ts-jest option", - "[level:20] backporting config", - "[level:20] normalized jest config", "[level:20] babel is disabled", "[level:20] normalized diagnostics config via ts-jest option", "[level:20] readTsConfig(): reading /tsconfig.json", @@ -46,12 +46,12 @@ Array [ "[level:20] created new transformer", "[level:20] created new transformer", "[level:30] no matching config-set found, creating a new one", + "[level:20] backporting config", + "[level:20] normalized jest config", "[level:20] loaded module typescript", "[level:20] patching typescript", "[level:20] checking version of typescript: OK", "[level:20] normalized compiler module config via ts-jest option", - "[level:20] backporting config", - "[level:20] normalized jest config", "[level:20] normalized babel config via ts-jest option", "[level:20] loaded module babel-jest", "[level:20] patching babel-jest", @@ -89,12 +89,12 @@ Array [ "[level:20] created new transformer", "[level:20] created new transformer", "[level:30] no matching config-set found, creating a new one", + "[level:20] backporting config", + "[level:20] normalized jest config", "[level:20] loaded module typescript", "[level:20] patching typescript", "[level:20] checking version of typescript: OK", "[level:20] normalized compiler module config via ts-jest option", - "[level:20] backporting config", - "[level:20] normalized jest config", "[level:20] resolved path from babel.config.js to /babel.config.js", "[level:20] normalized babel config via ts-jest option", "[level:20] loaded module babel-jest", diff --git a/src/cli/cli.spec.ts b/src/cli/cli.spec.ts index fcc6cd772d..8d73ec6ee2 100644 --- a/src/cli/cli.spec.ts +++ b/src/cli/cli.spec.ts @@ -307,30 +307,30 @@ Jest configuration written to "${normalize('/foo/bar/package.json')}". ) const res = await runCli(...noOption, pkgPaths.current) expect(res).toMatchInlineSnapshot(` - Object { - "exitCode": 0, - "log": "", - "stderr": " - Migrated Jest configuration: - - - Detected preset 'default' as the best matching preset for your configuration. - Visit https://kulshekhar.github.io/ts-jest/user/config/#jest-preset for more information about presets. - - ", - "stdout": "\\"jest\\": { - \\"globals\\": { - \\"ts-jest\\": { - \\"tsconfig\\": { - \\"target\\": \\"es6\\" - } - } - }, - \\"preset\\": \\"ts-jest\\" - } - ", - } - `) +Object { + "exitCode": 0, + "log": "", + "stderr": " +Migrated Jest configuration: + + +Detected preset 'default' as the best matching preset for your configuration. +Visit https://kulshekhar.github.io/ts-jest/user/config/#jest-preset for more information about presets. + +", + "stdout": "\\"jest\\": { + \\"globals\\": { + \\"ts-jest\\": { + \\"tsconfig\\": { + \\"target\\": \\"es6\\" + } + } + }, + \\"preset\\": \\"ts-jest\\" +} +", +} +`) expect(fs.writeFileSync).not.toHaveBeenCalled() }) @@ -346,24 +346,24 @@ Jest configuration written to "${normalize('/foo/bar/package.json')}". ) const res = await runCli(...fullOptions, pkgPaths.current) expect(res).toMatchInlineSnapshot(` - Object { - "exitCode": 0, - "log": "", - "stderr": " - Migrated Jest configuration: - ", - "stdout": "\\"jest\\": { - \\"globals\\": { - \\"ts-jest\\": { - \\"tsconfig\\": { - \\"target\\": \\"es6\\" - } - } - } - } - ", - } - `) +Object { + "exitCode": 0, + "log": "", + "stderr": " +Migrated Jest configuration: +", + "stdout": "\\"jest\\": { + \\"globals\\": { + \\"ts-jest\\": { + \\"tsconfig\\": { + \\"target\\": \\"es6\\" + } + } + } +} +", +} +`) expect(fs.writeFileSync).not.toHaveBeenCalled() }) @@ -388,29 +388,29 @@ Jest configuration written to "${normalize('/foo/bar/package.json')}". ) const res = await runCli(...noOption, pkgPaths.current) expect(res.stdout).toMatchInlineSnapshot(` - "\\"jest\\": { - \\"globals\\": { - \\"ts-jest\\": { - \\"tsconfig\\": { - \\"target\\": \\"es6\\" - } - } - }, - \\"moduleFileExtensions\\": [ - \\"js\\", - \\"ts\\", - \\"tsx\\" - ], - \\"testMatch\\": [ - \\"**/?(*.)+(spec|test).js?(x)\\", - \\"**/?(*.)+(spec|test).ts?(x)\\", - \\"**/__tests__/**/*.js?(x)\\", - \\"**/__tests__/**/*.ts?(x)\\" - ], - \\"preset\\": \\"ts-jest\\" - } - " - `) +"\\"jest\\": { + \\"globals\\": { + \\"ts-jest\\": { + \\"tsconfig\\": { + \\"target\\": \\"es6\\" + } + } + }, + \\"moduleFileExtensions\\": [ + \\"js\\", + \\"ts\\", + \\"tsx\\" + ], + \\"testMatch\\": [ + \\"**/?(*.)+(spec|test).js?(x)\\", + \\"**/?(*.)+(spec|test).ts?(x)\\", + \\"**/__tests__/**/*.js?(x)\\", + \\"**/__tests__/**/*.ts?(x)\\" + ], + \\"preset\\": \\"ts-jest\\" +} +" +`) }) it('should reset testMatch if testRegex is used', async () => { @@ -427,13 +427,13 @@ Jest configuration written to "${normalize('/foo/bar/package.json')}". ) const res = await runCli(...noOption, pkgPaths.current) expect(res.stdout).toMatchInlineSnapshot(` - "\\"jest\\": { - \\"testRegex\\": \\"foo-pattern\\", - \\"preset\\": \\"ts-jest\\", - \\"testMatch\\": null - } - " - `) +"\\"jest\\": { + \\"testRegex\\": \\"foo-pattern\\", + \\"preset\\": \\"ts-jest\\", + \\"testMatch\\": null +} +" +`) }) it('should detect best preset', async () => { @@ -445,31 +445,31 @@ Jest configuration written to "${normalize('/foo/bar/package.json')}". jest.doMock(pkgPaths.nextCfg, () => ({}), { virtual: true }) let res = await runCli(...noOption, pkgPaths.currentCfg) expect(res.stdout).toMatchInlineSnapshot(` - "module.exports = { - preset: 'ts-jest', - } - " - `) +"module.exports = { + preset: 'ts-jest', +} +" +`) // js-with-ts from args jest.doMock(pkgPaths.nextCfg, () => ({}), { virtual: true }) res = await runCli(...noOption, '--allow-js', pkgPaths.currentCfg) expect(res.stdout).toMatchInlineSnapshot(` - "module.exports = { - preset: 'ts-jest/presets/js-with-ts', - } - " - `) +"module.exports = { + preset: 'ts-jest/presets/js-with-ts', +} +" +`) // js-with-ts from previous transform jest.doMock(pkgPaths.nextCfg, () => ({ transform: { '^.+\\.[tj]sx?$': 'ts-jest' } }), { virtual: true }) res = await runCli(...noOption, pkgPaths.currentCfg) expect(res.stdout).toMatchInlineSnapshot(` - "module.exports = { - preset: 'ts-jest/presets/js-with-ts', - } - " - `) +"module.exports = { + preset: 'ts-jest/presets/js-with-ts', +} +" +`) // js-with-babel from previous transform jest.doMock(pkgPaths.nextCfg, () => ({ transform: { '^.+\\.jsx?$': 'babel-jest', '^.+\\.tsx?$': 'ts-jest' } }), { @@ -477,11 +477,11 @@ Jest configuration written to "${normalize('/foo/bar/package.json')}". }) res = await runCli(...noOption, pkgPaths.currentCfg) expect(res.stdout).toMatchInlineSnapshot(` - "module.exports = { - preset: 'ts-jest/presets/js-with-babel', - } - " - `) +"module.exports = { + preset: 'ts-jest/presets/js-with-babel', +} +" +`) // defaults when previous transform is ambiguous jest.doMock( @@ -491,15 +491,15 @@ Jest configuration written to "${normalize('/foo/bar/package.json')}". ) res = await runCli(...noOption, pkgPaths.currentCfg) expect(res.stdout).toMatchInlineSnapshot(` - "module.exports = { - transform: { - '^src/js/.+\\\\\\\\.jsx?$': 'babel-jest', - '^src/ts/.+\\\\\\\\.tsx?$': 'ts-jest', - }, - preset: 'ts-jest', - } - " - `) +"module.exports = { + transform: { + '^src/js/.+\\\\\\\\.jsx?$': 'babel-jest', + '^src/ts/.+\\\\\\\\.tsx?$': 'ts-jest', + }, + preset: 'ts-jest', +} +" +`) }) it('should normalize transform values', async () => { @@ -520,16 +520,16 @@ Jest configuration written to "${normalize('/foo/bar/package.json')}". ) const res = await runCli(...noOption, pkgPaths.current) expect(res.stdout).toMatchInlineSnapshot(` - "\\"jest\\": { - \\"transform\\": { - \\"/src/.+\\\\\\\\.[jt]s$\\": \\"ts-jest\\", - \\"foo\\\\\\\\.ts\\": \\"ts-jest\\", - \\"bar\\\\\\\\.ts\\": \\"ts-jest\\" - }, - \\"preset\\": \\"ts-jest\\" - } - " - `) +"\\"jest\\": { + \\"transform\\": { + \\"/src/.+\\\\\\\\.[jt]s$\\": \\"ts-jest\\", + \\"foo\\\\\\\\.ts\\": \\"ts-jest\\", + \\"bar\\\\\\\\.ts\\": \\"ts-jest\\" + }, + \\"preset\\": \\"ts-jest\\" +} +" +`) }) it('should output help', async () => { diff --git a/src/config/config-set.spec.ts b/src/config/config-set.spec.ts index 1e730bcbdd..995cb34d9e 100644 --- a/src/config/config-set.spec.ts +++ b/src/config/config-set.spec.ts @@ -30,6 +30,11 @@ beforeEach(() => { jest.clearAllMocks() }) +test('should create a default fallback jest config when jest config is undefined', () => { + // @ts-expect-error testing purpose + expect(new ConfigSet(undefined)._jestCfg).toBeDefined() +}) + describe('parsedTsConfig', () => { const get = (tsJest?: TsJestGlobalOptions) => createConfigSet({ tsJestConfig: tsJest }).parsedTsConfig diff --git a/src/config/config-set.ts b/src/config/config-set.ts index 4db6cbff57..6ac536ff19 100644 --- a/src/config/config-set.ts +++ b/src/config/config-set.ts @@ -166,13 +166,14 @@ export class ConfigSet { tsBuildInfoFile: undefined, } - constructor(readonly jestConfig: ProjectConfigTsJest, readonly parentLogger?: Logger) { + constructor(jestConfig: ProjectConfigTsJest | undefined, readonly parentLogger?: Logger) { this.logger = this.parentLogger ? this.parentLogger.child({ [LogContexts.namespace]: 'config' }) : rootLogger.child({ namespace: 'config' }) - this.cwd = normalize(this.jestConfig.cwd ?? process.cwd()) - this.rootDir = normalize(this.jestConfig.rootDir ?? this.cwd) - const tsJestCfg = this.jestConfig.globals && this.jestConfig.globals['ts-jest'] + this._backportJestCfg(jestConfig ?? Object.create(null)) + this.cwd = normalize(this._jestCfg.cwd ?? process.cwd()) + this.rootDir = normalize(this._jestCfg.rootDir ?? this.cwd) + const tsJestCfg = this._jestCfg.globals && this._jestCfg.globals['ts-jest'] const options = tsJestCfg ?? Object.create(null) // compiler module this.compilerModule = importer.typescript(ImportReasons.TsJest, options.compiler ?? 'typescript') @@ -181,7 +182,6 @@ export class ConfigSet { this.logger.debug({ compilerModule: this.compilerModule }, 'normalized compiler module config via ts-jest option') - this._backportJestCfg() this._setupConfigSet(options) this._resolveTsCacheDir() this._matchablePatterns = [...this._jestCfg.testMatch, ...this._jestCfg.testRegex].filter( @@ -203,12 +203,16 @@ export class ConfigSet { /** * @internal */ - private _backportJestCfg(): void { - const config = backportJestConfig(this.logger, this.jestConfig) + private _backportJestCfg(jestCfg: ProjectConfigTsJest): void { + const config = backportJestConfig(this.logger, jestCfg) this.logger.debug({ jestConfig: config }, 'normalized jest config') - this._jestCfg = config + this._jestCfg = { + ...config, + testMatch: config.testMatch ?? DEFAULT_JEST_TEST_MATCH, + testRegex: config.testRegex ?? [], + } } /** diff --git a/src/ts-jest-transformer.ts b/src/ts-jest-transformer.ts index 463f05e225..4432a2af9f 100644 --- a/src/ts-jest-transformer.ts +++ b/src/ts-jest-transformer.ts @@ -113,7 +113,7 @@ export class TsJestTransformer implements Transformer { } // eslint-disable-next-line class-methods-use-this - protected _createConfigSet(config: ProjectConfigTsJest): ConfigSet { + protected _createConfigSet(config: ProjectConfigTsJest | undefined): ConfigSet { return new ConfigSet(config) } diff --git a/src/utils/logger.ts b/src/utils/logger.ts index 832f0dc789..0e1b0a3cf5 100644 --- a/src/utils/logger.ts +++ b/src/utils/logger.ts @@ -13,9 +13,6 @@ const buildOptions = () => ({ targets: process.env.TS_JEST_LOG || undefined, }) -/** - * @internal - */ export let rootLogger = createLogger(buildOptions()) backportTsJestDebugEnvVar(rootLogger)