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

Created on 7 May 2017  ·  21Comments  ·  Source: facebook/jest


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
Bug

Most helpful comment

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

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

All 21 comments

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

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 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.

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

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

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.

Addressed in #3538

Thanks @tim-mc, works for me as well 👍

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 .

@thymikee in my case <rootDir> was used in setupTestFrameworkScriptFile property and the error is still present in [email protected]

We're still seeing this issue on [email protected], same as @le0nik with the setupTestFrameworkScriptFile.

The issue is that <rootDir> gets replaced with the folder in which jest-config.json lives, and not with the:

rootDir [string]
Default: The root of the directory containing the package.json or the pwd if no package.json is found

I opened #3613 for that

@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.

This is preventing us from upgrading past Jest 19 :(

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

@aaronabramov is this fixed in jest@test?

Not fixed in jest@21.

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>.

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.

Confirming that the issue I described above is resolved in [email protected].. Thank you!

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.

This works for me too:

module.exports = { 
  rootDir: process.cwd(),
};

check your package.json file is setupTestFrameworkScriptFile path correct.

Was this page helpful?
0 / 5 - 0 ratings