Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rootDir points to the folder that contains jest-config #3613

Closed
bumbu opened this issue May 19, 2017 · 10 comments
Closed

rootDir points to the folder that contains jest-config #3613

bumbu opened this issue May 19, 2017 · 10 comments
Labels

Comments

@bumbu
Copy link

bumbu commented May 19, 2017

<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

@bhouser
Copy link

bhouser commented May 30, 2017

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.

@bhouser
Copy link

bhouser commented May 30, 2017

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.

@mockdeep
Copy link

mockdeep commented Oct 6, 2017

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

mockdeep added a commit to mockdeep/questlog that referenced this issue Oct 6, 2017
Appears to be a bug where it gets the wrong `rootDir` in versions `20`
and up. Discovered a workaround that fixes the issue, though it appears
to be a bug.

jestjs/jest#3613 (comment)
@redonkulus
Copy link

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

@thymikee
Copy link
Collaborator

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

hudochenkov added a commit to hudochenkov/jest that referenced this issue Nov 11, 2017
hudochenkov added a commit to hudochenkov/jest that referenced this issue Nov 12, 2017
hudochenkov added a commit to hudochenkov/jest that referenced this issue Feb 20, 2018
@SimenB
Copy link
Member

SimenB commented Feb 3, 2019

This is documented behavior (#4726)

@SimenB SimenB closed this as completed Feb 3, 2019
@jeysal
Copy link
Contributor

jeysal commented Jul 10, 2019

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);

@samdoeswork
Copy link

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.

@jeysal
Copy link
Contributor

jeysal commented Nov 18, 2019

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! :)

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

8 participants