Skip to content

Commit d726016

Browse files
authoredFeb 10, 2021
fix(config): invalidate Jest transform cache when astTransformers value changes (#2345)
1 parent 20dc547 commit d726016

8 files changed

+23
-69
lines changed
 

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

-10
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,6 @@ Object {
5959
"before": Array [
6060
Object {
6161
"factory": [Function],
62-
"name": "hoisting-jest-mock",
63-
"version": 4,
6462
},
6563
],
6664
}
@@ -73,8 +71,6 @@ Object {
7371
"before": Array [
7472
Object {
7573
"factory": [Function],
76-
"name": "hoisting-jest-mock",
77-
"version": 4,
7874
},
7975
Object {
8076
"factory": [Function],
@@ -94,8 +90,6 @@ Object {
9490
"before": Array [
9591
Object {
9692
"factory": [Function],
97-
"name": "hoisting-jest-mock",
98-
"version": 4,
9993
},
10094
],
10195
}
@@ -112,8 +106,6 @@ Object {
112106
"before": Array [
113107
Object {
114108
"factory": [Function],
115-
"name": "hoisting-jest-mock",
116-
"version": 4,
117109
},
118110
],
119111
}
@@ -126,8 +118,6 @@ Object {
126118
"before": Array [
127119
Object {
128120
"factory": [Function],
129-
"name": "hoisting-jest-mock",
130-
"version": 4,
131121
},
132122
Object {
133123
"factory": [Function],

‎src/config/config-set.ts

+21-10
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ export class ConfigSet {
101101
readonly isolatedModules: boolean
102102
readonly cwd: string
103103
readonly rootDir: string
104+
cacheSuffix!: string
104105
tsCacheDir: string | undefined
105106
parsedTsConfig!: ParsedCommandLine | Record<string, any>
106107
resolvedTransformers: TsJestAstTransformer = {
@@ -349,20 +350,30 @@ export class ConfigSet {
349350
* @internal
350351
*/
351352
private _resolveTsCacheDir(): void {
353+
this.cacheSuffix = sha1(
354+
stringify({
355+
version: this.compilerModule.version,
356+
digest: this.tsJestDigest,
357+
babelConfig: this.babelConfig,
358+
compilerModule: this.compilerModule,
359+
tsconfig: {
360+
options: this.parsedTsConfig.options,
361+
raw: this.parsedTsConfig.raw,
362+
},
363+
isolatedModules: this.isolatedModules,
364+
diagnostics: this._diagnostics,
365+
transformers: this.resolvedTransformers,
366+
}),
367+
)
352368
if (!this._jestCfg.cache) {
353369
this.logger.debug('file caching disabled')
354370
} else {
355-
const cacheSuffix = sha1(
356-
stringify({
357-
version: this.compilerModule.version,
358-
digest: this.tsJestDigest,
359-
compilerModule: this.compilerModule,
360-
compilerOptions: this.parsedTsConfig.options,
361-
isolatedModules: this.isolatedModules,
362-
diagnostics: this._diagnostics,
363-
}),
371+
const res = join(
372+
this._jestCfg.cacheDirectory,
373+
'ts-jest',
374+
this.cacheSuffix.substr(0, 2),
375+
this.cacheSuffix.substr(2),
364376
)
365-
const res = join(this._jestCfg.cacheDirectory, 'ts-jest', cacheSuffix.substr(0, 2), cacheSuffix.substr(2))
366377

367378
this.logger.debug({ cacheDirectory: res }, 'will use file caching')
368379

‎src/transformers/README.md

-5
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@ import { SourceFile, TransformationContext, Transformer, Visitor } from 'typescr
99

1010
import type { TsCompilerInstance } from 'ts-jest/dist/types'
1111

12-
// this is a unique identifier for your transformer
13-
export const name = 'my-transformer'
14-
// increment this each time you change the behavior of your transformer
15-
export const version = 1
16-
1712
export function factory(compilerInstance: TsCompilerInstance) {
1813
const ts = compilerInstance.configSet.compilerModule
1914
function createVisitor(ctx: TransformationContext, sf: SourceFile) {

‎src/transformers/hoist-jest.spec.ts

+1-8
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,7 @@ const createFactory = () => hoist.factory(new TsCompiler(createConfigSet(), new
121121
const transpile = (source: string) => ts.transpileModule(source, { transformers: { before: [createFactory()] } })
122122

123123
describe('hoisting', () => {
124-
it('should have correct signature', () => {
125-
expect(hoist.name).toBe('hoisting-jest-mock')
126-
expect(typeof hoist.version).toBe('number')
127-
expect(hoist.version).toBeGreaterThan(0)
128-
expect(typeof hoist.factory).toBe('function')
129-
})
130-
131-
it.each([CODE_WITH_HOISTING_NO_JEST_GLOBALS, CODE_WITH_HOISTING_HAS_JEST_GLOBALS])(
124+
test.each([CODE_WITH_HOISTING_NO_JEST_GLOBALS, CODE_WITH_HOISTING_HAS_JEST_GLOBALS])(
132125
'should hoist correctly jest methods',
133126
(data) => {
134127
const out = transpile(data)

‎src/transformers/hoist-jest.ts

-10
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,6 @@ const HOIST_METHODS = ['mock', 'unmock', 'enableAutomock', 'disableAutomock', 'd
2020
const JEST_GLOBALS_MODULE_NAME = '@jest/globals'
2121
const JEST_GLOBAL_NAME = 'jest'
2222
const ROOT_LEVEL_AST = 1
23-
/**
24-
* @internal
25-
*/
26-
export const name = 'hoisting-jest-mock'
27-
/**
28-
* Please increment this each time the code is modified
29-
*
30-
* @internal
31-
*/
32-
export const version = 4
3323

3424
/**
3525
* The factory of hoisting transformer factory

‎src/transformers/path-mapping.spec.ts

-7
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,6 @@ const TS_JS_CODE_WITH_PATH_ALIAS = `
3030
`
3131

3232
describe('path-mapping', () => {
33-
test('should have correct signature', () => {
34-
expect(pathMapping.name).toBe('path-mapping')
35-
expect(typeof pathMapping.version).toBe('number')
36-
expect(pathMapping.version).toBeGreaterThan(0)
37-
expect(typeof pathMapping.factory).toBe('function')
38-
})
39-
4033
test.each([
4134
{
4235
baseUrl: '.',

‎src/transformers/path-mapping.ts

-10
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,6 @@ import type * as _ts from 'typescript'
1111

1212
import type { TsCompilerInstance } from '../types'
1313

14-
/**
15-
* @internal
16-
*/
17-
export const name = 'path-mapping'
18-
// increment this each time the code is modified
19-
/**
20-
* @internal
21-
*/
22-
export const version = 1
23-
2414
const isBaseDir = (base: string, dir: string) => !relative(base, dir)?.startsWith('.')
2515

2616
/**

‎src/ts-jest-transformer.ts

+1-9
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,7 @@ export class TsJestTransformer implements Transformer {
9090
// this which does not depend on config
9191
jest.name = undefined as any
9292
jest.cacheDirectory = undefined as any
93-
this._transformCfgStr = new JsonableValue({
94-
digest: configSet.tsJestDigest,
95-
babel: configSet.babelConfig,
96-
...jest,
97-
tsconfig: {
98-
options: configSet.parsedTsConfig.options,
99-
raw: configSet.parsedTsConfig.raw,
100-
},
101-
}).serialized
93+
this._transformCfgStr = `${new JsonableValue(jest).serialized}${configSet.cacheSuffix}`
10294
this._compiler = new TsJestCompiler(configSet, cacheFS)
10395
TsJestTransformer._cachedConfigSets.push({
10496
jestConfig: new JsonableValue(config),

0 commit comments

Comments
 (0)
Please sign in to comment.