Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(compiler): increase project version based on module value #2638

Merged
merged 1 commit into from May 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 2 additions & 10 deletions src/compiler/ts-compiler.spec.ts
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
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