jest: Jest 20 has problems reading config through --config CLI option

Do you want to request a feature or report a bug?

Bug

What is the current behavior?

Since switching to jest 20, it doesn’t apply my exclusion patterns from the collectCoverageFrom setting, whereas jest 29 used to do this correctly.

If the current behavior is a bug, please provide the steps to reproduce and either a repl.it demo through https://repl.it/languages/jest or a minimal repository on GitHub that we can yarn install and yarn test.

I’m using this config:

{
  "collectCoverageFrom": [
    "src/**/*.{js,jsx}",
    "!src/{client,server,shared}/*.{js,jsx}",
    "!src/{client,server,shared}/{components,data,services}/*/index.js",
    "!src/client/services/analytics/**",
    "!src/shared/services/raven/**"
  ]
}

For jest 19, this config would correctly exclude the ignored files from the coverage report, whereas jest 20 still includes them in the coverage report.

jest 19: https://coveralls.io/builds/11407983 jest 20: https://coveralls.io/builds/11453060

What is the expected behavior?

For jest to apply the exclusion patterns and ignore the matching files when reporting coverage.

Please provide your exact Jest configuration and mention your Jest, node, yarn/npm version and operating system.

Jest 20 yarn 0.24.4 node 7.10.0 macos sierra

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 3
  • Comments: 33 (2 by maintainers)

Most upvoted comments

I also ran into issues with the --config option after upgrading, and this is what I have found:

  • The configuration file rootDir is no longer relative to the current working directory (in which jest is called), but instead relative to the config file location. This is a good change, but I haven’t seen it documented anywhere.
  • These changes cause module resolution errors which are thrown here, but appear to get swallowed somewhere, causing Jest to simply exit with error code 1.

tl;dr: I had a jest.config.json file with rootDir: '' in a subdirectory. Changing it to rootDir: '..' makes the tests run again.

Ok i’ve tested it and the problem looks like if the file doesn’t have .json extension, it doesn’t read it. I’ve renamed my config files to jest.config.json and jest-ci.config.json and they both work as expected. Thanks @pjnovas!

After testing some more, it seems that jest 20 does not load my .jestrc config (used in combination with jest --config .jestrc --coverage. Which did work with jest 19.

After including the config in package.json instead of separately, excluding files from coverage works again. Weird that this broke though.

I’m having trouble too.

  • If my config is called jest.config.js and has module.exports, it works.
  • If it’s jest.config.json with plain JSON content, it does not work.
  • .jestrc with JSON content or module.exports does not work.
  • Putting the config in the jest key in package.json works.

Running jest --showConfig correctly shows my root directory, but it just doesn’t seem to want to use the .jestrc file. It’s strange, but I’ve simply added the config to the package.json and it’s no big deal. But it’s a little confusing because I believe the two failing options I mentioned are supposed to work, right? (I’m new to Jest, so I’m not sure what the Jest 19 behavior was.)

We’re experiencing a very terse error in the lerna project when attempting to run jest 20.0.3 with a --config option (that previously worked in jest 19): https://github.com/evocateur/lerna/tree/upgrade-jest

After cloning and checking out the branch:

yarn
yarn integration

What it yields (after the preintegration lifecycle script runs):

$ jest --config test/config/integration.json 

error Command failed with exit code 1.

test/config/integration.json

{
  "bail": true,
  "roots": [
    "<rootDir>/test/integration"
  ],
  "testEnvironment": "node",
  "setupTestFrameworkScriptFile": "<rootDir>/test/integration/_setupTestFramework.js",
  "snapshotSerializers": [
    "<rootDir>/test/helpers/serializePlaceholders.js"
  ],
  "verbose": true
}

yarn jest -- --showConfig --config=test/config/integration.json doesn’t even output the config, as the default without --config does successfully.

hate this bug to ☑️

As a workaround for now you may want to set "rootDir": "../../" or so.

For those having silent failures, check that you’re node version is > 6.

Only jest.config.js and jest key in package.json are automatically taken as your config. for .jestrc you need to specify --config flag. E.g. jest --config=.jestrc

I was having a similar issue to where jest 20.0.4 would just quit with no feedback and no tests run when I used the --config option. Running the same command through npm at least gave me Exit status 1, but that really didn’t clear up what the problem was.

My jest.config.json isn’t in my project root, but I use <rootDir> within it. I didn’t have a rootDir option, though. As soon as I added a rootDir entry with a path back to where jest is run from, it started working. This wasn’t needed in jest 19, and the lack of feedback turned it into a guessing game for me to try to figure out what the problem was.