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

Use process.cwd as config.rootDir fallback #4587

Closed
wants to merge 4 commits into from

Conversation

hudochenkov
Copy link

@hudochenkov hudochenkov commented Oct 2, 2017

Summary

Fixes #3499 and #3613.

I compared v19.0.2 and latest master.

In 19.0.2 rootDir is process.cwd() if rootDir isn't specified in a config: https://github.com/facebook/jest/blob/ed45267e9d8b8cabc83f69a538e03465d7376868/packages/jest-config/src/loadFromFile.js#L36-L38

In latest master rootDir is path.dirname(configPath): https://github.com/facebook/jest/blob/7aa3017f97a4dfa76f566f9bbeded5d03fdd35e7/packages/jest-config/src/read_config_file_and_set_root_dir.js#L54-L55

Apparently this change in behavior was made in #4122 and probably in #3472.

Test plan

I used https://github.com/hudochenkov/jest-rootdir-error provided #3499 for testing on a “real” project.

There is one failing test and I have no idea in what direction to go to fix it.

In this test configObject.rootDir is different.

With path.dirname(configPath) it's:

<<REPLACED>>/jest.config.js

While with process.cwd() it's:

<<REPLACED>>/jest.config.js/some/nested/directory

Current documentation says:

rootDir [string]

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

Which is true for a failing test case (in conditions when it's not failing), but it's not true for situations reported in issues mentioned above. For last situations rootDir is config dir.

I'm sorry this PR isn't complete. I hope my investigation will be a good headstart for fixing those issues.

@SimenB
Copy link
Member

SimenB commented Oct 24, 2017

This is failing CI.

Would it be possible to add an integration test as well that fails without your patch?

@hudochenkov hudochenkov force-pushed the fix-rootDir branch 2 times, most recently from 0f82f92 to 8bc7e2c Compare November 12, 2017 23:47
@hudochenkov
Copy link
Author

Thank you for your patience.

I've added failing test 8bc7e2c.

CI results are failing for this commit:

Jest shows this error (not visible in CI):

Validation Error:

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

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

After applying a fix (05484d7) the error is fixed, but another one was introduced:

P. S. Something strange with Jest's tests on local machine (macOS 10.12.6, Node 8.9.0, Yarn 1.3.2). I was doing everything according to Contributions instructions, but yarn test fails with over 500 errors on master (I see master in Github is also fails for quite some time). yarn jest -i fails once on master without any of my changes.

@cpojer
Copy link
Member

cpojer commented Feb 7, 2018

Can we take this over the finish line?

@hudochenkov
Copy link
Author

hudochenkov commented Feb 20, 2018

I would like too help. But I can't figure out how. I hope my research could help make proper fix.

I rebased PR on latest master and CI tests failing with totally unrelated error :(

@SimenB
Copy link
Member

SimenB commented Feb 21, 2018

Does yarn jest pass locally on your machine? To me it seems like your changes have made projects stop working properly

@hudochenkov
Copy link
Author

Same error. I did npm run clean-all, then yarn install, and then yarn test:

$ yarn test
yarn run v1.3.2
$ yarn typecheck && yarn lint && yarn jest
$ flow check --include-warnings
Found 0 errors
$ eslint . --cache --ext js,md
$ node ./packages/jest-cli/bin/jest.js
● Validation Error:

  Preset react-native not found.

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

error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

// If rootDir is not there, we'll set it to this file's __dirname
configObject.rootDir = path.dirname(configPath);
// If rootDir is not there, we'll set it to cwd
configObject.rootDir = process.cwd();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is the correct change - from what I can gather it looks at the filename now to support projects.

I'm not really sure what the correct behavior should be. I'm leading towards using cwd and requiring rootDir in the config of nested projects.

@rickhanlonii
Copy link
Member

@hudochenkov what do you think about @SimenB's last comment? Should we close this?

@hudochenkov
Copy link
Author

@rickhanlonii unfortunately, I'm not familiar with the concept of projects in Jest, so I don't understand the meaning of that comment.

Also, I won't be able to fix the problem without creating other problems. So this PR is more like research for someone who can actually fix #3613. Because currently, Jest doesn't behave like it's stated in the documentation for rootDir.

Feel free to close this PR, because I have doubts I contribute something helpful to Jest :(

@cpojer cpojer closed this Apr 28, 2018
@rickhanlonii
Copy link
Member

@hudochenkov ok, I think the move here would be to update the docs instead - if you want to submit that PR I'm happy to review and merge!

@github-actions
Copy link

This pull request 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 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

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