Skip to content

Commit c6d6e4c

Browse files
authored
fix(config): create fallback jest config when jest config is undefined (#2433)
Closes #2085
1 parent 1d411f2 commit c6d6e4c

File tree

6 files changed

+135
-129
lines changed

6 files changed

+135
-129
lines changed

e2e/__tests__/__snapshots__/logger.test.ts.snap

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ Array [
99
"[level:20] created new transformer",
1010
"[level:20] created new transformer",
1111
"[level:30] no matching config-set found, creating a new one",
12+
"[level:20] backporting config",
13+
"[level:20] normalized jest config",
1214
"[level:20] loaded module typescript",
1315
"[level:20] patching typescript",
1416
"[level:20] checking version of typescript: OK",
1517
"[level:20] normalized compiler module config via ts-jest option",
16-
"[level:20] backporting config",
17-
"[level:20] normalized jest config",
1818
"[level:20] babel is disabled",
1919
"[level:20] normalized diagnostics config via ts-jest option",
2020
"[level:20] readTsConfig(): reading <cwd>/tsconfig.json",
@@ -46,12 +46,12 @@ Array [
4646
"[level:20] created new transformer",
4747
"[level:20] created new transformer",
4848
"[level:30] no matching config-set found, creating a new one",
49+
"[level:20] backporting config",
50+
"[level:20] normalized jest config",
4951
"[level:20] loaded module typescript",
5052
"[level:20] patching typescript",
5153
"[level:20] checking version of typescript: OK",
5254
"[level:20] normalized compiler module config via ts-jest option",
53-
"[level:20] backporting config",
54-
"[level:20] normalized jest config",
5555
"[level:20] normalized babel config via ts-jest option",
5656
"[level:20] loaded module babel-jest",
5757
"[level:20] patching babel-jest",
@@ -89,12 +89,12 @@ Array [
8989
"[level:20] created new transformer",
9090
"[level:20] created new transformer",
9191
"[level:30] no matching config-set found, creating a new one",
92+
"[level:20] backporting config",
93+
"[level:20] normalized jest config",
9294
"[level:20] loaded module typescript",
9395
"[level:20] patching typescript",
9496
"[level:20] checking version of typescript: OK",
9597
"[level:20] normalized compiler module config via ts-jest option",
96-
"[level:20] backporting config",
97-
"[level:20] normalized jest config",
9898
"[level:20] resolved path from babel.config.js to <cwd>/babel.config.js",
9999
"[level:20] normalized babel config via ts-jest option",
100100
"[level:20] loaded module babel-jest",

src/cli/cli.spec.ts

Lines changed: 111 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -307,30 +307,30 @@ Jest configuration written to "${normalize('/foo/bar/package.json')}".
307307
)
308308
const res = await runCli(...noOption, pkgPaths.current)
309309
expect(res).toMatchInlineSnapshot(`
310-
Object {
311-
"exitCode": 0,
312-
"log": "",
313-
"stderr": "
314-
Migrated Jest configuration:
315-
316-
317-
Detected preset 'default' as the best matching preset for your configuration.
318-
Visit https://kulshekhar.github.io/ts-jest/user/config/#jest-preset for more information about presets.
319-
320-
",
321-
"stdout": "\\"jest\\": {
322-
\\"globals\\": {
323-
\\"ts-jest\\": {
324-
\\"tsconfig\\": {
325-
\\"target\\": \\"es6\\"
326-
}
327-
}
328-
},
329-
\\"preset\\": \\"ts-jest\\"
330-
}
331-
",
332-
}
333-
`)
310+
Object {
311+
"exitCode": 0,
312+
"log": "",
313+
"stderr": "
314+
Migrated Jest configuration:
315+
316+
317+
Detected preset 'default' as the best matching preset for your configuration.
318+
Visit https://kulshekhar.github.io/ts-jest/user/config/#jest-preset for more information about presets.
319+
320+
",
321+
"stdout": "\\"jest\\": {
322+
\\"globals\\": {
323+
\\"ts-jest\\": {
324+
\\"tsconfig\\": {
325+
\\"target\\": \\"es6\\"
326+
}
327+
}
328+
},
329+
\\"preset\\": \\"ts-jest\\"
330+
}
331+
",
332+
}
333+
`)
334334
expect(fs.writeFileSync).not.toHaveBeenCalled()
335335
})
336336

@@ -346,24 +346,24 @@ Jest configuration written to "${normalize('/foo/bar/package.json')}".
346346
)
347347
const res = await runCli(...fullOptions, pkgPaths.current)
348348
expect(res).toMatchInlineSnapshot(`
349-
Object {
350-
"exitCode": 0,
351-
"log": "",
352-
"stderr": "
353-
Migrated Jest configuration:
354-
",
355-
"stdout": "\\"jest\\": {
356-
\\"globals\\": {
357-
\\"ts-jest\\": {
358-
\\"tsconfig\\": {
359-
\\"target\\": \\"es6\\"
360-
}
361-
}
362-
}
363-
}
364-
",
365-
}
366-
`)
349+
Object {
350+
"exitCode": 0,
351+
"log": "",
352+
"stderr": "
353+
Migrated Jest configuration:
354+
",
355+
"stdout": "\\"jest\\": {
356+
\\"globals\\": {
357+
\\"ts-jest\\": {
358+
\\"tsconfig\\": {
359+
\\"target\\": \\"es6\\"
360+
}
361+
}
362+
}
363+
}
364+
",
365+
}
366+
`)
367367
expect(fs.writeFileSync).not.toHaveBeenCalled()
368368
})
369369

