Skip to content

Commit 6948855

Browse files
authored
fix(config): invalidate cache when other options in tsconfig change (#1788)
1 parent 8d02622 commit 6948855

File tree

6 files changed

+59
-54
lines changed

6 files changed

+59
-54
lines changed

e2e/__cases__/ts-jest-checks/index.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { readFileSync } from 'fs'
44
describe('tsConfig', () => {
55
it('should have digest and versions', () => {
66
const tr = createTransformer()
7-
const cs = tr.configsFor({} as any)
7+
const cs = tr.configsFor(Object.create(null))
88
expect(cs.tsJestDigest).toHaveLength(40)
99
expect(cs.tsJestDigest).toBe(readFileSync(require.resolve('ts-jest/.ts-jest-digest'), 'utf8'))
1010
expect(cs.versions['ts-jest']).toBe(require('ts-jest/package.json').version)

src/__mocks__/tsconfig-project-references.json

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/__mocks__/tsconfig-src.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,11 @@
33
"composite": true,
44
"declaration": true,
55
"types": []
6-
}
6+
},
7+
"include": [
8+
"bar/**/*"
9+
],
10+
"exclude": [
11+
"foo/**/*"
12+
]
713
}

src/config/__snapshots__/config-set.spec.ts.snap

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`cacheKey should be a string 1`] = `"{\\"digest\\":\\"a0d51ca854194df8191d0e65c0ca4730f510f332\\",\\"jest\\":{\\"__backported\\":true,\\"globals\\":{}},\\"projectDepVersions\\":{\\"dev\\":\\"1.2.5\\",\\"opt\\":\\"1.2.3\\",\\"peer\\":\\"1.2.4\\",\\"std\\":\\"1.2.6\\"},\\"transformers\\":[\\"hoisting-jest-mock@1\\"],\\"tsJest\\":{\\"compiler\\":\\"typescript\\",\\"diagnostics\\":{\\"ignoreCodes\\":[6059,18002,18003],\\"pretty\\":true,\\"throws\\":true},\\"isolatedModules\\":false,\\"packageJson\\":{\\"kind\\":\\"file\\"},\\"transformers\\":[]},\\"tsconfig\\":{\\"declaration\\":false,\\"inlineSourceMap\\":false,\\"inlineSources\\":true,\\"module\\":1,\\"noEmit\\":false,\\"removeComments\\":false,\\"sourceMap\\":true,\\"target\\":1}}"`;
3+
exports[`cacheKey should be a string 1`] = `"{\\"digest\\":\\"a0d51ca854194df8191d0e65c0ca4730f510f332\\",\\"jest\\":{\\"__backported\\":true,\\"globals\\":{}},\\"projectDepVersions\\":{\\"dev\\":\\"1.2.5\\",\\"opt\\":\\"1.2.3\\",\\"peer\\":\\"1.2.4\\",\\"std\\":\\"1.2.6\\"},\\"transformers\\":[\\"hoisting-jest-mock@1\\"],\\"tsJest\\":{\\"compiler\\":\\"typescript\\",\\"diagnostics\\":{\\"ignoreCodes\\":[6059,18002,18003],\\"pretty\\":true,\\"throws\\":true},\\"isolatedModules\\":false,\\"packageJson\\":{\\"kind\\":\\"file\\"},\\"transformers\\":[],\\"tsConfig\\":{\\"kind\\":\\"file\\",\\"value\\":\\"\\"}},\\"tsconfig\\":{\\"options\\":{\\"configFilePath\\":\\"\\",\\"declaration\\":false,\\"inlineSourceMap\\":false,\\"inlineSources\\":true,\\"module\\":1,\\"noEmit\\":false,\\"removeComments\\":false,\\"sourceMap\\":true,\\"target\\":1,\\"types\\":[]},\\"raw\\":{\\"compileOnSave\\":false,\\"compilerOptions\\":{\\"composite\\":true,\\"declaration\\":true,\\"types\\":[]},\\"exclude\\":[\\"foo/**/*\\"],\\"include\\":[\\"bar/**/*\\"]}}}"`;
44

55
exports[`isTestFile should return a boolean value whether the file matches test pattern 1`] = `true`;
66

@@ -68,18 +68,38 @@ Object {
6868
},
6969
"stringifyContentPathRegex": undefined,
7070
"transformers": Array [],
71-
"tsConfig": undefined,
71+
"tsConfig": Object {
72+
"kind": "file",
73+
"value": "",
74+
},
7275
},
7376
"tsconfig": Object {
74-
"configFilePath": undefined,
75-
"declaration": false,
76-
"inlineSourceMap": false,
77-
"inlineSources": true,
78-
"module": 1,
79-
"noEmit": false,
80-
"removeComments": false,
81-
"sourceMap": true,
82-
"target": 1,
77+
"options": Object {
78+
"configFilePath": "",
79+
"declaration": false,
80+
"inlineSourceMap": false,
81+
"inlineSources": true,
82+
"module": 1,
83+
"noEmit": false,
84+
"removeComments": false,
85+
"sourceMap": true,
86+
"target": 1,
87+
"types": Array [],
88+
},
89+
"raw": Object {
90+
"compileOnSave": false,
91+
"compilerOptions": Object {
92+
"composite": true,
93+
"declaration": true,
94+
"types": Array [],
95+
},
96+
"exclude": Array [
97+
"foo/**/*",
98+
],
99+
"include": Array [
100+
"bar/**/*",
101+
],
102+
},
83103
},
84104
}
85105
`;

src/config/config-set.spec.ts

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -417,35 +417,6 @@ describe('tsJest', () => {
417417
}) // compiler
418418
}) // tsJest
419419

420-
describe('makeDiagnostic', () => {
421-
const cs = createConfigSet()
422-
it('should create diagnostic with defaults', () => {
423-
expect(cs.makeDiagnostic(1234, 'foo is not bar')).toMatchInlineSnapshot(`
424-
Object {
425-
"category": 0,
426-
"code": 1234,
427-
"file": undefined,
428-
"length": undefined,
429-
"messageText": "foo is not bar",
430-
"start": undefined,
431-
}
432-
`)
433-
})
434-
it('should set category', () => {
435-
expect(cs.makeDiagnostic(4321, 'foo might be bar', { category: ts.DiagnosticCategory.Error }))
436-
.toMatchInlineSnapshot(`
437-
Object {
438-
"category": 1,
439-
"code": 4321,
440-
"file": undefined,
441-
"length": undefined,
442-
"messageText": "foo might be bar",
443-
"start": undefined,
444-
}
445-
`)
446-
})
447-
}) // makeDiagnostic
448-
449420
describe('typescript', () => {
450421
const get = (tsJest?: TsJestGlobalOptions, parentConfig?: TsJestGlobalOptions) =>
451422
createConfigSet({ tsJestConfig: tsJest, parentConfig }).parsedTsConfig
@@ -1045,36 +1016,47 @@ describe('projectDependencies', () => {
10451016
describe('cacheKey', () => {
10461017
it('should be a string', () => {
10471018
const cs = createConfigSet({
1048-
tsJestConfig: { tsConfig: false } as any,
1019+
tsJestConfig: { tsConfig: require.resolve('../__mocks__/tsconfig-src.json') },
10491020
projectDependencies: {
10501021
opt: '1.2.3',
10511022
peer: '1.2.4',
10521023
dev: '1.2.5',
10531024
std: '1.2.6',
10541025
},
1026+
resolve: null,
10551027
})
10561028
// we tested those and don't want the snapshot to change all the time we upgrade
10571029
const val = cs.jsonValue.value
10581030
delete val.versions
1031+
// we don't need to verify configFilePath and tsConfig value here
1032+
val.tsconfig.options.configFilePath = ''
1033+
val.tsJest.tsConfig.value = ''
10591034
cs.jsonValue.value = val
1060-
// digest is mocked in src/__mocks__/index.ts
1035+
/**
1036+
* digest is mocked in src/__mocks__/index.ts
1037+
* we don't want to save snapshot with real paths of tsconfig so we replace real path with empty string
1038+
*/
10611039
expect(cs.cacheKey).toMatchSnapshot()
10621040
})
10631041
}) // cacheKey
10641042

10651043
describe('jsonValue', () => {
10661044
it('should create jsonValue based on each config and version', () => {
10671045
const cs = createConfigSet({
1068-
tsJestConfig: { tsConfig: false } as any,
1046+
tsJestConfig: { tsConfig: require.resolve('../__mocks__/tsconfig-src.json') },
10691047
projectDependencies: {
10701048
'some-module': '1.2.3',
10711049
},
1050+
resolve: null,
10721051
})
10731052
const val = cs.jsonValue.valueOf()
10741053
expect(cs.toJSON()).toEqual(val)
10751054
// it will change each time we upgrade – we tested those in the `version` block
10761055
expect(val.versions).toEqual(cs.versions)
10771056
delete val.versions
1057+
// we don't need to verify configFilePath and tsConfig value here
1058+
val.tsconfig.options.configFilePath = ''
1059+
val.tsJest.tsConfig.value = ''
10781060

10791061
// digest is mocked in src/__mocks__/index.ts
10801062
expect(val).toMatchSnapshot()

src/config/config-set.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,10 @@ export class ConfigSet {
689689
jest,
690690
tsJest: this.tsJest,
691691
babel: this.babel,
692-
tsconfig: this.parsedTsConfig.options,
692+
tsconfig: {
693+
options: this.parsedTsConfig.options,
694+
raw: this.parsedTsConfig.raw,
695+
},
693696
})
694697
}
695698

@@ -699,6 +702,7 @@ export class ConfigSet {
699702
get cacheKey(): string {
700703
return this.jsonValue.serialized
701704
}
705+
702706
readonly logger: Logger
703707
/**
704708
* @internal
@@ -713,7 +717,7 @@ export class ConfigSet {
713717
/**
714718
* @internal
715719
*/
716-
makeDiagnostic(
720+
private makeDiagnostic(
717721
code: number,
718722
messageText: string,
719723
options: { category?: DiagnosticCategory; file?: SourceFile; start?: number; length?: number } = {},

0 commit comments

Comments
 (0)