Skip to content

Commit

Permalink
fix: make babel-jest warn when file to tarnsform is ignored by babel (j…
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB authored and captain-yossarian committed Jul 18, 2019
1 parent 6d4d34f commit 223124d
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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

Expand Down
9 changes: 9 additions & 0 deletions 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 |
Expand Down
12 changes: 12 additions & 0 deletions e2e/__tests__/transform.test.js
Expand Up @@ -12,6 +12,7 @@ import {
cleanup,
copyDir,
createEmptyPackage,
extractSummary,
linkJestPackage,
run,
} from '../Utils';
Expand Down Expand Up @@ -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', () => {
Expand Down
12 changes: 12 additions & 0 deletions 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);
});
3 changes: 3 additions & 0 deletions 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']};
5 changes: 5 additions & 0 deletions e2e/transform/babel-jest-ignored/package.json
@@ -0,0 +1,5 @@
{
"jest": {
"testEnvironment": "node"
}
}
4 changes: 3 additions & 1 deletion packages/babel-jest/package.json
Expand Up @@ -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"
Expand Down
19 changes: 17 additions & 2 deletions packages/babel-jest/src/index.js
Expand Up @@ -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');
Expand All @@ -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,
Expand Down

0 comments on commit 223124d

Please sign in to comment.