From c0b37b84bad01d8b0c5ca662bb6cf7cb24414eef Mon Sep 17 00:00:00 2001 From: Ahn Date: Tue, 9 Feb 2021 15:54:44 +0100 Subject: [PATCH 1/2] feat(config): add `astTransformers` and `tsCacheDir` to cache key --- src/config/__snapshots__/config-set.spec.ts.snap | 10 ---------- src/config/config-set.ts | 7 ++++++- src/transformers/README.md | 5 ----- src/transformers/hoist-jest.spec.ts | 9 +-------- src/transformers/hoist-jest.ts | 10 ---------- src/transformers/path-mapping.spec.ts | 7 ------- src/transformers/path-mapping.ts | 10 ---------- src/ts-jest-transformer.ts | 2 ++ 8 files changed, 9 insertions(+), 51 deletions(-) diff --git a/src/config/__snapshots__/config-set.spec.ts.snap b/src/config/__snapshots__/config-set.spec.ts.snap index 6619771d30..cdf4b615f8 100644 --- a/src/config/__snapshots__/config-set.spec.ts.snap +++ b/src/config/__snapshots__/config-set.spec.ts.snap @@ -59,8 +59,6 @@ Object { "before": Array [ Object { "factory": [Function], - "name": "hoisting-jest-mock", - "version": 4, }, ], } @@ -73,8 +71,6 @@ Object { "before": Array [ Object { "factory": [Function], - "name": "hoisting-jest-mock", - "version": 4, }, Object { "factory": [Function], @@ -94,8 +90,6 @@ Object { "before": Array [ Object { "factory": [Function], - "name": "hoisting-jest-mock", - "version": 4, }, ], } @@ -112,8 +106,6 @@ Object { "before": Array [ Object { "factory": [Function], - "name": "hoisting-jest-mock", - "version": 4, }, ], } @@ -126,8 +118,6 @@ Object { "before": Array [ Object { "factory": [Function], - "name": "hoisting-jest-mock", - "version": 4, }, Object { "factory": [Function], diff --git a/src/config/config-set.ts b/src/config/config-set.ts index 2a1498cd60..5972c7fa78 100644 --- a/src/config/config-set.ts +++ b/src/config/config-set.ts @@ -356,10 +356,15 @@ export class ConfigSet { stringify({ version: this.compilerModule.version, digest: this.tsJestDigest, + babelConfig: this.babelConfig, compilerModule: this.compilerModule, - compilerOptions: this.parsedTsConfig.options, + tsconfig: { + options: this.parsedTsConfig.options, + raw: this.parsedTsConfig.raw, + }, isolatedModules: this.isolatedModules, diagnostics: this._diagnostics, + transformers: this.resolvedTransformers, }), ) const res = join(this._jestCfg.cacheDirectory, 'ts-jest', cacheSuffix.substr(0, 2), cacheSuffix.substr(2)) diff --git a/src/transformers/README.md b/src/transformers/README.md index b5bb598a60..f4b9b0e55e 100644 --- a/src/transformers/README.md +++ b/src/transformers/README.md @@ -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) { diff --git a/src/transformers/hoist-jest.spec.ts b/src/transformers/hoist-jest.spec.ts index 80a01c78bc..9dc6930aec 100644 --- a/src/transformers/hoist-jest.spec.ts +++ b/src/transformers/hoist-jest.spec.ts @@ -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) diff --git a/src/transformers/hoist-jest.ts b/src/transformers/hoist-jest.ts index 30c3492b68..b0511a41a3 100644 --- a/src/transformers/hoist-jest.ts +++ b/src/transformers/hoist-jest.ts @@ -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 diff --git a/src/transformers/path-mapping.spec.ts b/src/transformers/path-mapping.spec.ts index 951f988d7b..7ab86020c7 100644 --- a/src/transformers/path-mapping.spec.ts +++ b/src/transformers/path-mapping.spec.ts @@ -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: '.', diff --git a/src/transformers/path-mapping.ts b/src/transformers/path-mapping.ts index 6d4971d5c7..dfdcb6aa85 100644 --- a/src/transformers/path-mapping.ts +++ b/src/transformers/path-mapping.ts @@ -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('.') /** diff --git a/src/ts-jest-transformer.ts b/src/ts-jest-transformer.ts index b42b6a2cab..25b58f17d2 100644 --- a/src/ts-jest-transformer.ts +++ b/src/ts-jest-transformer.ts @@ -98,6 +98,8 @@ export class TsJestTransformer implements Transformer { options: configSet.parsedTsConfig.options, raw: configSet.parsedTsConfig.raw, }, + transformers: configSet.resolvedTransformers, + tsCacheDir: configSet.tsCacheDir, }).serialized this._compiler = new TsJestCompiler(configSet, cacheFS) TsJestTransformer._cachedConfigSets.push({ From 3d87c0fa9d5c11dc79ae4b558b5133e470b82a6b Mon Sep 17 00:00:00 2001 From: Ahn Date: Wed, 10 Feb 2021 09:49:25 +0100 Subject: [PATCH 2/2] feat(config): invalidate cache when `astTransformers` value changes --- src/config/config-set.ts | 36 +++++++++++++++++++++--------------- src/ts-jest-transformer.ts | 12 +----------- 2 files changed, 22 insertions(+), 26 deletions(-) diff --git a/src/config/config-set.ts b/src/config/config-set.ts index 5972c7fa78..2753035a04 100644 --- a/src/config/config-set.ts +++ b/src/config/config-set.ts @@ -101,6 +101,7 @@ export class ConfigSet { readonly isolatedModules: boolean readonly cwd: string readonly rootDir: string + cacheSuffix!: string tsCacheDir: string | undefined parsedTsConfig!: ParsedCommandLine | Record resolvedTransformers: TsJestAstTransformer = { @@ -349,25 +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, - babelConfig: this.babelConfig, - compilerModule: this.compilerModule, - tsconfig: { - options: this.parsedTsConfig.options, - raw: this.parsedTsConfig.raw, - }, - isolatedModules: this.isolatedModules, - diagnostics: this._diagnostics, - transformers: this.resolvedTransformers, - }), + 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') diff --git a/src/ts-jest-transformer.ts b/src/ts-jest-transformer.ts index 25b58f17d2..99ef2a5885 100644 --- a/src/ts-jest-transformer.ts +++ b/src/ts-jest-transformer.ts @@ -90,17 +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, - }, - transformers: configSet.resolvedTransformers, - tsCacheDir: configSet.tsCacheDir, - }).serialized + this._transformCfgStr = `${new JsonableValue(jest).serialized}${configSet.cacheSuffix}` this._compiler = new TsJestCompiler(configSet, cacheFS) TsJestTransformer._cachedConfigSets.push({ jestConfig: new JsonableValue(config),