diff --git a/CHANGELOG.md b/CHANGELOG.md index bdf022c9ad6e..b95746b8ceb7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -47,7 +47,7 @@ - `[@jest/transform]`: New package extracted from `jest-runtime` ([#7915](https://github.com/facebook/jest/pull/7915)) - `[babel-plugin-jest-hoist]`: Migrate to TypeScript ([#7898](https://github.com/facebook/jest/pull/7898)) - `[@jest/core]` Create new package, which is `jest-cli` minus `yargs` and `prompts` ([#7696](https://github.com/facebook/jest/pull/7696)) -- `[@jest/transform]`: Migrate to TypeScript ([#7918](https://github.com/facebook/jest/pull/7918)) +- `[@jest/transform]`: Migrate to TypeScript ([#7918](https://github.com/facebook/jest/pull/7918), [#7945](https://github.com/facebook/jest/pull/7945)) - `[docs]` Add missing import to docs ([#7928](https://github.com/facebook/jest/pull/7928)) - `[jest-resolve-dependencies]`: Migrate to TypeScript ([#7922](https://github.com/facebook/jest/pull/7922)) - `[expect]`: Migrate to TypeScript ([#7919](https://github.com/facebook/jest/pull/7919)) diff --git a/e2e/__tests__/transform.test.js b/e2e/__tests__/transform.test.js index c9f12a0c406a..8adc624a0249 100644 --- a/e2e/__tests__/transform.test.js +++ b/e2e/__tests__/transform.test.js @@ -57,6 +57,21 @@ describe('babel-jest ignored', () => { }); }); +describe('babel-jest with manual transformer', () => { + const dir = path.resolve(__dirname, '..', 'transform/babel-jest-manual'); + + beforeEach(() => { + run('yarn', dir); + }); + + it('runs transpiled code', () => { + // --no-cache because babel can cache stuff and result in false green + const {json} = runWithJson(dir, ['--no-cache']); + expect(json.success).toBe(true); + expect(json.numTotalTests).toBeGreaterThanOrEqual(1); + }); +}); + // babel-jest is automatically linked at the root because it is a workspace now // a way to test this in isolation is to move the test suite into a temp folder describe('no babel-jest', () => { diff --git a/e2e/transform/babel-jest-manual/__tests__/babelJest.test.js b/e2e/transform/babel-jest-manual/__tests__/babelJest.test.js new file mode 100644 index 000000000000..49af46513702 --- /dev/null +++ b/e2e/transform/babel-jest-manual/__tests__/babelJest.test.js @@ -0,0 +1,15 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +'use strict'; + +require('../foo'); + +it('strips flowtypes using babel-jest and .babelrc', () => { + const a: string = 'a'; + expect(a).toBe('a'); +}); diff --git a/e2e/transform/babel-jest-manual/foo.js b/e2e/transform/babel-jest-manual/foo.js new file mode 100644 index 000000000000..7e8e2873685b --- /dev/null +++ b/e2e/transform/babel-jest-manual/foo.js @@ -0,0 +1,10 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +const someFunction = (): null => null; + +module.exports = someFunction(); diff --git a/e2e/transform/babel-jest-manual/package.json b/e2e/transform/babel-jest-manual/package.json new file mode 100644 index 000000000000..6f6e06a0b254 --- /dev/null +++ b/e2e/transform/babel-jest-manual/package.json @@ -0,0 +1,11 @@ +{ + "dependencies": { + "@babel/preset-flow": "^7.0.0" + }, + "jest": { + "testEnvironment": "node", + "transform": { + "\\.js$": "/transformer.js" + } + } +} diff --git a/e2e/transform/babel-jest-manual/transformer.js b/e2e/transform/babel-jest-manual/transformer.js new file mode 100644 index 000000000000..597b36bc9ae5 --- /dev/null +++ b/e2e/transform/babel-jest-manual/transformer.js @@ -0,0 +1,13 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +const {createTransformer} = require('babel-jest'); + +module.exports = createTransformer({ + presets: ['@babel/preset-flow'], + root: __dirname, +}); diff --git a/e2e/transform/babel-jest-manual/yarn.lock b/e2e/transform/babel-jest-manual/yarn.lock new file mode 100644 index 000000000000..a698b6900465 --- /dev/null +++ b/e2e/transform/babel-jest-manual/yarn.lock @@ -0,0 +1,31 @@ +# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. +# yarn lockfile v1 + + +"@babel/helper-plugin-utils@^7.0.0": + version "7.0.0" + resolved "https://registry.yarnpkg.com/@babel/helper-plugin-utils/-/helper-plugin-utils-7.0.0.tgz#bbb3fbee98661c569034237cc03967ba99b4f250" + integrity sha512-CYAOUCARwExnEixLdB6sDm2dIJ/YgEAKDM1MOeMeZu9Ld/bDgVo8aiWrXwcY7OBh+1Ea2uUcVRcxKk0GJvW7QA== + +"@babel/plugin-syntax-flow@^7.0.0": + version "7.0.0" + resolved "https://registry.yarnpkg.com/@babel/plugin-syntax-flow/-/plugin-syntax-flow-7.0.0.tgz#70638aeaad9ee426bc532e51523cff8ff02f6f17" + integrity sha512-zGcuZWiWWDa5qTZ6iAnpG0fnX/GOu49pGR5PFvkQ9GmKNaSphXQnlNXh/LG20sqWtNrx/eB6krzfEzcwvUyeFA== + dependencies: + "@babel/helper-plugin-utils" "^7.0.0" + +"@babel/plugin-transform-flow-strip-types@^7.0.0": + version "7.0.0" + resolved "https://registry.yarnpkg.com/@babel/plugin-transform-flow-strip-types/-/plugin-transform-flow-strip-types-7.0.0.tgz#c40ced34c2783985d90d9f9ac77a13e6fb396a01" + integrity sha512-WhXUNb4It5a19RsgKKbQPrjmy4yWOY1KynpEbNw7bnd1QTcrT/EIl3MJvnGgpgvrKyKbqX7nUNOJfkpLOnoDKA== + dependencies: + "@babel/helper-plugin-utils" "^7.0.0" + "@babel/plugin-syntax-flow" "^7.0.0" + +"@babel/preset-flow@^7.0.0": + version "7.0.0" + resolved "https://registry.yarnpkg.com/@babel/preset-flow/-/preset-flow-7.0.0.tgz#afd764835d9535ec63d8c7d4caf1c06457263da2" + integrity sha512-bJOHrYOPqJZCkPVbG1Lot2r5OSsB+iUOaxiHdlOeB1yPWS6evswVHwvkDLZ54WTaTRIk89ds0iHmGZSnxlPejQ== + dependencies: + "@babel/helper-plugin-utils" "^7.0.0" + "@babel/plugin-transform-flow-strip-types" "^7.0.0" diff --git a/packages/babel-jest/src/index.ts b/packages/babel-jest/src/index.ts index 2df36fe37d22..4e7ac6b3099f 100644 --- a/packages/babel-jest/src/index.ts +++ b/packages/babel-jest/src/index.ts @@ -26,7 +26,6 @@ const babelIstanbulPlugin = require.resolve('babel-plugin-istanbul'); // Narrow down the types interface BabelJestTransformer extends Transformer { canInstrument: true; - createTransformer: (options?: TransformOptions) => BabelJestTransformer; } const createTransformer = ( @@ -65,9 +64,8 @@ const createTransformer = ( return babelConfig; } - const transformer: BabelJestTransformer = { + return { canInstrument: true, - createTransformer, getCacheKey( fileData, filename, @@ -131,8 +129,15 @@ const createTransformer = ( return src; }, }; +}; - return transformer; +const transformer: BabelJestTransformer & { + createTransformer: (options?: TransformOptions) => BabelJestTransformer; +} = { + ...createTransformer(), + // Assigned here so only the exported transformer has `createTransformer`, + // instead of all created transformers by the function + createTransformer, }; -export = createTransformer(); +export = transformer;