From a4e5640f54a7810c9b7aba32663ce4a53893d22f Mon Sep 17 00:00:00 2001 From: Ahn Date: Wed, 14 Apr 2021 16:54:10 +0200 Subject: [PATCH] fix(compiler): return file content on emitSkipped for non ts/tsx files (#2519) Closes #2513 --- src/compiler/ts-compiler.spec.ts | 40 +++++++++++++++++++++++++++++--- src/compiler/ts-compiler.ts | 8 +++++-- src/utils/messages.ts | 3 ++- 3 files changed, 45 insertions(+), 6 deletions(-) diff --git a/src/compiler/ts-compiler.spec.ts b/src/compiler/ts-compiler.spec.ts index 804911ca1c..e670342587 100644 --- a/src/compiler/ts-compiler.spec.ts +++ b/src/compiler/ts-compiler.spec.ts @@ -195,7 +195,7 @@ describe('TsCompiler', () => { expect(output).toEqual(updateOutput(jsOutput, fileName, sourceMap)) }) - test('should return original file content if emitSkipped is true', () => { + test('should show a warning message and return original file content for non ts/tsx files if emitSkipped is true', () => { const compiler = makeCompiler({ tsJestConfig: { ...baseTsJestConfig }, }) @@ -205,16 +205,50 @@ describe('TsCompiler', () => { emitSkipped: true, } as EmitOutput) // @ts-expect-error testing purpose + compiler._logger.warn = jest.fn() + // @ts-expect-error testing purpose compiler._doTypeChecking = jest.fn() - const output = compiler.getCompiledOutput(fileContent, fileName, { + const fileToCheck = fileName.replace('.ts', '.js') + + const output = compiler.getCompiledOutput(fileContent, fileToCheck, { depGraphs: new Map(), supportsStaticESM: false, watchMode: false, }) - expect(output).toEqual(updateOutput(fileContent, fileName, sourceMap)) + // @ts-expect-error testing purpose + expect(compiler._logger.warn).toHaveBeenCalled() + expect(output).toEqual(updateOutput(fileContent, fileToCheck, sourceMap)) }) + test.each([fileName, fileName.replace('.ts', '.tsx')])( + 'should throw error for ts/tsx files if emitSkipped is true', + (fileToCheck) => { + const compiler = makeCompiler({ + tsJestConfig: { ...baseTsJestConfig }, + }) + // @ts-expect-error testing purpose + compiler._languageService.getEmitOutput = jest.fn().mockReturnValueOnce({ + outputFiles: [{ text: sourceMap }, { text: jsOutput }], + emitSkipped: true, + } as EmitOutput) + // @ts-expect-error testing purpose + compiler._logger.warn = jest.fn() + // @ts-expect-error testing purpose + compiler._doTypeChecking = jest.fn() + + // @ts-expect-error testing purpose + expect(compiler._logger.warn).not.toHaveBeenCalled() + expect(() => + compiler.getCompiledOutput(fileContent, fileToCheck, { + depGraphs: new Map(), + supportsStaticESM: false, + watchMode: false, + }), + ).toThrowError() + }, + ) + test('should throw error when there are no outputFiles', () => { const compiler = makeCompiler({ tsJestConfig: { ...baseTsJestConfig }, diff --git a/src/compiler/ts-compiler.ts b/src/compiler/ts-compiler.ts index cb61534c2c..aeae4b6f9e 100644 --- a/src/compiler/ts-compiler.ts +++ b/src/compiler/ts-compiler.ts @@ -176,9 +176,13 @@ export class TsCompiler implements TsCompilerInstance { const output: EmitOutput = this._languageService.getEmitOutput(fileName) this._doTypeChecking(fileName, options.depGraphs, options.watchMode) if (output.emitSkipped) { - this._logger.warn(interpolate(Errors.CannotProcessFile, { file: fileName })) + if (TS_TSX_REGEX.test(fileName)) { + throw new Error(interpolate(Errors.CannotProcessFile, { file: fileName })) + } else { + this._logger.warn(interpolate(Errors.CannotProcessFileReturnOriginal, { file: fileName })) - return updateOutput(fileContent, fileName, '{}') + return updateOutput(fileContent, fileName, '{}') + } } // Throw an error when requiring `.d.ts` files. if (!output.outputFiles.length) { diff --git a/src/utils/messages.ts b/src/utils/messages.ts index 4ca47e74cf..35e6abdc4a 100644 --- a/src/utils/messages.ts +++ b/src/utils/messages.ts @@ -17,7 +17,8 @@ export const enum Errors { 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.', 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.', 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', - 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", + 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 `'.'`", + 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}}", } /**