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(config): create fallback jest config when jest config is undefined #2433

Merged
merged 1 commit into from Mar 9, 2021
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
12 changes: 6 additions & 6 deletions e2e/__tests__/__snapshots__/logger.test.ts.snap
Expand Up @@ -8,12 +8,12 @@ Array [
"[level:20] checking version of jest: OK",
"[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 @@ -44,12 +44,12 @@ Array [
"[level:20] checking version of jest: OK",
"[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 @@ -86,12 +86,12 @@ Array [
"[level:20] checking version of jest: OK",
"[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
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
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
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