@@ -388,29 +388,29 @@ Jest configuration written to "${normalize('/foo/bar/package.json')}".
388388
)
389389
const res = await runCli(...noOption, pkgPaths.current)
390390
expect(res.stdout).toMatchInlineSnapshot(`
391-
"\\"jest\\": {
392-
\\"globals\\": {
393-
\\"ts-jest\\": {
394-
\\"tsconfig\\": {
395-
\\"target\\": \\"es6\\"
396-
}
397-
}
398-
},
399-
\\"moduleFileExtensions\\": [
400-
\\"js\\",
401-
\\"ts\\",
402-
\\"tsx\\"
403-
],
404-
\\"testMatch\\": [
405-
\\"**/?(*.)+(spec|test).js?(x)\\",
406-
\\"**/?(*.)+(spec|test).ts?(x)\\",
407-
\\"**/__tests__/**/*.js?(x)\\",
408-
\\"**/__tests__/**/*.ts?(x)\\"
409-
],
410-
\\"preset\\": \\"ts-jest\\"
411-
}
412-
"
413-
`)
391+
"\\"jest\\": {
392+
\\"globals\\": {
393+
\\"ts-jest\\": {
394+
\\"tsconfig\\": {
395+
\\"target\\": \\"es6\\"
396+
}
397+
}
398+
},
399+
\\"moduleFileExtensions\\": [
400+
\\"js\\",
401+
\\"ts\\",
402+
\\"tsx\\"
403+
],
404+
\\"testMatch\\": [
405+
\\"**/?(*.)+(spec|test).js?(x)\\",
406+
\\"**/?(*.)+(spec|test).ts?(x)\\",
407+
\\"**/__tests__/**/*.js?(x)\\",
408+
\\"**/__tests__/**/*.ts?(x)\\"
409+
],
410+
\\"preset\\": \\"ts-jest\\"
411+
}
412+
"
413+
`)
414414
})
415415

416416
it('should reset testMatch if testRegex is used', async () => {
@@ -427,13 +427,13 @@ Jest configuration written to "${normalize('/foo/bar/package.json')}".
427427
)
428428
const res = await runCli(...noOption, pkgPaths.current)
429429
expect(res.stdout).toMatchInlineSnapshot(`
430-
"\\"jest\\": {
431-
\\"testRegex\\": \\"foo-pattern\\",
432-
\\"preset\\": \\"ts-jest\\",
433-
\\"testMatch\\": null
434-
}
435-
"
436-
`)
430+
"\\"jest\\": {
431+
\\"testRegex\\": \\"foo-pattern\\",
432+
\\"preset\\": \\"ts-jest\\",
433+
\\"testMatch\\": null
434+
}
435+
"
436+
`)
437437
})
438438

