Skip to content

Commit

Permalink
refactor(compiler): increase project version based on module value (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ahnpnl authored May 31, 2021
1 parent c5ce979 commit a0821e5
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 17 deletions.
12 changes: 2 additions & 10 deletions src/compiler/ts-compiler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 4 additions & 7 deletions src/compiler/ts-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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 ||
Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand All @@ -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++
}
Expand Down

0 comments on commit a0821e5

Please sign in to comment.