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

feat: warn when there are multiple configs #11922

Merged
merged 41 commits into from
Oct 6, 2021

Conversation

jankaifer
Copy link
Contributor

@jankaifer jankaifer commented Oct 2, 2021

  • - CHANGELOG.md is not updated yet, since this feature is not approved/finished and contains breaking changes.

Summary

resolveConfigPath throws when there are both package.json(with "jest") and jest.config.ext present in a single directory.

Fix #10124

There are 2 stale PRs that I took inspiration from: #10798 and #10213

It is a breaking change (error, not a warning) as proposed by @SimenB here: #10798 (review)

Closes #10798
Closes #10213

Test plan

I wrote/updated unit tests that check the behavior.

Questions

@jankaifer jankaifer changed the title Throw when there are multiple configs. package.json and jest.config.js Throw when there are multiple configs - package.json and jest.config.js Oct 2, 2021
@mrazauskas
Copy link
Contributor

Just thinking out loud. What if Jest would simply console.info a line telling which config it is using? Only if the config was auto detected or always?

This wouldn’t be a breaking change in this case.

(Would be good to check how this shall work with projects.)

@jankaifer
Copy link
Contributor Author

jankaifer commented Oct 2, 2021

I just followed this comment by maintainers: #10798 (review) . I don't have strong feelings about this.

Copy link
Member

@SimenB SimenB left a comment

Choose a reason for hiding this comment

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

lgtm.

One thing is that if people define a --config flag this should not throw. We should only error if Jest needs to traverse and find configuration on its own and there is ambiguity on which to use.

I'd also like to see some integration tests for this (i.e. a dir with both a config file and config in package.json, and run it once to see the error, then with --config jets.config.js which should not throw)

packages/jest-config/src/resolveConfigPath.ts Outdated Show resolved Hide resolved
packages/jest-config/src/resolveConfigPath.ts Outdated Show resolved Hide resolved
@SimenB
Copy link
Member

SimenB commented Oct 4, 2021

As for breaking or not - I guess I could be down for a config warning similar to deprecation warnings like using --browser

image

Not just a single line, but a more in your face warning that you really wanna fix

Co-authored-by: Simen Bekkhus <sbekkhus91@gmail.com>
@jankaifer
Copy link
Contributor Author

One thing is that if people define a --config flag this should not throw. We should only error if Jest needs to traverse and find configuration on its own and there is ambiguity on which to use.

I'd also like to see some integration tests for this (i.e. a dir with both a config file and config in package.json, and run it once to see the error, then with --config jets.config.js which should not throw)

Sorry, missed that. On it.

As for breaking or not - I guess I could be down for a config warning similar to deprecation warnings like using --browser

I will leave that decision to you. I like error a bit more, but I don't have the knowledge about jest to make a good decision here.

@jankaifer
Copy link
Contributor Author

I'd also like to see some integration tests for this (i.e. a dir with both a config file and config in package.json, and run it once to see the error, then with --config jets.config.js which should not throw)

Added e2e test for that (surprisingly there wasn't one - at least I didn't find any).

@jankaifer jankaifer requested a review from SimenB October 4, 2021 19:09
Copy link
Member

@SimenB SimenB left a comment

Choose a reason for hiding this comment

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

thanks, this looks great! I just have a couple of nits for the tests, beyond that LGTM 👍

e2e/__tests__/dependencyClash.test.ts Show resolved Hide resolved
e2e/esm-config/cjs/package.json Show resolved Hide resolved
e2e/__tests__/configOverride.test.ts Show resolved Hide resolved
packages/jest-config/src/resolveConfigPath.ts Outdated Show resolved Hide resolved
Copy link
Member

@SimenB SimenB left a comment

Choose a reason for hiding this comment

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

Ok, I think we're almost done now 😀

(remember the changelog entry)

e2e/__tests__/multipleConfigs.ts Outdated Show resolved Hide resolved
e2e/__tests__/multipleConfigs.ts Outdated Show resolved Hide resolved
e2e/esm-config/mjs/package.json Outdated Show resolved Hide resolved
}