439439
it('should detect best preset', async () => {
@@ -445,43 +445,43 @@ Jest configuration written to "${normalize('/foo/bar/package.json')}".
445445
jest.doMock(pkgPaths.nextCfg, () => ({}), { virtual: true })
446446
let res = await runCli(...noOption, pkgPaths.currentCfg)
447447
expect(res.stdout).toMatchInlineSnapshot(`
448-
"module.exports = {
449-
preset: 'ts-jest',
450-
}
451-
"
452-
`)
448+
"module.exports = {
449+
preset: 'ts-jest',
450+
}
451+
"
452+
`)
453453

454454
// js-with-ts from args
455455
jest.doMock(pkgPaths.nextCfg, () => ({}), { virtual: true })
456456
res = await runCli(...noOption, '--allow-js', pkgPaths.currentCfg)
457457
expect(res.stdout).toMatchInlineSnapshot(`
458-
"module.exports = {
459-
preset: 'ts-jest/presets/js-with-ts',
460-
}
461-
"
462-
`)
458+
"module.exports = {
459+
preset: 'ts-jest/presets/js-with-ts',
460+
}
461+
"
462+
`)
463463

464464
// js-with-ts from previous transform
465465
jest.doMock(pkgPaths.nextCfg, () => ({ transform: { '^.+\\.[tj]sx?$': 'ts-jest' } }), { virtual: true })
466466
res = await runCli(...noOption, pkgPaths.currentCfg)
467467
expect(res.stdout).toMatchInlineSnapshot(`
468-
"module.exports = {
469-
preset: 'ts-jest/presets/js-with-ts',
470-
}
471-
"
472-
`)
468+
"module.exports = {
469+
preset: 'ts-jest/presets/js-with-ts',
470+
}
471+
"
472+
`)
473473

474474
// js-with-babel from previous transform
475475
jest.doMock(pkgPaths.nextCfg, () => ({ transform: { '^.+\\.jsx?$': 'babel-jest', '^.+\\.tsx?$': 'ts-jest' } }), {
476476
virtual: true,
477477
})
478478
res = await runCli(...noOption, pkgPaths.currentCfg)
479479
expect(res.stdout).toMatchInlineSnapshot(`
480-
"module.exports = {
481-
preset: 'ts-jest/presets/js-with-babel',
482-
}
483-
"
484-
`)
480+
"module.exports = {
481+
preset: 'ts-jest/presets/js-with-babel',
482+
}
483+
"
484+
`)
485485

486486
// defaults when previous transform is ambiguous
487487
jest.doMock(
@@ -491,15 +491,15 @@ Jest configuration written to "${normalize('/foo/bar/package.json')}".
491491
)
492492
res = await runCli(...noOption, pkgPaths.currentCfg)
493493
expect(res.stdout).toMatchInlineSnapshot(`
494-
"module.exports = {
495-
transform: {
496-
'^src/js/.+\\\\\\\\.jsx?$': 'babel-jest',
497-
'^src/ts/.+\\\\\\\\.tsx?$': 'ts-jest',
498-
},
499-
preset: 'ts-jest',
500-
}
501-
"
502-
`)
494+
"module.exports = {
495+
transform: {
496+
'^src/js/.+\\\\\\\\.jsx?$': 'babel-jest',
497+
'^src/ts/.+\\\\\\\\.tsx?$': 'ts-jest',
498+
},
499+
preset: 'ts-jest',
500+
}
501+
"
502+
`)
503503
})
504504

505505
it('should normalize transform values', async () => {
@@ -520,16 +520,16 @@ Jest configuration written to "${normalize('/foo/bar/package.json')}".
520520
)
521521
const res = await runCli(...noOption, pkgPaths.current)
522522
expect(res.stdout).toMatchInlineSnapshot(`
523-
"\\"jest\\": {
524-
\\"transform\\": {
525-
\\"<rootDir>/src/.+\\\\\\\\.[jt]s$\\": \\"ts-jest\\",
526-
\\"foo\\\\\\\\.ts\\": \\"ts-jest\\",
527-
\\"bar\\\\\\\\.ts\\": \\"ts-jest\\"
528-
},
529-
\\"preset\\": \\"ts-jest\\"
530-
}
531-
"
532-
`)
523+
"\\"jest\\": {
524+
\\"transform\\": {
525+
\\"<rootDir>/src/.+\\\\\\\\.[jt]s$\\": \\"ts-jest\\",
526+
\\"foo\\\\\\\\.ts\\": \\"ts-jest\\",
527+
\\"bar\\\\\\\\.ts\\": \\"ts-jest\\"
528+
},
529+
\\"preset\\": \\"ts-jest\\"
530+
}
531+
"
532+
`)
533533
})
534534

