Skip to content

Commit

Permalink
fix(compiler): return file content on emitSkipped for non ts/tsx files (
Browse files Browse the repository at this point in the history
#2519)

Closes #2513
  • Loading branch information
ahnpnl authored Apr 14, 2021
1 parent fda4b6b commit a4e5640
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 6 deletions.
40 changes: 37 additions & 3 deletions src/compiler/ts-compiler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
})
Expand All @@ -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 },
Expand Down
8 changes: 6 additions & 2 deletions src/compiler/ts-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion src/utils/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}}",
}

/**
Expand Down

0 comments on commit a4e5640

Please sign in to comment.