Jest: rootDir points to the folder that contains jest-config

Created on 19 May 2017  路  9Comments  路  Source: facebook/jest

<rootDir> points to the folder that contains jest-config instead of the root of the directory containing the package.json or the pwd if no package.json is found

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

What is the current behavior?
<rootDir> gets replaced with the path to the folder that contains config file.

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.
That works fine when your jest config lives in root dir, but if you place your jest config in a different folder (say test) then config paths will be wrong because a <rootDir>/src will be transformed into /myproj/test/src instead of myproj/src.

What is the expected behavior?
As per documentation (and behaviour in 19):

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

Please provide your exact Jest configuration and mention your Jest, node, yarn/npm version and operating system.
Jest: 20.0.3
Node: v7.5.0
NPM 4.1.2
macOS 10.12.4

jestConfig:

{
  "transform": {
    ".*": "<rootDir>/test/jest-preprocessor.js"
  },
  "moduleFileExtensions": [
    "ts",
    "tsx",
    "js",
    "json",
    "svg",
    "png",
    "gif"
  ],
  "setupTestFrameworkScriptFile": "<rootDir>/test/jest-setup.js",
  "testEnvironment": "node",
  "testRegex": "(/__tests__/.*|\\.tests?)\\.(ts|tsx)$",
  "roots": [
    "<rootDir>/src"
  ],
  "testResultsProcessor": "jest-teamcity-reporter",
  "coverageDirectory": "<rootDir>/build/coverage/",
  "coverageReporters": [
    "text-summary",
    "teamcity",
    "html",
    "json"
  ]
}

running with jest --config test/jest-config.json --forceExit --coverage

Bug

Most helpful comment

I added "rootDir": "../" to my jest config (my jest config is one level deeper than my project root. Works great now ... however I'm not sure if Jest is supposed to find my project root (for example through the package.json location) automatically, if I don't specify rootDir.

All 9 comments

I can confirm having exactly the same problem when upgrading from Jest 19 to Jest 20.

I'm using macOS 10.12.4, but for my team members using Windows, the "Validation Error" reporting the path which can't be resolved isn't displayed.

I added "rootDir": "../" to my jest config (my jest config is one level deeper than my project root. Works great now ... however I'm not sure if Jest is supposed to find my project root (for example through the package.json location) automatically, if I don't specify rootDir.

Awesome, thanks @bhouser! Adding a "rootDir" config fixed things for me.

"rootDir" will not work for monorepo's where the rootDir changes based on which packages are being tested. Adding --rootDir flag to the jest command is a work around we use.

@redonkulus for that use "roots" instead. "rootDir" is, for now, just for reporters to show proper path and for providing the project root (from where you run the command).

This is documented behavior (#4726)

For shared Jest configuration, a base config file somewhere in the root is a good option. You can even export a function there that accepts the root dir of the subproject and sets it as the rootDir. Each project then has a jest.config.js with only:

module.exports = require('../../config/jest/base.config.js')(__dirname);

For shared Jest configuration, a base config file somewhere in the root is a good option. You can even export a function there that accepts the root dir of the subproject and sets it as the rootDir. Each project then has a jest.config.js with only:

module.exports = require('../../config/jest/base.config.js')(__dirname);

Just to let you know I was researching for hours to find this answer. Very helpful, thanks!

Maybe worth a dedicated/prominent section in the docs - since it's quite a common case to do multi-root and currently takes a lot of research to find the answer.

Maybe worth a dedicated/prominent section in the docs - since it's quite a common case to do multi-root and currently takes a lot of research to find the answer.

Happy to accept a PR for this! :)

Was this page helpful?
0 / 5 - 0 ratings