From 374dca126f8748882370f0207dbd8346492e3ab5 Mon Sep 17 00:00:00 2001 From: Huafu Gandon Date: Thu, 20 Sep 2018 20:42:22 +0200 Subject: [PATCH 1/2] fix(compile): js files were never transpiled thru TS Closes #740 --- src/ts-jest-transformer.spec.ts | 59 +++++++++++++++++++++++++++++++-- src/ts-jest-transformer.ts | 2 +- 2 files changed, 58 insertions(+), 3 deletions(-) diff --git a/src/ts-jest-transformer.spec.ts b/src/ts-jest-transformer.spec.ts index e9244ad935..2d8ea19c92 100644 --- a/src/ts-jest-transformer.spec.ts +++ b/src/ts-jest-transformer.spec.ts @@ -64,7 +64,7 @@ describe('process', () => { typescript = { options: {} } as any }) - it('should process input without babel', () => { + it('should process ts input without babel', () => { expect(process()).toBe(`ts:${INPUT}`) expect(config.shouldStringifyContent.mock.calls).toMatchInlineSnapshot(` Array [ @@ -83,7 +83,28 @@ Array [ `) }) - it('should process input with babel', () => { + it('should process js input without babel', () => { + typescript.options.allowJs = true + args[1] = '/foo/bar.js' + expect(process()).toBe(`ts:${INPUT}`) + expect(config.shouldStringifyContent.mock.calls).toMatchInlineSnapshot(` +Array [ + Array [ + "/foo/bar.js", + ], +] +`) + expect(config.tsCompiler.compile.mock.calls).toMatchInlineSnapshot(` +Array [ + Array [ + "export default \\"foo\\"", + "/foo/bar.js", + ], +] +`) + }) + + it('should process ts input with babel', () => { babel = { process: jest.fn(s => `babel:${s}`) } expect(process()).toBe(`babel:ts:${INPUT}`) expect(config.shouldStringifyContent.mock.calls).toMatchInlineSnapshot(` @@ -115,6 +136,40 @@ Array [ `) }) + it('should process js input with babel', () => { + typescript.options.allowJs = true + babel = { process: jest.fn(s => `babel:${s}`) } + args[1] = '/foo/bar.js' + expect(process()).toBe(`babel:ts:${INPUT}`) + expect(config.shouldStringifyContent.mock.calls).toMatchInlineSnapshot(` +Array [ + Array [ + "/foo/bar.js", + ], +] +`) + expect(config.babelJestTransformer.process.mock.calls).toMatchInlineSnapshot(` +Array [ + Array [ + "ts:export default \\"foo\\"", + "/foo/bar.js", + Object {}, + Object { + "instrument": false, + }, + ], +] +`) + expect(config.tsCompiler.compile.mock.calls).toMatchInlineSnapshot(` +Array [ + Array [ + "export default \\"foo\\"", + "/foo/bar.js", + ], +] +`) + }) + it('should return stringified version of file', () => { config.shouldStringifyContent.mockImplementation(() => true) expect(process()).toMatchInlineSnapshot(`"module.exports=\\"export default \\\\\\"foo\\\\\\"\\""`) diff --git a/src/ts-jest-transformer.ts b/src/ts-jest-transformer.ts index fa443a6d78..bcbeafa47e 100644 --- a/src/ts-jest-transformer.ts +++ b/src/ts-jest-transformer.ts @@ -111,7 +111,7 @@ export class TsJestTransformer implements jest.Transformer { // 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 if (isTsFile) { + } else if (isJsFile || isTsFile) { // transpile TS code (source maps are included) result = configs.tsCompiler.compile(source, filePath) } else { From 9e7d6a0cb11f9c77e4c77e67c4ef05a74caa8082 Mon Sep 17 00:00:00 2001 From: Huafu Gandon Date: Thu, 20 Sep 2018 21:02:47 +0200 Subject: [PATCH 2/2] test(config): adds test related to allowJs --- e2e/__cases__/allow-js/bar.spec.ts | 10 +++ e2e/__cases__/allow-js/bar.ts | 1 + e2e/__cases__/allow-js/foo.js | 1 + e2e/__cases__/allow-js/foo.spec.js | 10 +++ e2e/__cases__/allow-js/tsconfig.json | 6 ++ .../__snapshots__/allow-js.test.ts.snap | 86 +++++++++++++++++++ e2e/__tests__/allow-js.test.ts | 14 +++ 7 files changed, 128 insertions(+) create mode 100644 e2e/__cases__/allow-js/bar.spec.ts create mode 100644 e2e/__cases__/allow-js/bar.ts create mode 100644 e2e/__cases__/allow-js/foo.js create mode 100644 e2e/__cases__/allow-js/foo.spec.js create mode 100644 e2e/__cases__/allow-js/tsconfig.json create mode 100644 e2e/__tests__/__snapshots__/allow-js.test.ts.snap create mode 100644 e2e/__tests__/allow-js.test.ts diff --git a/e2e/__cases__/allow-js/bar.spec.ts b/e2e/__cases__/allow-js/bar.spec.ts new file mode 100644 index 0000000000..b2be7ec5e8 --- /dev/null +++ b/e2e/__cases__/allow-js/bar.spec.ts @@ -0,0 +1,10 @@ +import * as foo from './foo' +import bar = require('./bar') + +test('foo', () => { + expect(foo).toBe('FOO!') +}) + +test('bar', () => { + expect(bar).toBe('BAR!') +}) diff --git a/e2e/__cases__/allow-js/bar.ts b/e2e/__cases__/allow-js/bar.ts new file mode 100644 index 0000000000..03df82b30b --- /dev/null +++ b/e2e/__cases__/allow-js/bar.ts @@ -0,0 +1 @@ +export = 'BAR!' diff --git a/e2e/__cases__/allow-js/foo.js b/e2e/__cases__/allow-js/foo.js new file mode 100644 index 0000000000..73cfb2112e --- /dev/null +++ b/e2e/__cases__/allow-js/foo.js @@ -0,0 +1 @@ +module.exports = 'FOO!' diff --git a/e2e/__cases__/allow-js/foo.spec.js b/e2e/__cases__/allow-js/foo.spec.js new file mode 100644 index 0000000000..d6eb95ee1a --- /dev/null +++ b/e2e/__cases__/allow-js/foo.spec.js @@ -0,0 +1,10 @@ +const foo = require('./foo') +const bar = require('./bar') + +test('foo', () => { + expect(foo).toBe('FOO!') +}) + +test('bar', () => { + expect(bar).toBe('BAR!') +}) diff --git a/e2e/__cases__/allow-js/tsconfig.json b/e2e/__cases__/allow-js/tsconfig.json new file mode 100644 index 0000000000..26f31aa261 --- /dev/null +++ b/e2e/__cases__/allow-js/tsconfig.json @@ -0,0 +1,6 @@ +{ + "compilerOptions": { + "target": "es5", + "allowJs": true + } +} diff --git a/e2e/__tests__/__snapshots__/allow-js.test.ts.snap b/e2e/__tests__/__snapshots__/allow-js.test.ts.snap new file mode 100644 index 0000000000..33e8f288c0 --- /dev/null +++ b/e2e/__tests__/__snapshots__/allow-js.test.ts.snap @@ -0,0 +1,86 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Allow JS test should pass using template "default" 1`] = ` + √ jest + ↳ exit code: 0 + ===[ STDOUT ]=================================================================== + + ===[ STDERR ]=================================================================== + PASS ./bar.spec.ts + PASS ./foo.spec.js + + Test Suites: 2 passed, 2 total + Tests: 4 passed, 4 total + Snapshots: 0 total + Time: XXs + Ran all test suites. + ================================================================================ +`; + +exports[`Allow JS test should pass using template "with-babel-6" 1`] = ` + √ jest + ↳ exit code: 0 + ===[ STDOUT ]=================================================================== + + ===[ STDERR ]=================================================================== + PASS ./bar.spec.ts + PASS ./foo.spec.js + + Test Suites: 2 passed, 2 total + Tests: 4 passed, 4 total + Snapshots: 0 total + Time: XXs + Ran all test suites. + ================================================================================ +`; + +exports[`Allow JS test should pass using template "with-babel-7" 1`] = ` + √ jest + ↳ exit code: 0 + ===[ STDOUT ]=================================================================== + + ===[ STDERR ]=================================================================== + PASS ./bar.spec.ts + PASS ./foo.spec.js + + Test Suites: 2 passed, 2 total + Tests: 4 passed, 4 total + Snapshots: 0 total + Time: XXs + Ran all test suites. + ================================================================================ +`; + +exports[`Allow JS test should pass using template "with-jest-22" 1`] = ` + √ jest + ↳ exit code: 0 + ===[ STDOUT ]=================================================================== + + ===[ STDERR ]=================================================================== + PASS ./bar.spec.ts + PASS ./foo.spec.js + + Test Suites: 2 passed, 2 total + Tests: 4 passed, 4 total + Snapshots: 0 total + Time: XXs + Ran all test suites. + ================================================================================ +`; + +exports[`Allow JS test should pass using template "with-typescript-2-7" 1`] = ` + √ jest + ↳ exit code: 0 + ===[ STDOUT ]=================================================================== + + ===[ STDERR ]=================================================================== + PASS ./bar.spec.ts + PASS ./foo.spec.js + + Test Suites: 2 passed, 2 total + Tests: 4 passed, 4 total + Snapshots: 0 total + Time: XXs + Ran all test suites. + ================================================================================ +`; diff --git a/e2e/__tests__/allow-js.test.ts b/e2e/__tests__/allow-js.test.ts new file mode 100644 index 0000000000..b3a121b1a8 --- /dev/null +++ b/e2e/__tests__/allow-js.test.ts @@ -0,0 +1,14 @@ +import { allValidPackageSets } from '../__helpers__/templates' +import { configureTestCase } from '../__helpers__/test-case' + +describe('Allow JS test', () => { + const testCase = configureTestCase('allow-js') + + testCase.runWithTemplates(allValidPackageSets, 0, (runTest, { testLabel }) => { + it(testLabel, () => { + const result = runTest() + expect(result.status).toBe(0) + expect(result).toMatchSnapshot() + }) + }) +})