question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Bug]: referencing `jest` inside `jest.mock` block throws, if `jest` is imported from `@jest/globals`

See original GitHub issue

Version

28.0.0-alpha.3

Steps to reproduce

Here is a repo with minimal reproduction: https://github.com/mrazauskas/x-jest-globals

I simply added explicit import to the example from docs (and used TS to trigger transpiration) – https://jestjs.io/docs/next/mock-functions#mocking-partials

Running yarn jest will throw an ReferenceError:

Screenshot 2022-02-18 at 09 38 22

Expected behavior

It should not throw ReferenceError.

Actual behavior

It throws ReferenceError, probably because of transpilation. I also tried transpiling using ts-jest and the error was gone. It seems like only users of babel-jest are affected.

Additional context

No response

Environment

System:
    OS: macOS 10.15.7
    CPU: (8) x64 Intel(R) Core(TM) i7-3615QM CPU @ 2.30GHz
  Binaries:
    Node: 17.3.1 - ~/.nvm/versions/node/v17.3.1/bin/node
    Yarn: 1.22.15 - ~/.nvm/versions/node/v17.3.1/bin/yarn
    npm: 8.3.0 - ~/.nvm/versions/node/v17.3.1/bin/npm
  npmPackages:
    jest: ^28.0.0-alpha.3 => 28.0.0-alpha.3

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
nicolo-ribaudocommented, Feb 20, 2022

When you are checking if a variable is safe to use, you could get binding = path.scope.getBinding(varName).

  • If it’s undefined, it’s a global (so probably it’s safe?)
  • Otherwise, check if binding.path is a variable declaration whose RHS is a call expression calling require with "@jest/globals".

An alternative is to remove the @jest/globals import before that the module transform runs, and the add it back in post().

0reactions
github-actions[bot]commented, Oct 4, 2022

This issue 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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Globals - Jest
In your test files, Jest puts each of these methods and objects into the global environment. You don't have to require or import...
Read more >
Service mocked with Jest causes "The module factory of jest ...
You need to store your mocked component in a variable with a name prefixed by "mock". This solution is based on the Note...
Read more >
Jest Expect - w3resource
Any calls to the mock function that throw an error will not be counted toward the number of times the function returned. For...
Read more >
1 - Salesforce Stack Exchange
If the stub is not provided for a standard module in the sfdx-lwc-jest ... in Jest Test Patterns and Mock Dependencies under Module...
Read more >
Fix jest mock 'Cannot access before initialization' error
If you start your variables with the word 'mock' for jest, you might have fallen into this misconception.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found