diff --git a/CHANGELOG.md b/CHANGELOG.md index 86f2904e9ca8..1116fd8a46d9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ - `[jest-cli]` Load transformers before installing require hooks ([#7752](https://github.com/facebook/jest/pull/7752) - `[jest-cli]` Handle missing `numTodoTests` in test results ([#7779](https://github.com/facebook/jest/pull/7779)) - `[jest-runtime]` Exclude setup/teardown files from coverage report ([#7790](https://github.com/facebook/jest/pull/7790) +- `[babel-jest]` Throw an error if `babel-jest` tries to transform a file ignored by Babel ([#7797](https://github.com/facebook/jest/pull/7797)) ### Chore & Maintenance diff --git a/e2e/__tests__/__snapshots__/transform.test.js.snap b/e2e/__tests__/__snapshots__/transform.test.js.snap index aad749c2f0ad..791e7ec6ae94 100644 --- a/e2e/__tests__/__snapshots__/transform.test.js.snap +++ b/e2e/__tests__/__snapshots__/transform.test.js.snap @@ -1,5 +1,14 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`babel-jest ignored tells user to match ignored files 1`] = ` +FAIL __tests__/ignoredFile.test.js + ● Test suite failed to run + + babel-jest: Babel ignores __tests__/ignoredFile.test.js - make sure to include the file in Jest's transformIgnorePatterns as well. + + at loadBabelConfig (../../../packages/babel-jest/build/index.js:134:13) +`; + exports[`babel-jest instruments only specific files and collects coverage 1`] = ` ------------|----------|----------|----------|----------|-------------------| File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s | diff --git a/e2e/__tests__/transform.test.js b/e2e/__tests__/transform.test.js index 50dba3b10f26..c9f12a0c406a 100644 --- a/e2e/__tests__/transform.test.js +++ b/e2e/__tests__/transform.test.js @@ -12,6 +12,7 @@ import { cleanup, copyDir, createEmptyPackage, + extractSummary, linkJestPackage, run, } from '../Utils'; @@ -45,6 +46,17 @@ describe('babel-jest', () => { }); }); +describe('babel-jest ignored', () => { + const dir = path.resolve(__dirname, '..', 'transform/babel-jest-ignored'); + + it('tells user to match ignored files', () => { + // --no-cache because babel can cache stuff and result in false green + const {status, stderr} = runJest(dir, ['--no-cache']); + expect(status).toBe(1); + expect(wrap(extractSummary(stderr).rest)).toMatchSnapshot(); + }); +}); + // 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-ignored/__tests__/ignoredFile.test.js b/e2e/transform/babel-jest-ignored/__tests__/ignoredFile.test.js new file mode 100644 index 000000000000..89e5137d7342 --- /dev/null +++ b/e2e/transform/babel-jest-ignored/__tests__/ignoredFile.test.js @@ -0,0 +1,12 @@ +/** + * 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'; + +it('should not run since the file is ignored by babel config', () => { + expect(true).toBe(true); +}); diff --git a/e2e/transform/babel-jest-ignored/babel.config.js b/e2e/transform/babel-jest-ignored/babel.config.js new file mode 100644 index 000000000000..ef0850effc4a --- /dev/null +++ b/e2e/transform/babel-jest-ignored/babel.config.js @@ -0,0 +1,3 @@ +// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + +module.exports = {only: ['blablabla']}; diff --git a/e2e/transform/babel-jest-ignored/package.json b/e2e/transform/babel-jest-ignored/package.json new file mode 100644 index 000000000000..148788b25446 --- /dev/null +++ b/e2e/transform/babel-jest-ignored/package.json @@ -0,0 +1,5 @@ +{ + "jest": { + "testEnvironment": "node" + } +} diff --git a/packages/babel-jest/package.json b/packages/babel-jest/package.json index b9e484631a0e..b05b51da1646 100644 --- a/packages/babel-jest/package.json +++ b/packages/babel-jest/package.json @@ -11,7 +11,9 @@ "main": "build/index.js", "dependencies": { "babel-plugin-istanbul": "^5.1.0", - "babel-preset-jest": "^24.0.0" + "babel-preset-jest": "^24.0.0", + "chalk": "^2.4.2", + "slash": "^2.0.0" }, "devDependencies": { "@babel/core": "^7.1.0" diff --git a/packages/babel-jest/src/index.js b/packages/babel-jest/src/index.js index bae5553dbe6c..b659d715588f 100644 --- a/packages/babel-jest/src/index.js +++ b/packages/babel-jest/src/index.js @@ -19,6 +19,8 @@ import crypto from 'crypto'; import fs from 'fs'; import path from 'path'; import {transformSync as babelTransform, loadPartialConfig} from '@babel/core'; +import chalk from 'chalk'; +import slash from 'slash'; const THIS_FILE = fs.readFileSync(__filename); const jestPresetPath = require.resolve('babel-preset-jest'); @@ -40,9 +42,22 @@ const createTransformer = (options: any): Transformer => { delete options.cacheDirectory; delete options.filename; - const loadBabelConfig = (cwd, filename) => + function loadBabelConfig(cwd, filename) { // `cwd` first to allow incoming options to override it - loadPartialConfig({cwd, ...options, filename}); + const babelConfig = loadPartialConfig({cwd, ...options, filename}); + + if (!babelConfig) { + throw new Error( + `babel-jest: Babel ignores ${chalk.bold( + slash(path.relative(cwd, filename)), + )} - make sure to include the file in Jest's ${chalk.bold( + 'transformIgnorePatterns', + )} as well.`, + ); + } + + return babelConfig; + } return { canInstrument: true,