if (configFiles.length > 1) {
throw new Error(makeMultipleConfigsError(configFiles));
Copy link
Member

Choose a reason for hiding this comment

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

if we throw it's a breaking change, and we need to wait for Jest 28. Thoughts on making it a warning for now so we can land it, then throw later?

Copy link
Contributor Author

@jankaifer jankaifer Oct 5, 2021

Choose a reason for hiding this comment

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

Sounds reasonable.

Should I hide it behind a flag? Or just delete it and rewrite it for warnings?
This question is related to tests, the code itself is just one line, but tests are completely different.

Copy link
Member

Choose a reason for hiding this comment

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

I'd do console.warn instead of throwing, make the unit tests mock out the console, while the e2e test is essentially the same except for the exit code

Copy link
Contributor Author

Choose a reason for hiding this comment

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

resolveConfigPath has to be a pure function (no console.warn there), because it is run multiple times.

There is hasDeprecationWarnings: boolean flag somewhere. I will look into it later.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hasDeprecationWarnings has no text associated with it.

The best solution I found was to propagate shouldLogWarnings. But I'm not sure it is good enough.

Copy link
Contributor Author

@jankaifer jankaifer Oct 5, 2021

Choose a reason for hiding this comment

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

The "second" usage is here: https://github.com/facebook/jest/blob/a22ed6592f772df6b19314d9f06a7285c3b0968b/packages/jest-config/src/index.ts#L287

It's reading jest config to determine if there are any additional projects defined.

And then it is called again for each project.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@SimenB ^ this one is still not solved.

Copy link
Member

Choose a reason for hiding this comment

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

gave that a whack with 6a52634 (#11922)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, so you went with shouldLogWarnings. 👍

Copy link
Member

Choose a reason for hiding this comment

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

yeah, it was either that or store some list of the exact warning we had logged (a Set to dedupe) then only log after all configs were resolved. This seemed cleaner

packages/jest-config/src/resolveConfigPath.ts Outdated Show resolved Hide resolved
e2e/__tests__/multipleConfigs.ts Outdated Show resolved Hide resolved
jankaifer and others added 2 commits October 5, 2021 11:09
Co-authored-by: Simen Bekkhus <sbekkhus91@gmail.com>
Copy link
Member

@SimenB SimenB left a comment

Choose a reason for hiding this comment

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

thanks! is a fantastic first contribution 🎉

Copy link
Member

@SimenB SimenB left a comment

Choose a reason for hiding this comment

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

got a bit too eager, didn't notice the failing tests 😅

e2e/__tests__/multipleConfigs.ts Outdated Show resolved Hide resolved
packages/jest-config/src/resolveConfigPath.ts Outdated Show resolved Hide resolved
@jankaifer
Copy link
Contributor Author

Sorry, was busy with school. Thanks for finishing it @SimenB.

And thanks for guiding me through this PR.

Copy link
Member

@SimenB SimenB left a comment

Choose a reason for hiding this comment

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

Now it seems CI is happy. Thanks again!

@codecov-commenter
Copy link

Codecov Report

Merging #11922 (b57cbf5) into main (24f9bc8) will increase coverage by 0.03%.
The diff coverage is 93.54%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main   #11922      +/-   ##
==========================================
+ Coverage   68.75%   68.78%   +0.03%     
==========================================
  Files         322      322              
  Lines       16587    16608      +21     
  Branches     4787     4793       +6     
==========================================
+ Hits        11404    11424      +20     
- Misses       5151     5152       +1     
  Partials       32       32              
Impacted Files Coverage Δ
packages/jest-config/src/index.ts 20.00% <33.33%> (+1.01%) ⬆️
packages/jest-config/src/resolveConfigPath.ts 97.95% <100.00%> (+1.40%) ⬆️
packages/expect/src/utils.ts 95.85% <0.00%> (-0.52%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 24f9bc8...b57cbf5. Read the comment docs.

@SimenB SimenB changed the title Throw when there are multiple configs - package.json and jest.config.js feat: warn when there are multiple configs Oct 6, 2021
@SimenB SimenB merged commit cdc64c6 into jestjs:main Oct 6, 2021
@jankaifer jankaifer deleted the jankaifer/warn-multiple-configs-found branch October 6, 2021 11:11
@github-actions
Copy link

github-actions bot commented Nov 6, 2021

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 Nov 6, 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.

Warn when multiple configs are found
5 participants