From 952cc87f415bd1412bb74253e31a413f45a2c7ed Mon Sep 17 00:00:00 2001 From: Huafu Gandon Date: Mon, 17 Sep 2018 09:46:06 +0200 Subject: [PATCH] feat(hints): warns if transform matches js without allowJs --- src/ts-jest-transformer.spec.ts | 28 ++++++++++++++++++++++++++-- src/ts-jest-transformer.ts | 17 +++++++++++++---- src/util/messages.ts | 1 + 3 files changed, 40 insertions(+), 6 deletions(-) diff --git a/src/ts-jest-transformer.spec.ts b/src/ts-jest-transformer.spec.ts index 1f80c9ac89..1ec58884d5 100644 --- a/src/ts-jest-transformer.spec.ts +++ b/src/ts-jest-transformer.spec.ts @@ -1,5 +1,7 @@ import stringify from 'fast-json-stable-stringify' +import { ParsedCommandLine } from 'typescript' +import { logTargetMock } from './__helpers__/mocks' import { TsJestTransformer } from './ts-jest-transformer' describe('configFor', () => { @@ -31,8 +33,12 @@ describe('lastTransformerId', () => { describe('process', () => { let tr: TsJestTransformer let babel: any + let typescript: ParsedCommandLine let args: [string, string, any, any] const config = { + get typescript() { + return typescript + }, shouldStringifyContent: jest.fn(), get babelJestTransformer() { return babel @@ -53,12 +59,12 @@ describe('process', () => { .mockImplementation(() => config) .mockClear() config.shouldStringifyContent.mockImplementation(() => false).mockClear() - babel = { process: jest.fn(s => `babel:${s}`) } + babel = null config.tsCompiler.compile.mockImplementation(s => `ts:${s}`).mockClear() + typescript = { options: {} } as any }) it('should process input without babel', () => { - babel = null expect(process()).toBe(`ts:${INPUT}`) expect(config.shouldStringifyContent.mock.calls).toMatchInlineSnapshot(` Array [ @@ -78,6 +84,7 @@ Array [ }) it('should process input with babel', () => { + babel = { process: jest.fn(s => `babel:${s}`) } expect(process()).toBe(`babel:ts:${INPUT}`) expect(config.shouldStringifyContent.mock.calls).toMatchInlineSnapshot(` Array [ @@ -113,7 +120,24 @@ Array [ expect(process()).toMatchInlineSnapshot(`"ts:module.exports=\\"export default \\\\\\"foo\\\\\\"\\""`) }) + it('should warn when trying to process js but allowJs is false', () => { + args[1] = '/foo/bar.js' + typescript.options.allowJs = false + const logs = logTargetMock() + logs.clear() + expect(process()).toBe(INPUT) + expect(logs.lines.warn).toMatchInlineSnapshot(` +Array [ + "[level:40] Got a \`.js\` file to compile while \`allowJs\` option is not set to \`true\` (file: /foo/bar.js). To fix this: + - if you want TypeScript to process JS files, set \`allowJs\` to \`true\` in your TypeScript config (usually tsconfig.json) + - 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 +", +] +`) + }) + it('should not pass the instrument option to babel-jest', () => { + babel = { process: jest.fn(s => `babel:${s}`) } args[3] = { instrument: true } expect(process()).toBe(`babel:ts:${INPUT}`) expect(config.babelJestTransformer.process.mock.calls).toMatchInlineSnapshot(` diff --git a/src/ts-jest-transformer.ts b/src/ts-jest-transformer.ts index fa33547370..d7849c18fc 100644 --- a/src/ts-jest-transformer.ts +++ b/src/ts-jest-transformer.ts @@ -6,6 +6,7 @@ import { TsJestGlobalOptions } from './types' import { parse, stringify } from './util/json' import { JsonableValue } from './util/jsonable-value' import { rootLogger } from './util/logger' +import { Errors, interpolate } from './util/messages' import { sha1 } from './util/sha1' /** @@ -101,10 +102,18 @@ export class TsJestTransformer implements jest.Transformer { source = `module.exports=${JSON.stringify(source)}` } - // transpile TS code (source maps are included) - result = filePath.endsWith('.d.ts') - ? '' // do not try to compile declaration files - : configs.tsCompiler.compile(source, filePath) + // compilation + if (filePath.endsWith('.d.ts')) { + // do not try to compile declaration files + result = '' + } else if (!configs.typescript.options.allowJs && filePath.endsWith('.js')) { + // 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 { + // transpile TS code (source maps are included) + result = configs.tsCompiler.compile(source, filePath) + } // calling babel-jest transformer if (babelJest) { diff --git a/src/util/messages.ts b/src/util/messages.ts index 96862312a5..2493ea8ba0 100644 --- a/src/util/messages.ts +++ b/src/util/messages.ts @@ -12,6 +12,7 @@ export enum Errors { NotMappingPathWithEmptyMap = 'Not mapping "{{path}}" because it has no target.', 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', } export enum Helps {