535535
it('should output help', async () => {

src/config/config-set.spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ beforeEach(() => {
3030
jest.clearAllMocks()
3131
})
3232

33+
test('should create a default fallback jest config when jest config is undefined', () => {
34+
// @ts-expect-error testing purpose
35+
expect(new ConfigSet(undefined)._jestCfg).toBeDefined()
36+
})
37+
3338
describe('parsedTsConfig', () => {
3439
const get = (tsJest?: TsJestGlobalOptions) => createConfigSet({ tsJestConfig: tsJest }).parsedTsConfig
3540

src/config/config-set.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -166,13 +166,14 @@ export class ConfigSet {
166166
tsBuildInfoFile: undefined,
167167
}
168168

169-
constructor(readonly jestConfig: ProjectConfigTsJest, readonly parentLogger?: Logger) {
169+
constructor(jestConfig: ProjectConfigTsJest | undefined, readonly parentLogger?: Logger) {
170170
this.logger = this.parentLogger
171171
? this.parentLogger.child({ [LogContexts.namespace]: 'config' })
172172
: rootLogger.child({ namespace: 'config' })
173-
this.cwd = normalize(this.jestConfig.cwd ?? process.cwd())
174-
this.rootDir = normalize(this.jestConfig.rootDir ?? this.cwd)
175-
const tsJestCfg = this.jestConfig.globals && this.jestConfig.globals['ts-jest']
173+
this._backportJestCfg(jestConfig ?? Object.create(null))
174+
this.cwd = normalize(this._jestCfg.cwd ?? process.cwd())
175+
this.rootDir = normalize(this._jestCfg.rootDir ?? this.cwd)
176+
const tsJestCfg = this._jestCfg.globals && this._jestCfg.globals['ts-jest']
176177
const options = tsJestCfg ?? Object.create(null)
177178
// compiler module
178179
this.compilerModule = importer.typescript(ImportReasons.TsJest, options.compiler ?? 'typescript')
@@ -181,7 +182,6 @@ export class ConfigSet {
181182

182183
this.logger.debug({ compilerModule: this.compilerModule }, 'normalized compiler module config via ts-jest option')
183184

184-
this._backportJestCfg()
185185
this._setupConfigSet(options)
186186
this._resolveTsCacheDir()
187187
this._matchablePatterns = [...this._jestCfg.testMatch, ...this._jestCfg.testRegex].filter(
@@ -203,12 +203,16 @@ export class ConfigSet {
203203
/**
204204
* @internal
205205
*/
206-
private _backportJestCfg(): void {
207-
const config = backportJestConfig(this.logger, this.jestConfig)
206+
private _backportJestCfg(jestCfg: ProjectConfigTsJest): void {
207+
const config = backportJestConfig(this.logger, jestCfg)
208208

209209
this.logger.debug({ jestConfig: config }, 'normalized jest config')
210210

211-
this._jestCfg = config
211+
this._jestCfg = {
212+
...config,
213+
testMatch: config.testMatch ?? DEFAULT_JEST_TEST_MATCH,
214+
testRegex: config.testRegex ?? [],
215+
}
212216
}
213217

214218
/**

0 commit comments

Comments
 (0)