Skip to content

Commit

Permalink
fix(config): create fallback jest config when jest config is `undefin…
Browse files Browse the repository at this point in the history
…ed` (#2433)

Closes #2085
  • Loading branch information
ahnpnl authored Mar 9, 2021
1 parent 1d411f2 commit c6d6e4c
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 129 deletions.
12 changes: 6 additions & 6 deletions e2e/__tests__/__snapshots__/logger.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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 <cwd>/tsconfig.json",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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 <cwd>/babel.config.js",
"[level:20] normalized babel config via ts-jest option",
"[level:20] loaded module babel-jest",
Expand Down
222 changes: 111 additions & 111 deletions src/cli/cli.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})

Expand All @@ -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()
})

Expand All @@ -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 () => {
Expand All @@ -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 () => {
Expand All @@ -445,43 +445,43 @@ 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' } }), {
virtual: true,
})
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(
Expand All @@ -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 () => {
Expand All @@ -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\\": {
\\"<rootDir>/src/.+\\\\\\\\.[jt]s$\\": \\"ts-jest\\",
\\"foo\\\\\\\\.ts\\": \\"ts-jest\\",
\\"bar\\\\\\\\.ts\\": \\"ts-jest\\"
},
\\"preset\\": \\"ts-jest\\"
}
"
`)
"\\"jest\\": {
\\"transform\\": {
\\"<rootDir>/src/.+\\\\\\\\.[jt]s$\\": \\"ts-jest\\",
\\"foo\\\\\\\\.ts\\": \\"ts-jest\\",
\\"bar\\\\\\\\.ts\\": \\"ts-jest\\"
},
\\"preset\\": \\"ts-jest\\"
}
"
`)
})

it('should output help', async () => {
Expand Down
5 changes: 5 additions & 0 deletions src/config/config-set.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
20 changes: 12 additions & 8 deletions src/config/config-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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(
Expand All @@ -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 ?? [],
}
}

/**
Expand Down
Loading

0 comments on commit c6d6e4c

Please sign in to comment.