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

fix(compiler): update memory cache after changing module value #4440

Merged
merged 1 commit into from
Jul 10, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 0 additions & 32 deletions src/legacy/compiler/__snapshots__/ts-compiler.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -1,37 +1,5 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`TsCompiler getCompiledOutput isolatedModules false should compile codes with useESM {"babelConfig": false, "supportsStaticESM": false, "useESM": true} 1`] = `
{
"allowSyntheticDefaultImports": undefined,
"esModuleInterop": true,
"module": 1,
}
`;

exports[`TsCompiler getCompiledOutput isolatedModules false should compile codes with useESM {"babelConfig": false, "supportsStaticESM": true, "useESM": false} 1`] = `
{
"allowSyntheticDefaultImports": undefined,
"esModuleInterop": true,
"module": 1,
}
`;

exports[`TsCompiler getCompiledOutput isolatedModules false should compile codes with useESM {"babelConfig": false, "supportsStaticESM": true, "useESM": true} 1`] = `
{
"allowSyntheticDefaultImports": undefined,
"esModuleInterop": true,
"module": 1,
}
`;

exports[`TsCompiler getCompiledOutput isolatedModules false should compile codes with useESM {"babelConfig": true, "supportsStaticESM": false, "useESM": true} 1`] = `
{
"allowSyntheticDefaultImports": undefined,
"esModuleInterop": true,
"module": 1,
}
`;

exports[`TsCompiler getCompiledOutput isolatedModules true should transpile code with config {"babelConfig": false, "supportsStaticESM": false, "useESM": true} 1`] = `
{
"allowSyntheticDefaultImports": undefined,
Expand Down
99 changes: 58 additions & 41 deletions src/legacy/compiler/ts-compiler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { CompilerOptions, EmitOutput, transpileModule, TranspileOutput } fr
import * as ts from 'typescript'

import { createConfigSet, makeCompiler } from '../../__helpers__/fakers'
import type { RawCompilerOptions } from '../../raw-compiler-options'
import type { DepGraphInfo } from '../../types'
import { Errors, interpolate } from '../../utils/messages'

Expand Down Expand Up @@ -190,64 +191,80 @@ describe('TsCompiler', () => {
test.each([
{
useESM: true,
babelConfig: true,
supportsStaticESM: false,
supportsStaticESM: true,
moduleValue: 'ESNext',
expectedModule: ts.ModuleKind.ESNext,
expectedEsModuleInterop: false,
},
{
useESM: true,
babelConfig: false,
supportsStaticESM: true,
moduleValue: 'NodeNext',
expectedModule: ts.ModuleKind.NodeNext,
expectedEsModuleInterop: true,
},
{
useESM: true,
babelConfig: false,
supportsStaticESM: false,
moduleValue: 'ESNext',
expectedModule: ts.ModuleKind.CommonJS,
expectedEsModuleInterop: false,
},
{
useESM: false,
babelConfig: false,
supportsStaticESM: true,
moduleValue: 'ESNext',
expectedModule: ts.ModuleKind.CommonJS,
expectedEsModuleInterop: false,
},
])('should compile codes with useESM %p', ({ useESM, babelConfig, supportsStaticESM }) => {
const configSet = createConfigSet({
tsJestConfig: { ...baseTsJestConfig, useESM, babelConfig },
})
const emptyFile = join(mockFolder, 'empty.ts')
configSet.parsedTsConfig.fileNames.push(emptyFile)
const compiler = new TsCompiler(configSet, new Map())
// @ts-expect-error testing purpose
compiler._languageService.getEmitOutput = jest.fn().mockReturnValueOnce({
outputFiles: [{ text: sourceMap }, { text: jsOutput }],
emitSkipped: false,
} as EmitOutput)
// @ts-expect-error testing purpose
compiler.getDiagnostics = jest.fn().mockReturnValue([])
])(
'should compile codes with useESM %p',
({ useESM, supportsStaticESM, moduleValue, expectedModule, expectedEsModuleInterop }) => {
const configSet = createConfigSet({
tsJestConfig: {
...baseTsJestConfig,
useESM,
tsconfig: {
module: moduleValue as unknown as RawCompilerOptions['module'],
esModuleInterop: false,
},
},
})
const emptyFile = join(mockFolder, 'empty.ts')
configSet.parsedTsConfig.fileNames.push(emptyFile)
const compiler = new TsCompiler(configSet, new Map())
// @ts-expect-error testing purpose
compiler._languageService.getEmitOutput = jest.fn().mockReturnValueOnce({
outputFiles: [{ text: sourceMap }, { text: jsOutput }],
emitSkipped: false,
} as EmitOutput)
// @ts-expect-error testing purpose
compiler.getDiagnostics = jest.fn().mockReturnValue([])

const output = compiler.getCompiledOutput(fileContent, fileName, {
depGraphs: new Map(),
supportsStaticESM,
watchMode: false,
})
const output = compiler.getCompiledOutput(fileContent, fileName, {
depGraphs: new Map(),
supportsStaticESM,
watchMode: false,
})

// @ts-expect-error testing purpose
const usedCompilerOptions = compiler._compilerOptions
expect({
module: usedCompilerOptions.module,
esModuleInterop: usedCompilerOptions.esModuleInterop,
allowSyntheticDefaultImports: usedCompilerOptions.allowSyntheticDefaultImports,
}).toMatchSnapshot()
expect(output).toEqual({
code: updateOutput(jsOutput, fileName, sourceMap),
diagnostics: [],
})
// @ts-expect-error testing purpose
const usedCompilerOptions = compiler._compilerOptions

// @ts-expect-error testing purpose
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
compiler._languageService!.getSemanticDiagnostics(fileName)
expect(usedCompilerOptions.module).toBe(expectedModule)
expect(usedCompilerOptions.esModuleInterop).toBe(expectedEsModuleInterop)
expect(output).toEqual({
code: updateOutput(jsOutput, fileName, sourceMap),
diagnostics: [],
})

// @ts-expect-error testing purpose
expect(compiler._fileContentCache.has(emptyFile)).toBe(true)
})
// @ts-expect-error testing purpose
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
compiler._languageService!.getSemanticDiagnostics(fileName)

// @ts-expect-error testing purpose
expect(compiler._fileContentCache.has(emptyFile)).toBe(true)
},
)

test('should show a warning message and return original file content for non ts/tsx files if emitSkipped is true', () => {
const compiler = makeCompiler({
Expand Down
4 changes: 2 additions & 2 deletions src/legacy/compiler/ts-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,10 @@ export class TsCompiler implements TsCompilerInstance {
}

getCompiledOutput(fileContent: string, fileName: string, options: TsJestCompileOptions): CompiledOutput {
const moduleKind = this._initialCompilerOptions.module
const currentModuleKind = this._compilerOptions.module
const isEsmMode = this.configSet.useESM && options.supportsStaticESM
this._compilerOptions = this.fixupCompilerOptionsForModuleKind(this._initialCompilerOptions, isEsmMode)
const moduleKind = this._initialCompilerOptions.module
const currentModuleKind = this._compilerOptions.module
if (this._languageService) {
this._logger.debug({ fileName }, 'getCompiledOutput(): compiling using language service')

Expand Down
Loading