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

fix: use correct highlight offset even when rootDir is not root package directory #34

Merged
merged 5 commits into from
Mar 17, 2021

Conversation

wilsonparson
Copy link
Contributor

@wilsonparson wilsonparson commented Mar 26, 2020

When a Jest config's rootDir is not the same directory as the root package directory (e.g., a monorepo with local Jest configs for each library or app), the highlight offset is calculated incorrectly because it assumes rootDir is the root directory of the package. This only occurs when the filePath is not truncated (i.e., does not have a leading '...'):

incorrect-offset

This PR updates the offset calculation to rely on rawPath and filePath instead of rootDir to provide a consistent highlight offset regardless of rootDir's location:

correct-offset

The PR includes three commits:

  1. 3 passing tests and 1 failing test to document the bug
  2. The fix (4 passing tests)
  3. A refactor of the highlight function, removing the rootDir parameter since it is no longer being used

When the Jest rootDir is not the same as the package's root directory
(i.e., a monorepo scenario with multiple local Jest configs) the
highlight function incorrectly calculates the offset for highlighting file
name matches, resulting in a highlight in the wrong place.
This only occurs when filePath is not truncated.
This change fixes the failing test by moving the offset calculation out of the
conditional and calculating it the same way in all scenarios using
rawPath and filePath.
Since highlight no longer needs rootDir to calculate the
highlight offset, this change removes rootDir from highlight's
parameters. It also updates tests to use inline snapshots now that
there are fewer scenarios to account for.
@wilsonparson wilsonparson changed the title Fix incorrect highlight offset when rootDir is not root package directory fix: use correct highlight offset even when rootDir is not root package directory Mar 26, 2020
@thymikee thymikee requested a review from rogeliog March 26, 2020 07:42
@SimenB
Copy link
Member

SimenB commented Mar 26, 2020

Do we do any rootDir replacement? If not this makes sense to me

Copy link
Member

@jeysal jeysal left a comment

Choose a reason for hiding this comment

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

Very sorry this was lying around so long, just came across it deep in my inbox.
Tried this on a random workspace project I have, seems to work. Code also LGTM. Another review, perhaps from @rogeliog, would be nice though to maybe understand why this was there in the first place.

@ghost
Copy link

ghost commented Jul 6, 2020

Just commenting to see if we can keep things moving on this!

@SimenB
Copy link
Member

SimenB commented Nov 7, 2020

@jeysal @thymikee should we land this?

@jeysal
Copy link
Member

jeysal commented Nov 8, 2020

👍

Copy link
Member

@thymikee thymikee left a comment

Choose a reason for hiding this comment

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

LGTM. If it breaks a use case we're not aware of, we can then add another test and make it work for both cases

@joshdegouveia
Copy link

@wilsonparson I ran into this same issue while attempting to add jest-watch-typeahead to a monorepo. Would you mind if I forked your repo and fixed conflicts so that this PR could be merged? Or give me write permissions to your repo?

@wilsonparson
Copy link
Contributor Author

@joshdegouveia thanks for the nudge! I just resolved the conflicts. Hopefully this fix resolves the issue you're having as well!

The changes in this PR allow us to remove the rootDir param from the
highlight function. I forgot to remove it from the function declaration
and it was causing type checking to fail.
@joshdegouveia
Copy link

@wilsonparson thank you! I appreciate it 😄 Yeah it looks like it's the same issue - thanks for putting this fix together!

@thymikee If this PR looks good - could you merge it?

@joshdegouveia
Copy link

@jeysal @SimenB @thymikee bumping this 🙂

@jeysal jeysal merged commit 9dfa861 into jest-community:master Mar 17, 2021
@jeysal
Copy link
Member

jeysal commented Apr 6, 2021

This may have caused #46

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants