Skip to content

Commit

Permalink
fix(config): invalidate Jest transform cache when astTransformers v…
Browse files Browse the repository at this point in the history
…alue changes (#2345)
  • Loading branch information
ahnpnl authored Feb 10, 2021
1 parent 20dc547 commit d726016
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 69 deletions.
10 changes: 0 additions & 10 deletions src/config/__snapshots__/config-set.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ Object {
"before": Array [
Object {
"factory": [Function],
"name": "hoisting-jest-mock",
"version": 4,
},
],
}
Expand All @@ -73,8 +71,6 @@ Object {
"before": Array [
Object {
"factory": [Function],
"name": "hoisting-jest-mock",
"version": 4,
},
Object {
"factory": [Function],
Expand All @@ -94,8 +90,6 @@ Object {
"before": Array [
Object {
"factory": [Function],
"name": "hoisting-jest-mock",
"version": 4,
},
],
}
Expand All @@ -112,8 +106,6 @@ Object {
"before": Array [
Object {
"factory": [Function],
"name": "hoisting-jest-mock",
"version": 4,
},
],
}
Expand All @@ -126,8 +118,6 @@ Object {
"before": Array [
Object {
"factory": [Function],
"name": "hoisting-jest-mock",
"version": 4,
},
Object {
"factory": [Function],
Expand Down
31 changes: 21 additions & 10 deletions src/config/config-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ export class ConfigSet {
readonly isolatedModules: boolean
readonly cwd: string
readonly rootDir: string
cacheSuffix!: string
tsCacheDir: string | undefined
parsedTsConfig!: ParsedCommandLine | Record<string, any>
resolvedTransformers: TsJestAstTransformer = {
Expand Down Expand Up @@ -349,20 +350,30 @@ export class ConfigSet {
* @internal
*/
private _resolveTsCacheDir(): void {
this.cacheSuffix = sha1(
stringify({
version: this.compilerModule.version,
digest: this.tsJestDigest,
babelConfig: this.babelConfig,
compilerModule: this.compilerModule,
tsconfig: {
options: this.parsedTsConfig.options,
raw: this.parsedTsConfig.raw,
},
isolatedModules: this.isolatedModules,
diagnostics: this._diagnostics,
transformers: this.resolvedTransformers,
}),
)
if (!this._jestCfg.cache) {
this.logger.debug('file caching disabled')
} else {
const cacheSuffix = sha1(
stringify({
version: this.compilerModule.version,
digest: this.tsJestDigest,
compilerModule: this.compilerModule,
compilerOptions: this.parsedTsConfig.options,
isolatedModules: this.isolatedModules,
diagnostics: this._diagnostics,
}),
const res = join(
this._jestCfg.cacheDirectory,
'ts-jest',
this.cacheSuffix.substr(0, 2),
this.cacheSuffix.substr(2),
)
const res = join(this._jestCfg.cacheDirectory, 'ts-jest', cacheSuffix.substr(0, 2), cacheSuffix.substr(2))

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

Expand Down
5 changes: 0 additions & 5 deletions src/transformers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@ import { SourceFile, TransformationContext, Transformer, Visitor } from 'typescr

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

// this is a unique identifier for your transformer
export const name = 'my-transformer'
// increment this each time you change the behavior of your transformer
export const version = 1

export function factory(compilerInstance: TsCompilerInstance) {
const ts = compilerInstance.configSet.compilerModule
function createVisitor(ctx: TransformationContext, sf: SourceFile) {
Expand Down
9 changes: 1 addition & 8 deletions src/transformers/hoist-jest.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,7 @@ const createFactory = () => hoist.factory(new TsCompiler(createConfigSet(), new
const transpile = (source: string) => ts.transpileModule(source, { transformers: { before: [createFactory()] } })

describe('hoisting', () => {
it('should have correct signature', () => {
expect(hoist.name).toBe('hoisting-jest-mock')
expect(typeof hoist.version).toBe('number')
expect(hoist.version).toBeGreaterThan(0)
expect(typeof hoist.factory).toBe('function')
})

it.each([CODE_WITH_HOISTING_NO_JEST_GLOBALS, CODE_WITH_HOISTING_HAS_JEST_GLOBALS])(
test.each([CODE_WITH_HOISTING_NO_JEST_GLOBALS, CODE_WITH_HOISTING_HAS_JEST_GLOBALS])(
'should hoist correctly jest methods',
(data) => {
const out = transpile(data)
Expand Down
10 changes: 0 additions & 10 deletions src/transformers/hoist-jest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,6 @@ const HOIST_METHODS = ['mock', 'unmock', 'enableAutomock', 'disableAutomock', 'd
const JEST_GLOBALS_MODULE_NAME = '@jest/globals'
const JEST_GLOBAL_NAME = 'jest'
const ROOT_LEVEL_AST = 1
/**
* @internal
*/
export const name = 'hoisting-jest-mock'
/**
* Please increment this each time the code is modified
*
* @internal
*/
export const version = 4

/**
* The factory of hoisting transformer factory
Expand Down
7 changes: 0 additions & 7 deletions src/transformers/path-mapping.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,6 @@ const TS_JS_CODE_WITH_PATH_ALIAS = `
`

describe('path-mapping', () => {
test('should have correct signature', () => {
expect(pathMapping.name).toBe('path-mapping')
expect(typeof pathMapping.version).toBe('number')
expect(pathMapping.version).toBeGreaterThan(0)
expect(typeof pathMapping.factory).toBe('function')
})

test.each([
{
baseUrl: '.',
Expand Down
10 changes: 0 additions & 10 deletions src/transformers/path-mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,6 @@ import type * as _ts from 'typescript'

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

/**
* @internal
*/
export const name = 'path-mapping'
// increment this each time the code is modified
/**
* @internal
*/
export const version = 1

const isBaseDir = (base: string, dir: string) => !relative(base, dir)?.startsWith('.')

/**
Expand Down
10 changes: 1 addition & 9 deletions src/ts-jest-transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,7 @@ export class TsJestTransformer implements Transformer {
// this which does not depend on config
jest.name = undefined as any
jest.cacheDirectory = undefined as any
this._transformCfgStr = new JsonableValue({
digest: configSet.tsJestDigest,
babel: configSet.babelConfig,
...jest,
tsconfig: {
options: configSet.parsedTsConfig.options,
raw: configSet.parsedTsConfig.raw,
},
}).serialized
this._transformCfgStr = `${new JsonableValue(jest).serialized}${configSet.cacheSuffix}`
this._compiler = new TsJestCompiler(configSet, cacheFS)
TsJestTransformer._cachedConfigSets.push({
jestConfig: new JsonableValue(config),
Expand Down

0 comments on commit d726016

Please sign in to comment.