From 11aaed1ea8a7a214a5863a5a428fae27751d8072 Mon Sep 17 00:00:00 2001 From: Ahn Date: Sun, 30 May 2021 23:38:55 +0200 Subject: [PATCH] refactor(compiler): increase project version based on `module` value --- src/compiler/ts-compiler.spec.ts | 12 ++---------- src/compiler/ts-compiler.ts | 11 ++++------- 2 files changed, 6 insertions(+), 17 deletions(-) diff --git a/src/compiler/ts-compiler.spec.ts b/src/compiler/ts-compiler.spec.ts index 80f4885c48..06fd98cc54 100644 --- a/src/compiler/ts-compiler.spec.ts +++ b/src/compiler/ts-compiler.spec.ts @@ -379,24 +379,16 @@ describe('TsCompiler', () => { expect(compiler._projectVersion).toEqual(2) }) - test('should increase project version if processing file is in compiler file list', () => { - // @ts-expect-error testing purpose - compiler._parsedTsConfig.fileNames.push(fileName) + test('should increase project version if module value has changed', () => { fileContentCache.set(fileName, fileContent) fileVersionCache.set(fileName, 1) // @ts-expect-error testing purpose compiler._fileContentCache = fileContentCache // @ts-expect-error testing purpose compiler._fileVersionCache = fileVersionCache - // @ts-expect-error testing purpose - compiler._compilerOptions = { - // @ts-expect-error testing purpose - ...compiler._compilerOptions, - module: ModuleKind.AMD, - } // @ts-expect-error testing purpose - compiler._updateMemoryCache(fileContent, fileName) + compiler._updateMemoryCache(fileContent, fileName, false) // @ts-expect-error testing purpose expect(compiler._projectVersion).toEqual(2) diff --git a/src/compiler/ts-compiler.ts b/src/compiler/ts-compiler.ts index 5c5831bc07..6b2d773dca 100644 --- a/src/compiler/ts-compiler.ts +++ b/src/compiler/ts-compiler.ts @@ -32,7 +32,6 @@ import type { TsJestCompileOptions, TTypeScript, } from '../types' -import { stringify } from '../utils/json' import { rootLogger } from '../utils/logger' import { Errors, interpolate } from '../utils/messages' @@ -150,6 +149,7 @@ export class TsCompiler implements TsCompilerInstance { let moduleKind = this._initialCompilerOptions.module let esModuleInterop = this._initialCompilerOptions.esModuleInterop let allowSyntheticDefaultImports = this._initialCompilerOptions.allowSyntheticDefaultImports + const currentModuleKind = this._compilerOptions.module if (options.supportsStaticESM && this.configSet.useESM) { moduleKind = !moduleKind || @@ -173,7 +173,7 @@ export class TsCompiler implements TsCompilerInstance { this._logger.debug({ fileName }, 'getCompiledOutput(): compiling using language service') // Must set memory cache before attempting to compile - this._updateMemoryCache(fileContent, fileName) + this._updateMemoryCache(fileContent, fileName, currentModuleKind === moduleKind) const output: EmitOutput = this._languageService.getEmitOutput(fileName) this._doTypeChecking(fileName, options.depGraphs, options.watchMode) if (output.emitSkipped) { @@ -376,7 +376,7 @@ export class TsCompiler implements TsCompilerInstance { /** * @internal */ - private _updateMemoryCache(contents: string, fileName: string): void { + private _updateMemoryCache(contents: string, fileName: string, isModuleKindTheSame = true): void { this._logger.debug({ fileName }, 'updateMemoryCache: update memory cache for language service') let shouldIncrementProjectVersion = false @@ -402,13 +402,10 @@ export class TsCompiler implements TsCompilerInstance { * When a file is from node_modules or referenced to a referenced project and jest wants to transform it, we need * to make sure that the Program is updated with this information */ - if (!this._parsedTsConfig.fileNames.includes(fileName)) { + if (!this._parsedTsConfig.fileNames.includes(fileName) || !isModuleKindTheSame) { shouldIncrementProjectVersion = true } } - if (stringify(this._compilerOptions) !== stringify(this._initialCompilerOptions)) { - shouldIncrementProjectVersion = true - } if (shouldIncrementProjectVersion) this._projectVersion++ }