-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
docs: mention rootDir
changes when using projects
#12871
docs: mention rootDir
changes when using projects
#12871
Conversation
Hi @kettanaito! Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at cla@fb.com. Thanks! |
Just a friendly note, I do understand that Jest internals is a deep dive. Configuration is resolved / normalized before running test. It means that all project configs already have their By the way, running I was thinking, perhaps an example would be helpful? A snippet from the issue is puzzling: module.exports = {
projects: ["<rootDir>/packages/*"],
collectCoverageFrom: ["<rootDir>/src/*"],
}; Running this with {
"configs": [
{
"rootDir": "/Users/me/projects/x-jest-projects/packages/one",
},
{
"rootDir": "/Users/me/projects/x-jest-projects/packages/two",
}
],
"globalConfig": {
"collectCoverageFrom": ["src/*"],
"projects": [
"/Users/me/projects/x-jest-projects/packages/one",
"/Users/me/projects/x-jest-projects/packages/two"
],
"rootDir": "/Users/me/projects/x-jest-projects",
}
} Hm.. Looking at the issue I was expect to see I tried to rework an example from the docs. A comment is added, but all is obvious without a comment, or? module.exports = {
projects: [
{
displayName: "lint",
rootDir: "lint-project",
runner: 'jest-runner-eslint',
testMatch: ["<rootDir>/**/*.js"], // here `<rootDir>` points to `<cwd>/lint-project`
},
],
rootDir: "src",
}; One more attempt: module.exports = {
projects: [
"packages/*",
],
testMatch: ["<rootDir>/**/*.js"],
}; Resolved configs (did not expect to see default {
"configs": [
{
"rootDir": "/Users/me/projects/x-jest-projects/packages/one",
"testMatch": [
"**/__tests__/**/*.[jt]s?(x)",
"**/?(*.)+(spec|test).[tj]s?(x)"
],
},
{
"rootDir": "/Users/me/projects/x-jest-projects/packages/two",
"testMatch": [
"**/__tests__/**/*.[jt]s?(x)",
"**/?(*.)+(spec|test).[tj]s?(x)"
],
}
],
"globalConfig": {
"projects": [
"/Users/me/projects/x-jest-projects/packages/one",
"/Users/me/projects/x-jest-projects/packages/two"
],
"rootDir": "/Users/me/projects/x-jest-projects",
},
} Apologies for such long comment. I just try to understand what does it mean "Jest will copy the root-level configuration options to each individual child configuration". What did I miss? |
Thanks for the quick look at this, @mrazauskas! I believe it was Jest's source code where I found that when using The confusion was around the module.exports = {
projects: ["<rootDir>/packages/*"],
collectCoverageFrom: ["<rootDir>/src/*"],
}; Would not yield
The change proposed here clarifies the different I think a code coverage is a good example, as you want to limit the glob you gather coverage to something less permissive than I can suggest the following example: // jest.config.js
module.exports = {
// Here, "<rootDir>" resolves to the actual root of the project.
// In this case, it's CWD ("./").
// This path is relative to the root.
projects: ['<rootDir>/packages/*'],
// In this option, however, "<rootDir>" resolves to
// each individual child "/packages/*" path.
// For example:
// /packages/foo/src/**/*.js
// This path is NOT relative to the root.
collectCoverageFrom: ['<rootDir>/src/**/*.js']
} |
Thanks. I do follow (; Did you try running this with For me The puzzle for me is: where do you see or get the |
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
I get it from the actual coverage logic. If you specify the path relative to the root, where the root-level config resides, you won't get any coverage. Nothing will fail, the coverage will simply be 0 as no files would exist at that path: // ./jest.config.js
module.exports = {
projects: ['<rootDir>/packages/*'],
// Here I'm making a mistake thinking "rootDir" means the same
// it does in the "projects" option above. Since it doesn't,
// Jest will try to collect coverage from "packages/foo/packages/foo/src/**/*"
// which doesn't exist.
collectCoverageFrom: ['<rootDir>/packages/foo/src/**/*']
} It's not really an issue of what the config shows in the end but the overall developer's experience of handling two identical tokens (rootDir) in the same file that will eventually resolve to completely different things. In order words: developers shouldn't rely on |
Thanks again. Just to add After looking around, it seems like what you see is how Running with Each project has its own 💎 implicit 💎 (or explicit) How to fix this? Hm.. Looks like any changes in code would be breaking. It might be a better idea to add a note to documentation. Take a look at |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks! can you also update all versioned docs?
Unfortunately, that's not the case at all. My projects don't have their own |
Thanks for the review, @SimenB! I've rephrased the explanation a little to make it more concise. I've also updated all the versioned docs, although I'd appreciate your keen eye on whether this behavior applies to the previous versions of Jest. I've experienced it on Jest 27, I suspect Jest 28 has the same behavior. |
@@ -707,6 +707,8 @@ The projects feature can also be used to run multiple configurations or multiple | |||
|
|||
_Note: When using multi-project runner, it's recommended to add a `displayName` for each project. This will show the `displayName` of a project next to its tests._ | |||
|
|||
_Note: With the `projects` option enabled, Jest will copy the root-level configuration options to each individual child configuration during the test run, resolving its values in the child's context. This means that string tokens like `<rootDir>` will point to the *child's root directory* even if they are defined in the root-level configuration._ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've noticed Jest 27 versioned docs are not used special tags like :::note
so I kept plain italics for consistency. Jest 26 docs, however, use those custom tags.
Hey, @SimenB. Sorry for troubling you but could you please have another look at this change once you have a spare minute? Thanks! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks, and sorry for the radio silence!
Could you update this to latest main? (v28 has been combined to 28.x, there's a 29.0 now)
Hey, @SimenB. Thanks for reaching out! I've updated this branch against the latest main, the new hint is still present in every version of the docs (25-29). May I ask you for the final round of review on this? If the changes are good, feel free to merge them straight away. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks!
rootDir
changes when using projects
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. |
Not sure this should be represented in the CHANGELOG. Let me know if it should.
Summary
The
<rootDir>
token changes its behavior when used with theprojects
option. I'm describing this at length in this issue. This pull request adds a mention to theprojects
section of the Configuration documentation that explains how<rootDir>
will be evaluated.Test plan
A change to the documentation, no change plan necessary.