Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make babel-jest warn when file to tarnsform is ignored by babel #7797

Merged
merged 5 commits into from Feb 4, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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:124: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"
}
}
3 changes: 2 additions & 1 deletion packages/babel-jest/package.json
Expand Up @@ -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"
Expand Down
18 changes: 16 additions & 2 deletions packages/babel-jest/src/index.js
Expand Up @@ -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');
Expand All @@ -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(
SimenB marked this conversation as resolved.
Show resolved Hide resolved
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