[Bug]: referencing `jest` inside `jest.mock` block throws, if `jest` is imported from `@jest/globals`
See original GitHub issueVersion
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:
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:
- Created 2 years ago
- Reactions:1
- Comments:10 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
When you are checking if a variable is safe to use, you could get
binding = path.scope.getBinding(varName)
.binding.path
is a variable declaration whose RHS is a call expression callingrequire
with"@jest/globals"
.An alternative is to remove the
@jest/globals
import before that the module transform runs, and the add it back inpost()
.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.