diff --git a/src/compiler/ts-compiler.spec.ts b/src/compiler/ts-compiler.spec.ts index 56395a69f7..540750aec6 100644 --- a/src/compiler/ts-compiler.spec.ts +++ b/src/compiler/ts-compiler.spec.ts @@ -329,6 +329,14 @@ describe('TsCompiler', () => { beforeEach(() => { // @ts-expect-error testing purpose compiler._projectVersion = 1 + /** + * This is to ensure that `compilerOptions` value and `_parsedTsConfig.fileNames` are always like + * when compiler instance is created since here we only create compiler instance once for all the tests below. + */ + // @ts-expect-error testing purpose. + compiler._compilerOptions = { ...compiler._initialCompilerOptions } + // @ts-expect-error testing purpose. + compiler._parsedTsConfig.fileNames = [] fileContentCache.clear() fileVersionCache.clear() }) @@ -360,6 +368,29 @@ 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) + 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) + + // @ts-expect-error testing purpose + expect(compiler._projectVersion).toEqual(2) + }) + test('should increase project version if processing file version is 0', () => { fileContentCache.set(fileName, fileContent) fileVersionCache.set(fileName, 0) diff --git a/src/compiler/ts-compiler.ts b/src/compiler/ts-compiler.ts index 0543708566..7eafdb1b86 100644 --- a/src/compiler/ts-compiler.ts +++ b/src/compiler/ts-compiler.ts @@ -32,6 +32,7 @@ import type { TsJestCompileOptions, TTypeScript, } from '../types' +import { stringify } from '../utils/json' import { rootLogger } from '../utils/logger' import { Errors, interpolate } from '../utils/messages' @@ -405,6 +406,9 @@ export class TsCompiler implements TsCompilerInstance { shouldIncrementProjectVersion = true } } + if (stringify(this._compilerOptions) !== stringify(this._initialCompilerOptions)) { + shouldIncrementProjectVersion = true + } if (shouldIncrementProjectVersion) this._projectVersion++ }