Skip to content

Commit a4e5640

Browse files
authored
fix(compiler): return file content on emitSkipped for non ts/tsx files (#2519)
Closes #2513
1 parent fda4b6b commit a4e5640

File tree

3 files changed

+45
-6
lines changed

3 files changed

+45
-6
lines changed

src/compiler/ts-compiler.spec.ts

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ describe('TsCompiler', () => {
195195
expect(output).toEqual(updateOutput(jsOutput, fileName, sourceMap))
196196
})
197197

198-
test('should return original file content if emitSkipped is true', () => {
198+
test('should show a warning message and return original file content for non ts/tsx files if emitSkipped is true', () => {
199199
const compiler = makeCompiler({
200200
tsJestConfig: { ...baseTsJestConfig },
201201
})
@@ -205,16 +205,50 @@ describe('TsCompiler', () => {
205205
emitSkipped: true,
206206
} as EmitOutput)
207207
// @ts-expect-error testing purpose
208+
compiler._logger.warn = jest.fn()
209+
// @ts-expect-error testing purpose
208210
compiler._doTypeChecking = jest.fn()
209-
const output = compiler.getCompiledOutput(fileContent, fileName, {
211+
const fileToCheck = fileName.replace('.ts', '.js')
212+
213+
const output = compiler.getCompiledOutput(fileContent, fileToCheck, {
210214
depGraphs: new Map(),
211215
supportsStaticESM: false,
212216
watchMode: false,
213217
})
214218

215-
expect(output).toEqual(updateOutput(fileContent, fileName, sourceMap))
219+
// @ts-expect-error testing purpose
220+
expect(compiler._logger.warn).toHaveBeenCalled()
221+
expect(output).toEqual(updateOutput(fileContent, fileToCheck, sourceMap))
216222
})
217223

224+
test.each([fileName, fileName.replace('.ts', '.tsx')])(
225+
'should throw error for ts/tsx files if emitSkipped is true',
226+
(fileToCheck) => {
227+
const compiler = makeCompiler({
228+
tsJestConfig: { ...baseTsJestConfig },
229+
})
230+
// @ts-expect-error testing purpose
231+
compiler._languageService.getEmitOutput = jest.fn().mockReturnValueOnce({
232+
outputFiles: [{ text: sourceMap }, { text: jsOutput }],
233+
emitSkipped: true,
234+
} as EmitOutput)
235+
// @ts-expect-error testing purpose
236+
compiler._logger.warn = jest.fn()
237+
// @ts-expect-error testing purpose
238+
compiler._doTypeChecking = jest.fn()
239+
240+
// @ts-expect-error testing purpose
241+
expect(compiler._logger.warn).not.toHaveBeenCalled()
242+
expect(() =>
243+
compiler.getCompiledOutput(fileContent, fileToCheck, {
244+
depGraphs: new Map(),
245+
supportsStaticESM: false,
246+
watchMode: false,
247+
}),
248+
).toThrowError()
249+
},
250+
)
251+
218252
test('should throw error when there are no outputFiles', () => {
219253
const compiler = makeCompiler({
220254
tsJestConfig: { ...baseTsJestConfig },

src/compiler/ts-compiler.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,13 @@ export class TsCompiler implements TsCompilerInstance {
176176
const output: EmitOutput = this._languageService.getEmitOutput(fileName)
177177
this._doTypeChecking(fileName, options.depGraphs, options.watchMode)
178178
if (output.emitSkipped) {
179-
this._logger.warn(interpolate(Errors.CannotProcessFile, { file: fileName }))
179+
if (TS_TSX_REGEX.test(fileName)) {
180+
throw new Error(interpolate(Errors.CannotProcessFile, { file: fileName }))
181+
} else {
182+
this._logger.warn(interpolate(Errors.CannotProcessFileReturnOriginal, { file: fileName }))
180183

181-
return updateOutput(fileContent, fileName, '{}')
184+
return updateOutput(fileContent, fileName, '{}')
185+
}
182186
}
183187
// Throw an error when requiring `.d.ts` files.
184188
if (!output.outputFiles.length) {

src/utils/messages.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ export const enum Errors {
1717
GotUnknownFileTypeWithBabel = 'Got a unknown file type to compile (file: {{path}}). To fix this, in your Jest config change the `transform` key which value is `ts-jest` so that it does not match this kind of files anymore. If you still want Babel to process it, add another entry to the `transform` option with value `babel-jest` which key matches this type of files.',
1818
ConfigNoModuleInterop = 'If you have issues related to imports, you should consider setting `esModuleInterop` to `true` in your TypeScript configuration file (usually `tsconfig.json`). See https://blogs.msdn.microsoft.com/typescript/2018/01/31/announcing-typescript-2-7/#easier-ecmascript-module-interoperability for more information.',
1919
MismatchNodeTargetMapping = 'There is a mismatch between your NodeJs version {{nodeJsVer}} and your TypeScript target {{compilationTarget}}. This might lead to some unexpected errors when running tests with `ts-jest`. To fix this, you can check https://github.com/microsoft/TypeScript/wiki/Node-Target-Mapping',
20-
CannotProcessFile = "Unable to process '{{file}}', falling back to original file content. Please make sure that `outDir` in your tsconfig is neither `''` or `'.'`. You can also configure Jest config option `transformIgnorePatterns` to ignore {{file}} from transformation",
20+
CannotProcessFileReturnOriginal = "Unable to process '{{file}}', falling back to original file content. You can also configure Jest config option `transformIgnorePatterns` to ignore {{file}} from transformation or make sure that `outDir` in your tsconfig is neither `''` or `'.'`",
21+
CannotProcessFile = "Unable to process '{{file}}', please make sure that `outDir` in your tsconfig is neither `''` or `'.'`. You can also configure Jest config option `transformIgnorePatterns` to inform `ts-jest` to transform {{file}}",
2122
}
2223

2324
/**

0 commit comments

Comments
 (0)