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(jestUtils): use global expect if available #3583

Closed
wants to merge 1 commit into from

Conversation

SimenB
Copy link
Contributor

@SimenB SimenB commented Sep 18, 2022

Description

Doing require('expect') (or require('@jest/globals')) is not needed unless Jest is configured with injectGlobals: false. So just using the global means less work.

We probably don't even need the fallback to require('expect') as @jest/globals will exist in any setup where expect is not a global.

Closes #3559
Fixes #3553
Fixes #3215

Changes

Check for global expect before trying to load it through node_modules.

Test code and steps to reproduce

This now works correctly for Jest 28.

Checklist

  • Included code example that can be used to test this change
  • Updated TS types
  • Added TS types tests
  • Added unit / integration tests
  • Updated documentation
  • Ensured that CI passes

@backlands
Copy link

@piaskowyk I tested this PR against a project attempting to upgrade to Jest v29 and the tests are all able to run through as before upgrading from an older version of Jest. This change or the change from #3559 works in this same project, though I noticed that the internal checks from GitHub Actions are passing on the other PR but not this one, specifically due to TypeError: Cannot read property 'extend' of undefined so this PR may be affecting something unexpected (hah).

Since it is not my project I am impartial on which solution you use as both provide the fix needed to support a wider range of Jest versions. Thanks the work on this PR @SimenB and for moving issues #3553 and #3215 forward @piaskowyk.

@piaskowyk
Copy link
Member

piaskowyk commented Nov 3, 2022

Thank you for the help! I appreciate so much 😍
I merged this PR with #3559, and #3559 includes this PR also

@piaskowyk piaskowyk closed this Nov 3, 2022
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.

Won't build Jest issue TypeError: expect.extend is not a function
3 participants