From 6c32a93fd790646bd74d471352fff403f210c190 Mon Sep 17 00:00:00 2001 From: Huafu Gandon Date: Tue, 18 Sep 2018 15:14:45 +0200 Subject: [PATCH] fix(config): jsx should also be considered as js files --- src/ts-jest-transformer.spec.ts | 24 +++++++++++++++++++++++- src/ts-jest-transformer.ts | 26 +++++++++++++++++--------- src/util/messages.ts | 2 ++ tsconfig.build.json | 2 +- tsconfig.json | 5 +++-- 5 files changed, 46 insertions(+), 13 deletions(-) diff --git a/src/ts-jest-transformer.spec.ts b/src/ts-jest-transformer.spec.ts index 1ec58884d5..e9244ad935 100644 --- a/src/ts-jest-transformer.spec.ts +++ b/src/ts-jest-transformer.spec.ts @@ -117,7 +117,7 @@ Array [ it('should return stringified version of file', () => { config.shouldStringifyContent.mockImplementation(() => true) - expect(process()).toMatchInlineSnapshot(`"ts:module.exports=\\"export default \\\\\\"foo\\\\\\"\\""`) + expect(process()).toMatchInlineSnapshot(`"module.exports=\\"export default \\\\\\"foo\\\\\\"\\""`) }) it('should warn when trying to process js but allowJs is false', () => { @@ -136,6 +136,28 @@ Array [ `) }) + it('should warn when trying to process unknown file types', () => { + args[1] = '/foo/bar.jest' + const logs = logTargetMock() + logs.clear() + expect(process()).toBe(INPUT) + expect(logs.lines.warn).toMatchInlineSnapshot(` +Array [ + "[level:40] Got a unknown file type to compile (file: /foo/bar.jest). 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. +", +] +`) + logs.clear() + babel = { process: jest.fn(s => `babel:${s}`) } + expect(process()).toBe(`babel:${INPUT}`) + expect(logs.lines.warn).toMatchInlineSnapshot(` +Array [ + "[level:40] Got a unknown file type to compile (file: /foo/bar.jest). 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. +", +] +`) + }) + it('should not pass the instrument option to babel-jest', () => { babel = { process: jest.fn(s => `babel:${s}`) } args[3] = { instrument: true } diff --git a/src/ts-jest-transformer.ts b/src/ts-jest-transformer.ts index d7849c18fc..fa443a6d78 100644 --- a/src/ts-jest-transformer.ts +++ b/src/ts-jest-transformer.ts @@ -89,7 +89,7 @@ export class TsJestTransformer implements jest.Transformer { ): jest.TransformedSource | string { this.logger.debug({ fileName: filePath, transformOptions }, 'processing', filePath) let result: string | jest.TransformedSource - let source: string = input + const source: string = input const configs = this.configsFor(jestConfig) const { hooks } = configs @@ -97,22 +97,30 @@ export class TsJestTransformer implements jest.Transformer { const stringify = configs.shouldStringifyContent(filePath) const babelJest = stringify ? undefined : configs.babelJestTransformer - // handles here what we should simply stringify - if (stringify) { - source = `module.exports=${JSON.stringify(source)}` - } + const isDefinitionFile = filePath.endsWith('.d.ts') + const isJsFile = !isDefinitionFile && /\.jsx?$/.test(filePath) + const isTsFile = isDefinitionFile || /\.tsx?$/.test(filePath) - // compilation - if (filePath.endsWith('.d.ts')) { + if (stringify) { + // handles here what we should simply stringify + result = `module.exports=${JSON.stringify(source)}` + } else if (isDefinitionFile) { // do not try to compile declaration files result = '' - } else if (!configs.typescript.options.allowJs && filePath.endsWith('.js')) { + } else if (!configs.typescript.options.allowJs && isJsFile) { // we've got a '.js' but the compiler option `allowJs` is not set or set to false this.logger.warn({ fileName: filePath }, interpolate(Errors.GotJsFileButAllowJsFalse, { path: filePath })) result = source - } else { + } else if (isTsFile) { // transpile TS code (source maps are included) result = configs.tsCompiler.compile(source, filePath) + } else { + // we should not get called for files with other extension than js[x], ts[x] and d.ts, + // TypeScript will bail if we try to compile, and if it was to call babel, users can + // define the transform value with `babel-jest` for this extension instead + const message = babelJest ? Errors.GotUnknownFileTypeWithBabel : Errors.GotUnknownFileTypeWithoutBabel + this.logger.warn({ fileName: filePath }, interpolate(message, { path: filePath })) + result = source } // calling babel-jest transformer diff --git a/src/util/messages.ts b/src/util/messages.ts index 2493ea8ba0..b59d62eb09 100644 --- a/src/util/messages.ts +++ b/src/util/messages.ts @@ -13,6 +13,8 @@ export enum Errors { MappingOnlyFirstTargetOfPath = 'Mapping only to first target of "{{path}}" because it has more than one ({{count}}).', CannotPatchBabelCore6 = 'Error while trying to patch babel-core/lib/transformation/file: {{error}}', GotJsFileButAllowJsFalse = 'Got a `.js` file to compile while `allowJs` option is not set to `true` (file: {{path}}). To fix this:\n - if you want TypeScript to process JS files, set `allowJs` to `true` in your TypeScript config (usually tsconfig.json)\n - if you do not want TypeScript to process your `.js` files, in your Jest config change the `transform` key which value is `ts-jest` so that it does not match `.js` files anymore', + GotUnknownFileTypeWithoutBabel = '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.', + 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.', } export enum Helps { diff --git a/tsconfig.build.json b/tsconfig.build.json index e615257178..9284ed7ee7 100644 --- a/tsconfig.build.json +++ b/tsconfig.build.json @@ -4,7 +4,7 @@ "sourceMap": false, "inlineSources": false, "inlineSourceMap": false, - "removeComments": true, + "noEmit": false, "outDir": "dist", "rootDir": "src", }, diff --git a/tsconfig.json b/tsconfig.json index 39ca9f334e..89ac531996 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,6 +1,7 @@ { "compilerOptions": { - "declaration": true, + "declaration": false, + "noEmit": true, "downlevelIteration": true, "esModuleInterop": true, "experimentalDecorators": true, @@ -18,7 +19,7 @@ "noImplicitReturns": true, "noUnusedLocals": true, "noUnusedParameters": true, - "removeComments": false, + "removeComments": true, "resolveJsonModule": true, "skipLibCheck": true, "sourceMap": false,