jest: Jest 20 can't resolve path relative to rootDir if ran from the project root

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

What is the current behavior?

● Validation Error:

  Module <rootDir>/test/setup.js in the setupTestFrameworkScriptFile option was not found.

  Configuration Documentation:
  https://facebook.github.io/jest/docs/configuration.html

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.

package.json

{
  "scripts": "jest --config test/config.json"
}

test/config.json

{
  "setupTestFrameworkScriptFile": "<rootDir>/test/setup.js"
}

test/setup.js file is present.

Example repo: https://github.com/le0nik/jest-validation-error After getting ValidationError with jest@20 install jest@19 and the test will run.

What is the expected behavior? Jest runs tests like it does in jest@19

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

  • OSX 10.12.4
  • Node.js 7.10.0
  • npm 4.2.0
  • Jest 20.0.0

About this issue

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

Commits related to this issue

Most upvoted comments

I was able to work around this bu duplicating roots to modulePaths, e.g.:

  roots: ['<rootDir>/src'],
  modulePaths: ['<rootDir>/src'],

I was able to fix mine with a simple change:

"jest": {
     "moduleDirectories": [
       "node_modules",
-      "<rootDir>/src",
-      "<rootDir>/test"
+      "src",
+      "test"
     ],
     "moduleNameMapper": {

simple for me likely because my rootDir was .

I am also having similar issues where after upgrading from 19.0.0 to 20.0.0, where the runners broke due to not being able to match anything via testMatch which was working fine in 19.0.0. My configuration is:

{
  "collectCoverageFrom": ["src/**/*.js"],
  "coverageDirectory": "<rootDir>/coverage",
  "coveragePathIgnorePatterns": [
      "/node_modules/"
  ],
  "moduleNameMapper": {
	"\\.(less)$": "identity-obj-proxy"
  },
  "setupFiles": ["<rootDir>/__tests__/environ-jest.js"],
  "testMatch": ["<rootDir>/src/**/*.test.js"]
}

After upgrading to 20.0.0 I’m getting:

No tests found
In /Users/evgueni.naverniouk/Git/ux
  483 files checked.
  testMatch: <rootDir>/src/**/*.test.js - 0 matches
  testPathIgnorePatterns: /node_modules/ - 483 matches
Pattern: "" - 0 matches

Rolling back to 19.0.0 makes the issue go away and my tests are found without issue.

This is preventing us from upgrading past Jest 19 😦

I also ran into this exact same problem. I had my test config.json in /test/, and using the --showConfig flag revealed that my rootDir was being set to /test rather than the root directory where my package.json lives. Setting rootDir to ../ is my current workaround.

@evan-scott-zocdoc same thing. Have to stay on Jest 19 and just read and listen about how delightful Jest 20 experience is.

As a workaround for people waiting for this to be resolved, adding a "rootDir" key to your jest config might do the trick, e.g.: "rootDir": "../". Found here.

Yes this is definitely a bug. We need to spend some time fixing up the new config resolution.

@bumbu yeap. Found the culprit to be this line: https://github.com/facebook/jest/blob/master/packages/jest-config/src/index.js#L50.

If the path to config is passed in argv.config, then rawOptions is equal to that path and jest sets the root to the directory that contains the config.

I can also add that using

"moduleDirectories": [
  "node_modules",
  "<rootDir>/src/shared"
]

also stopped working (nothing in src/shared was found/resolved correctly) when I tried upgrading to v20. It sounds related to me but I could just be doing something wrong.

I made some investigations and submitted a PR with explanations https://github.com/facebook/jest/pull/4587. The issue is fixed in a PR, but it creates a new problem. I hope people subscribed to this issue could cheap in and help fix an issue with <rootDir>.