From ac562dcb0735d5554fc4521efb4d9723a3a9b1a0 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Mon, 4 Feb 2019 15:15:43 +0100 Subject: [PATCH 1/5] fix: make babel-jest warn when file to tarnsform is ignored by babel --- CHANGELOG.md | 1 + .../__snapshots__/transform.test.js.snap | 9 +++++++++ e2e/__tests__/transform.test.js | 13 ++++++++++++- .../__tests__/ignoredFile.test.js | 12 ++++++++++++ .../babel-jest-ignored/babel.config.js | 3 +++ e2e/transform/babel-jest-ignored/package.json | 5 +++++ packages/babel-jest/package.json | 3 ++- packages/babel-jest/src/index.js | 18 ++++++++++++++++-- 8 files changed, 60 insertions(+), 4 deletions(-) create mode 100644 e2e/transform/babel-jest-ignored/__tests__/ignoredFile.test.js create mode 100644 e2e/transform/babel-jest-ignored/babel.config.js create mode 100644 e2e/transform/babel-jest-ignored/package.json diff --git a/CHANGELOG.md b/CHANGELOG.md index 86f2904e9ca8..367670c3459f 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 ### Chore & Maintenance diff --git a/e2e/__tests__/__snapshots__/transform.test.js.snap b/e2e/__tests__/__snapshots__/transform.test.js.snap index aad749c2f0ad..450131246c44 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:124: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..268207c3ee74 100644 --- a/e2e/__tests__/transform.test.js +++ b/e2e/__tests__/transform.test.js @@ -11,7 +11,7 @@ import path from 'path'; import { cleanup, copyDir, - createEmptyPackage, + createEmptyPackage, extractSummary, linkJestPackage, run, } from '../Utils'; @@ -45,6 +45,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..227791ba49fd 100644 --- a/packages/babel-jest/package.json +++ b/packages/babel-jest/package.json @@ -11,7 +11,8 @@ "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" }, "devDependencies": { "@babel/core": "^7.1.0" diff --git a/packages/babel-jest/src/index.js b/packages/babel-jest/src/index.js index bae5553dbe6c..a15c3c13a4f4 100644 --- a/packages/babel-jest/src/index.js +++ b/packages/babel-jest/src/index.js @@ -19,6 +19,7 @@ import crypto from 'crypto'; import fs from 'fs'; import path from 'path'; import {transformSync as babelTransform, loadPartialConfig} from '@babel/core'; +import chalk from 'chalk'; const THIS_FILE = fs.readFileSync(__filename); const jestPresetPath = require.resolve('babel-preset-jest'); @@ -40,9 +41,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( + path.relative(cwd, filename), + )} - make sure to include the file in Jest's ${chalk.bold( + 'transformIgnorePatterns', + )} as well.`, + ); + } + + return babelConfig; + } return { canInstrument: true, From 11b271d64ac9d142d1ff1023b79402872dd860f0 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Mon, 4 Feb 2019 15:20:14 +0100 Subject: [PATCH 2/5] link to PR --- CHANGELOG.md | 2 +- e2e/__tests__/transform.test.js | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 367670c3459f..1116fd8a46d9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,7 +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 +- `[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__/transform.test.js b/e2e/__tests__/transform.test.js index 268207c3ee74..c9f12a0c406a 100644 --- a/e2e/__tests__/transform.test.js +++ b/e2e/__tests__/transform.test.js @@ -11,7 +11,8 @@ import path from 'path'; import { cleanup, copyDir, - createEmptyPackage, extractSummary, + createEmptyPackage, + extractSummary, linkJestPackage, run, } from '../Utils'; From 2061b299deb633c5584452ef8c7603519a05e8ab Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Mon, 4 Feb 2019 15:31:28 +0100 Subject: [PATCH 3/5] lowercase --- e2e/__tests__/__snapshots__/transform.test.js.snap | 2 +- packages/babel-jest/src/index.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/e2e/__tests__/__snapshots__/transform.test.js.snap b/e2e/__tests__/__snapshots__/transform.test.js.snap index 450131246c44..be4954e79b4f 100644 --- a/e2e/__tests__/__snapshots__/transform.test.js.snap +++ b/e2e/__tests__/__snapshots__/transform.test.js.snap @@ -4,7 +4,7 @@ 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. + 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:124:13) `; diff --git a/packages/babel-jest/src/index.js b/packages/babel-jest/src/index.js index a15c3c13a4f4..246d49d890b5 100644 --- a/packages/babel-jest/src/index.js +++ b/packages/babel-jest/src/index.js @@ -47,7 +47,7 @@ const createTransformer = (options: any): Transformer => { if (!babelConfig) { throw new Error( - `Babel-Jest: Babel ignores ${chalk.bold( + `babel-jest: Babel ignores ${chalk.bold( path.relative(cwd, filename), )} - make sure to include the file in Jest's ${chalk.bold( 'transformIgnorePatterns', From 371b62c029c3dc01b804cd2f638d47a3022893b7 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Mon, 4 Feb 2019 16:07:48 +0100 Subject: [PATCH 4/5] windows <3 --- packages/babel-jest/package.json | 3 ++- packages/babel-jest/src/index.js | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/babel-jest/package.json b/packages/babel-jest/package.json index 227791ba49fd..b05b51da1646 100644 --- a/packages/babel-jest/package.json +++ b/packages/babel-jest/package.json @@ -12,7 +12,8 @@ "dependencies": { "babel-plugin-istanbul": "^5.1.0", "babel-preset-jest": "^24.0.0", - "chalk": "^2.4.2" + "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 246d49d890b5..b659d715588f 100644 --- a/packages/babel-jest/src/index.js +++ b/packages/babel-jest/src/index.js @@ -20,6 +20,7 @@ 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'); @@ -48,7 +49,7 @@ const createTransformer = (options: any): Transformer => { if (!babelConfig) { throw new Error( `babel-jest: Babel ignores ${chalk.bold( - path.relative(cwd, filename), + slash(path.relative(cwd, filename)), )} - make sure to include the file in Jest's ${chalk.bold( 'transformIgnorePatterns', )} as well.`, From 32e61a451bfb6b7b80a67bde1a3d9c3c458aaaf1 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Mon, 4 Feb 2019 16:35:12 +0100 Subject: [PATCH 5/5] update snapshot --- e2e/__tests__/__snapshots__/transform.test.js.snap | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/e2e/__tests__/__snapshots__/transform.test.js.snap b/e2e/__tests__/__snapshots__/transform.test.js.snap index be4954e79b4f..791e7ec6ae94 100644 --- a/e2e/__tests__/__snapshots__/transform.test.js.snap +++ b/e2e/__tests__/__snapshots__/transform.test.js.snap @@ -6,7 +6,7 @@ FAIL __tests__/ignoredFile.test.js 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:124:13) + at loadBabelConfig (../../../packages/babel-jest/build/index.js:134:13) `; exports[`babel-jest instruments only specific files and collects coverage 1`] = `