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): invalidate cache when other options in tsconfig change #1788

Merged
merged 3 commits into from Jul 9, 2020
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
2 changes: 1 addition & 1 deletion e2e/__cases__/ts-jest-checks/index.spec.ts
Expand Up @@ -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)
Expand Down
7 changes: 0 additions & 7 deletions src/__mocks__/tsconfig-project-references.json

This file was deleted.

8 changes: 7 additions & 1 deletion src/__mocks__/tsconfig-src.json
Expand Up @@ -3,5 +3,11 @@
"composite": true,
"declaration": true,
"types": []
}
},
"include": [
"bar/**/*"
],
"exclude": [
"foo/**/*"
]
}
42 changes: 31 additions & 11 deletions 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`;

Expand Down Expand Up @@ -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/**/*",
],
},
},
}
`;
Expand Down
46 changes: 14 additions & 32 deletions src/config/config-set.spec.ts
Expand Up @@ -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
Expand Down Expand Up @@ -1045,36 +1016,47 @@ 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

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()
Expand Down
8 changes: 6 additions & 2 deletions src/config/config-set.ts
Expand Up @@ -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,
},
})
}

Expand All @@ -699,6 +702,7 @@ export class ConfigSet {
get cacheKey(): string {
return this.jsonValue.serialized
}

readonly logger: Logger
/**
* @internal
Expand All @@ -713,7 +717,7 @@ export class ConfigSet {
/**
* @internal
*/
makeDiagnostic(
private makeDiagnostic(
code: number,
messageText: string,
options: { category?: DiagnosticCategory; file?: SourceFile; start?: number; length?: number } = {},
Expand Down