From 69488552eca2846f3fc6ba86ab49d7893caaf521 Mon Sep 17 00:00:00 2001 From: Ahn Date: Thu, 9 Jul 2020 16:20:39 +0200 Subject: [PATCH] fix(config): invalidate cache when other options in `tsconfig` change (#1788) --- e2e/__cases__/ts-jest-checks/index.spec.ts | 2 +- .../tsconfig-project-references.json | 7 --- src/__mocks__/tsconfig-src.json | 8 +++- .../__snapshots__/config-set.spec.ts.snap | 42 ++++++++++++----- src/config/config-set.spec.ts | 46 ++++++------------- src/config/config-set.ts | 8 +++- 6 files changed, 59 insertions(+), 54 deletions(-) delete mode 100644 src/__mocks__/tsconfig-project-references.json diff --git a/e2e/__cases__/ts-jest-checks/index.spec.ts b/e2e/__cases__/ts-jest-checks/index.spec.ts index 2ea1f32ce1..8f573a49a3 100644 --- a/e2e/__cases__/ts-jest-checks/index.spec.ts +++ b/e2e/__cases__/ts-jest-checks/index.spec.ts @@ -4,7 +4,7 @@ import { readFileSync } from 'fs' describe('tsConfig', () => { it('should have digest and versions', () => { const tr = createTransformer() - const cs = tr.configsFor({} as any) + const cs = tr.configsFor(Object.create(null)) expect(cs.tsJestDigest).toHaveLength(40) expect(cs.tsJestDigest).toBe(readFileSync(require.resolve('ts-jest/.ts-jest-digest'), 'utf8')) expect(cs.versions['ts-jest']).toBe(require('ts-jest/package.json').version) diff --git a/src/__mocks__/tsconfig-project-references.json b/src/__mocks__/tsconfig-project-references.json deleted file mode 100644 index 6b36a95289..0000000000 --- a/src/__mocks__/tsconfig-project-references.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "references": [ - { - "path": "./tsconfig-src.json" - } - ] -} diff --git a/src/__mocks__/tsconfig-src.json b/src/__mocks__/tsconfig-src.json index f134830d7e..7883a8bb37 100644 --- a/src/__mocks__/tsconfig-src.json +++ b/src/__mocks__/tsconfig-src.json @@ -3,5 +3,11 @@ "composite": true, "declaration": true, "types": [] - } + }, + "include": [ + "bar/**/*" + ], + "exclude": [ + "foo/**/*" + ] } diff --git a/src/config/__snapshots__/config-set.spec.ts.snap b/src/config/__snapshots__/config-set.spec.ts.snap index ac0693e1d5..44a72c7738 100644 --- a/src/config/__snapshots__/config-set.spec.ts.snap +++ b/src/config/__snapshots__/config-set.spec.ts.snap @@ -1,6 +1,6 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -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}}"`; +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/**/*\\"]}}}"`; exports[`isTestFile should return a boolean value whether the file matches test pattern 1`] = `true`; @@ -68,18 +68,38 @@ Object { }, "stringifyContentPathRegex": undefined, "transformers": Array [], - "tsConfig": undefined, + "tsConfig": Object { + "kind": "file", + "value": "", + }, }, "tsconfig": Object { - "configFilePath": undefined, - "declaration": false, - "inlineSourceMap": false, - "inlineSources": true, - "module": 1, - "noEmit": false, - "removeComments": false, - "sourceMap": true, - "target": 1, + "options": Object { + "configFilePath": "", + "declaration": false, + "inlineSourceMap": false, + "inlineSources": true, + "module": 1, + "noEmit": false, + "removeComments": false, + "sourceMap": true, + "target": 1, + "types": Array [], + }, + "raw": Object { + "compileOnSave": false, + "compilerOptions": Object { + "composite": true, + "declaration": true, + "types": Array [], + }, + "exclude": Array [ + "foo/**/*", + ], + "include": Array [ + "bar/**/*", + ], + }, }, } `; diff --git a/src/config/config-set.spec.ts b/src/config/config-set.spec.ts index 9e5a981dcf..eed844839c 100644 --- a/src/config/config-set.spec.ts +++ b/src/config/config-set.spec.ts @@ -417,35 +417,6 @@ describe('tsJest', () => { }) // compiler }) // tsJest -describe('makeDiagnostic', () => { - const cs = createConfigSet() - it('should create diagnostic with defaults', () => { - expect(cs.makeDiagnostic(1234, 'foo is not bar')).toMatchInlineSnapshot(` - Object { - "category": 0, - "code": 1234, - "file": undefined, - "length": undefined, - "messageText": "foo is not bar", - "start": undefined, - } - `) - }) - it('should set category', () => { - expect(cs.makeDiagnostic(4321, 'foo might be bar', { category: ts.DiagnosticCategory.Error })) - .toMatchInlineSnapshot(` - Object { - "category": 1, - "code": 4321, - "file": undefined, - "length": undefined, - "messageText": "foo might be bar", - "start": undefined, - } - `) - }) -}) // makeDiagnostic - describe('typescript', () => { const get = (tsJest?: TsJestGlobalOptions, parentConfig?: TsJestGlobalOptions) => createConfigSet({ tsJestConfig: tsJest, parentConfig }).parsedTsConfig @@ -1045,19 +1016,26 @@ describe('projectDependencies', () => { describe('cacheKey', () => { it('should be a string', () => { const cs = createConfigSet({ - tsJestConfig: { tsConfig: false } as any, + tsJestConfig: { tsConfig: require.resolve('../__mocks__/tsconfig-src.json') }, projectDependencies: { opt: '1.2.3', peer: '1.2.4', dev: '1.2.5', std: '1.2.6', }, + resolve: null, }) // we tested those and don't want the snapshot to change all the time we upgrade const val = cs.jsonValue.value delete val.versions + // we don't need to verify configFilePath and tsConfig value here + val.tsconfig.options.configFilePath = '' + val.tsJest.tsConfig.value = '' cs.jsonValue.value = val - // digest is mocked in src/__mocks__/index.ts + /** + * digest is mocked in src/__mocks__/index.ts + * we don't want to save snapshot with real paths of tsconfig so we replace real path with empty string + */ expect(cs.cacheKey).toMatchSnapshot() }) }) // cacheKey @@ -1065,16 +1043,20 @@ describe('cacheKey', () => { describe('jsonValue', () => { it('should create jsonValue based on each config and version', () => { const cs = createConfigSet({ - tsJestConfig: { tsConfig: false } as any, + tsJestConfig: { tsConfig: require.resolve('../__mocks__/tsconfig-src.json') }, projectDependencies: { 'some-module': '1.2.3', }, + resolve: null, }) const val = cs.jsonValue.valueOf() expect(cs.toJSON()).toEqual(val) // it will change each time we upgrade – we tested those in the `version` block expect(val.versions).toEqual(cs.versions) delete val.versions + // we don't need to verify configFilePath and tsConfig value here + val.tsconfig.options.configFilePath = '' + val.tsJest.tsConfig.value = '' // digest is mocked in src/__mocks__/index.ts expect(val).toMatchSnapshot() diff --git a/src/config/config-set.ts b/src/config/config-set.ts index 7ba4a754bc..e9c9c3c37d 100644 --- a/src/config/config-set.ts +++ b/src/config/config-set.ts @@ -689,7 +689,10 @@ export class ConfigSet { jest, tsJest: this.tsJest, babel: this.babel, - tsconfig: this.parsedTsConfig.options, + tsconfig: { + options: this.parsedTsConfig.options, + raw: this.parsedTsConfig.raw, + }, }) } @@ -699,6 +702,7 @@ export class ConfigSet { get cacheKey(): string { return this.jsonValue.serialized } + readonly logger: Logger /** * @internal @@ -713,7 +717,7 @@ export class ConfigSet { /** * @internal */ - makeDiagnostic( + private makeDiagnostic( code: number, messageText: string, options: { category?: DiagnosticCategory; file?: SourceFile; start?: number; length?: number } = {},