Skip to content

Commit 94db43f

Browse files
committed
fix(compiler): update memory cache after changing module value
Fixes #4439
1 parent e80594d commit 94db43f

File tree

3 files changed

+60
-75
lines changed

3 files changed

+60
-75
lines changed

src/legacy/compiler/__snapshots__/ts-compiler.spec.ts.snap

-32
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,5 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`TsCompiler getCompiledOutput isolatedModules false should compile codes with useESM {"babelConfig": false, "supportsStaticESM": false, "useESM": true} 1`] = `
4-
{
5-
"allowSyntheticDefaultImports": undefined,
6-
"esModuleInterop": true,
7-
"module": 1,
8-
}
9-
`;
10-
11-
exports[`TsCompiler getCompiledOutput isolatedModules false should compile codes with useESM {"babelConfig": false, "supportsStaticESM": true, "useESM": false} 1`] = `
12-
{
13-
"allowSyntheticDefaultImports": undefined,
14-
"esModuleInterop": true,
15-
"module": 1,
16-
}
17-
`;
18-
19-
exports[`TsCompiler getCompiledOutput isolatedModules false should compile codes with useESM {"babelConfig": false, "supportsStaticESM": true, "useESM": true} 1`] = `
20-
{
21-
"allowSyntheticDefaultImports": undefined,
22-
"esModuleInterop": true,
23-
"module": 1,
24-
}
25-
`;
26-
27-
exports[`TsCompiler getCompiledOutput isolatedModules false should compile codes with useESM {"babelConfig": true, "supportsStaticESM": false, "useESM": true} 1`] = `
28-
{
29-
"allowSyntheticDefaultImports": undefined,
30-
"esModuleInterop": true,
31-
"module": 1,
32-
}
33-
`;
34-
353
exports[`TsCompiler getCompiledOutput isolatedModules true should transpile code with config {"babelConfig": false, "supportsStaticESM": false, "useESM": true} 1`] = `
364
{
375
"allowSyntheticDefaultImports": undefined,

src/legacy/compiler/ts-compiler.spec.ts

+58-41
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type { CompilerOptions, EmitOutput, transpileModule, TranspileOutput } fr
55
import * as ts from 'typescript'
66

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

@@ -190,64 +191,80 @@ describe('TsCompiler', () => {
190191
test.each([
191192
{
192193
useESM: true,
193-
babelConfig: true,
194-
supportsStaticESM: false,
194+
supportsStaticESM: true,
195+
moduleValue: 'ESNext',
196+
expectedModule: ts.ModuleKind.ESNext,
197+
expectedEsModuleInterop: false,
195198
},
196199
{
197200
useESM: true,
198-
babelConfig: false,
199201
supportsStaticESM: true,
202+
moduleValue: 'NodeNext',
203+
expectedModule: ts.ModuleKind.NodeNext,
204+
expectedEsModuleInterop: true,
200205
},
201206
{
202207
useESM: true,
203-
babelConfig: false,
204208
supportsStaticESM: false,
209+
moduleValue: 'ESNext',
210+
expectedModule: ts.ModuleKind.CommonJS,
211+
expectedEsModuleInterop: false,
205212
},
206213
{
207214
useESM: false,
208-
babelConfig: false,
209215
supportsStaticESM: true,
216+
moduleValue: 'ESNext',
217+
expectedModule: ts.ModuleKind.CommonJS,
218+
expectedEsModuleInterop: false,
210219
},
211-
])('should compile codes with useESM %p', ({ useESM, babelConfig, supportsStaticESM }) => {
212-
const configSet = createConfigSet({
213-
tsJestConfig: { ...baseTsJestConfig, useESM, babelConfig },
214-
})
215-
const emptyFile = join(mockFolder, 'empty.ts')
216-
configSet.parsedTsConfig.fileNames.push(emptyFile)
217-
const compiler = new TsCompiler(configSet, new Map())
218-
// @ts-expect-error testing purpose
219-
compiler._languageService.getEmitOutput = jest.fn().mockReturnValueOnce({
220-
outputFiles: [{ text: sourceMap }, { text: jsOutput }],
221-
emitSkipped: false,
222-
} as EmitOutput)
223-
// @ts-expect-error testing purpose
224-
compiler.getDiagnostics = jest.fn().mockReturnValue([])
220+
])(
221+
'should compile codes with useESM %p',
222+
({ useESM, supportsStaticESM, moduleValue, expectedModule, expectedEsModuleInterop }) => {
223+
const configSet = createConfigSet({
224+
tsJestConfig: {
225+
...baseTsJestConfig,
226+
useESM,
227+
tsconfig: {
228+
module: moduleValue as unknown as RawCompilerOptions['module'],
229+
esModuleInterop: false,
230+
},
231+
},
232+
})
233+
const emptyFile = join(mockFolder, 'empty.ts')
234+
configSet.parsedTsConfig.fileNames.push(emptyFile)
235+
const compiler = new TsCompiler(configSet, new Map())
236+
// @ts-expect-error testing purpose
237+
compiler._languageService.getEmitOutput = jest.fn().mockReturnValueOnce({
238+
outputFiles: [{ text: sourceMap }, { text: jsOutput }],
239+
emitSkipped: false,
240+
} as EmitOutput)
241+
// @ts-expect-error testing purpose
242+
compiler.getDiagnostics = jest.fn().mockReturnValue([])
225243

226-
const output = compiler.getCompiledOutput(fileContent, fileName, {
227-
depGraphs: new Map(),
228-
supportsStaticESM,
229-
watchMode: false,
230-
})
244+
const output = compiler.getCompiledOutput(fileContent, fileName, {
245+
depGraphs: new Map(),
246+
supportsStaticESM,
247+
watchMode: false,
248+
})
231249

232-
// @ts-expect-error testing purpose
233-
const usedCompilerOptions = compiler._compilerOptions
234-
expect({
235-
module: usedCompilerOptions.module,
236-
esModuleInterop: usedCompilerOptions.esModuleInterop,
237-
allowSyntheticDefaultImports: usedCompilerOptions.allowSyntheticDefaultImports,
238-
}).toMatchSnapshot()
239-
expect(output).toEqual({
240-
code: updateOutput(jsOutput, fileName, sourceMap),
241-
diagnostics: [],
242-
})
250+
// @ts-expect-error testing purpose
251+
const usedCompilerOptions = compiler._compilerOptions
243252

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

248-
// @ts-expect-error testing purpose
249-
expect(compiler._fileContentCache.has(emptyFile)).toBe(true)
250-
})
260+
// @ts-expect-error testing purpose
261+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
262+
compiler._languageService!.getSemanticDiagnostics(fileName)
263+
264+
// @ts-expect-error testing purpose
265+
expect(compiler._fileContentCache.has(emptyFile)).toBe(true)
266+
},
267+
)
251268

252269
test('should show a warning message and return original file content for non ts/tsx files if emitSkipped is true', () => {
253270
const compiler = makeCompiler({

src/legacy/compiler/ts-compiler.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,10 @@ export class TsCompiler implements TsCompilerInstance {
176176
}
177177

178178
getCompiledOutput(fileContent: string, fileName: string, options: TsJestCompileOptions): CompiledOutput {
179-
const moduleKind = this._initialCompilerOptions.module
180-
const currentModuleKind = this._compilerOptions.module
181179
const isEsmMode = this.configSet.useESM && options.supportsStaticESM
182180
this._compilerOptions = this.fixupCompilerOptionsForModuleKind(this._initialCompilerOptions, isEsmMode)
181+
const moduleKind = this._initialCompilerOptions.module
182+
const currentModuleKind = this._compilerOptions.module
183183
if (this._languageService) {
184184
this._logger.debug({ fileName }, 'getCompiledOutput(): compiling using language service')
185185

0 commit comments

Comments
 (0)