Jest: Bug with babel-jest when coverage mode is on

Created on 19 Apr 2019  路  4Comments  路  Source: facebook/jest

馃悰 Bug Report

Jest returns an error when babel ignore some directory and --coverage mode is enabled:

babel-jest: Babel ignores server/init/index.js - make sure to include the file in Jest's transformIgnorePatterns as well.

Adding this directory in transformIgnorePatterns - do not help.
When we disable coverage all is working properly.

To Reproduce

Steps to reproduce the behavior:
OS: Windows 10
babel-jest: 24.7.1
jest: 24.6.0

.babelrc
{ ... "only": [ "client/", "test/" ] ... }

jest-config.js
{ ... "transform": { "^.+\\.jsx?$": "babel-jest" }, "transformIgnorePatterns": [ "/server/", "/node_modules/" ], ... }

command
jest test/ --coverage --verbose

Bug Report Needs Repro Needs Triage

Most helpful comment

Using coveragePathIgnorePatterns isn't an option if you want the coverage report to include all tests, not just those that were transformed.

There's some kind of conflict between transform and transformIgnorePatterns when coverage is used. @ni1son, the pattern "^.+\\.jsx?$" is very permissive and matches all js/x files it finds. I would expect that adding directories to transformIgnorePatterns like you have would be sufficient. It actually seems to work fine without the coverage flag, but I ran into the same problem when I wanted to use coverage.

Workaround I found is to use a more specific pattern in transform so that I don't need to use transformIgnorePatterns at all:

config/jest.config.js

rootDir: '..',
roots: [
  '<rootDir>/config',
  '<rootDir>/scripts',
  '<rootDir>/src',
],
transform: {
  // Only transform files in src/!
  // Only this directory is built/transpiled by Babel.
  '\\/src\\/.+\\.[t|j]sx?$': '<rootDir>/config/babelJest.js',
}

config/babel.config.js

only: [
  '../src',
]

You can ignore most of the details, the relevant part is the specification of \\/src\\/ in '\\/src\\/.+\\.[t|j]sx?$', since that's the only directory I actually want transformed. The files and tests in config/ and in scripts/ are just run straight by Node.

All 4 comments

Try adding coveragePathIgnorePatterns.

source: https://github.com/facebook/jest/blob/master/docs/Configuration.md

Using coveragePathIgnorePatterns isn't an option if you want the coverage report to include all tests, not just those that were transformed.

There's some kind of conflict between transform and transformIgnorePatterns when coverage is used. @ni1son, the pattern "^.+\\.jsx?$" is very permissive and matches all js/x files it finds. I would expect that adding directories to transformIgnorePatterns like you have would be sufficient. It actually seems to work fine without the coverage flag, but I ran into the same problem when I wanted to use coverage.

Workaround I found is to use a more specific pattern in transform so that I don't need to use transformIgnorePatterns at all:

config/jest.config.js

rootDir: '..',
roots: [
  '<rootDir>/config',
  '<rootDir>/scripts',
  '<rootDir>/src',
],
transform: {
  // Only transform files in src/!
  // Only this directory is built/transpiled by Babel.
  '\\/src\\/.+\\.[t|j]sx?$': '<rootDir>/config/babelJest.js',
}

config/babel.config.js

only: [
  '../src',
]

You can ignore most of the details, the relevant part is the specification of \\/src\\/ in '\\/src\\/.+\\.[t|j]sx?$', since that's the only directory I actually want transformed. The files and tests in config/ and in scripts/ are just run straight by Node.

@shanedg Thanks so much for that workaround, that ended a long annoying session of Googling/debugging/cursing/teeth-gnashing for me.

Thank you @spktklr, that was exactly what I needed.

Was this page helpful?
0 / 5 - 0 ratings