diff --git a/CHANGELOG.md b/CHANGELOG.md index 5e4503e4dfeb..8caaba089165 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,9 @@ None for now ### Fixes +* `[babel-jest]` moduleFileExtensions not passed to babel transformer. + ([#4637](https://github.com/facebook/jest/issues/4637)) + ### Features ### Chore & Maintenance diff --git a/integration_tests/__tests__/transform.test.js b/integration_tests/__tests__/transform.test.js index fe0ec5603fec..9e360b37af43 100644 --- a/integration_tests/__tests__/transform.test.js +++ b/integration_tests/__tests__/transform.test.js @@ -120,3 +120,18 @@ describe('multiple-transformers', () => { expect(json.numPassedTests).toBe(1); }); }); + +describe('ecmascript-modules-support', () => { + const dir = path.resolve( + __dirname, + '..', + 'transform/ecmascript-modules-support', + ); + + it('runs transpiled code', () => { + // --no-cache because babel can cache stuff and result in false green + const {json} = runJest.json(dir, ['--no-cache']); + expect(json.success).toBe(true); + expect(json.numTotalTests).toBeGreaterThanOrEqual(1); + }); +}); diff --git a/integration_tests/transform/ecmascript-modules-support/.babelrc b/integration_tests/transform/ecmascript-modules-support/.babelrc new file mode 100644 index 000000000000..7b2ac3418b07 --- /dev/null +++ b/integration_tests/transform/ecmascript-modules-support/.babelrc @@ -0,0 +1,10 @@ +{ + "presets": [ + ["env", { + "targets": { + "node": "current" + }, + "modules": "commonjs" + }] + ] +} \ No newline at end of file diff --git a/integration_tests/transform/ecmascript-modules-support/__tests__/ecmascript-modules-support.mjs b/integration_tests/transform/ecmascript-modules-support/__tests__/ecmascript-modules-support.mjs new file mode 100644 index 000000000000..84c878a2595e --- /dev/null +++ b/integration_tests/transform/ecmascript-modules-support/__tests__/ecmascript-modules-support.mjs @@ -0,0 +1,13 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. 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'; + +import {foo} from '../src/module'; + +it('can be used with mjs files using babel-jest', () => { + expect(foo()).toBe('a'); +}); diff --git a/integration_tests/transform/ecmascript-modules-support/package.json b/integration_tests/transform/ecmascript-modules-support/package.json new file mode 100644 index 000000000000..b3145fb854df --- /dev/null +++ b/integration_tests/transform/ecmascript-modules-support/package.json @@ -0,0 +1,10 @@ +{ + "jest": { + "testEnvironment": "node", + "moduleFileExtensions": ["js", "json", "jsx", "node", "mjs"], + "testMatch": [ "**/__tests__/**/*.mjs"], + "transform": { + "^.+\\.mjs?$": "../../../packages/babel-jest" + } + } +} diff --git a/integration_tests/transform/ecmascript-modules-support/src/index.mjs b/integration_tests/transform/ecmascript-modules-support/src/index.mjs new file mode 100644 index 000000000000..311817aa3eec --- /dev/null +++ b/integration_tests/transform/ecmascript-modules-support/src/index.mjs @@ -0,0 +1,3 @@ +import {foo} from './module'; + +foo(); diff --git a/integration_tests/transform/ecmascript-modules-support/src/module.mjs b/integration_tests/transform/ecmascript-modules-support/src/module.mjs new file mode 100644 index 000000000000..73104d324790 --- /dev/null +++ b/integration_tests/transform/ecmascript-modules-support/src/module.mjs @@ -0,0 +1 @@ +export const foo = () => 'a'; diff --git a/packages/babel-jest/src/index.js b/packages/babel-jest/src/index.js index 0de53fb61b9c..4dd71bacbe87 100644 --- a/packages/babel-jest/src/index.js +++ b/packages/babel-jest/src/index.js @@ -104,7 +104,10 @@ const createTransformer = (options: any) => { config: ProjectConfig, transformOptions: TransformOptions, ): string { - if (babelUtil && !babelUtil.canCompile(filename)) { + const altExts = config.moduleFileExtensions.map( + extension => '.' + extension, + ); + if (babelUtil && !babelUtil.canCompile(filename, altExts)) { return src; }