[Bug]: Jest 29 not respecting `mock` prefix for variables when hoisting
See original GitHub issueVersion
29.1.1
Steps to reproduce
- clone https://github.com/jcollum-nutrien/jest-mock-issue
- yarn
- yarn test
Dupe of #10996, which is locked (but also a year old)
Expected behavior
Variables prefixed with mock
should be hoisted, as the docs state.
Actual behavior
Cannot access 'mockInfo' before initialization
yarn run v1.22.15
$ jest
FAIL ./logger.spec.ts
● Test suite failed to run
ReferenceError: Cannot access 'mockInfo' before initialization
5 | jest.mock('./logger', ()=>{
6 | return {
> 7 | info: mockInfo
| ^
8 | }
9 | })
10 | describe('repro', function () {
at mockInfo (logger.spec.ts:7:11)
at Object.<anonymous> (logger.spec.ts:4:1)
Additional context
No response
Environment
npx envinfo --preset jest
npx: installed 1 in 0.74s
System:
OS: macOS 12.5.1
CPU: (10) x64 Apple M1 Pro
Binaries:
Node: 14.19.2 - ~/.nvm/versions/node/v14.19.2/bin/node
Yarn: 1.22.19 - ~/work/jest-mock-issue/node_modules/.bin/yarn
npm: 6.14.17 - ~/.nvm/versions/node/v14.19.2/bin/npm
npmPackages:
jest: 29.1.1 => 29.1.1
Issue Analytics
- State:
- Created a year ago
- Comments:7 (2 by maintainers)
Top Results From Across the Web
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 mock cannot access before initialization - You.com
Jest mock aws-sdk ReferenceError: Cannot access before initialization ... Jest 29 not respecting `mock` prefix for variables when hoisting#13333.
Read more >Two ways to fix the Jest test error “the module factory of `jest ...
Jest will complain that: The module factory of “jest.mock()” is not allowed to reference any out-of-scope variables. Fix 1.
Read more >@rushstack/eslint-plugin - npm
Jest module mocking APIs such as "jest.mock()" must be called before the associated module is imported, otherwise they will have no effect.
Read more >ECMAScript Modules - Jest
Since ESM evaluates static import statements before looking at the code, the hoisting of jest.mock calls that happens in CJS won't work for...
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
With two changes in your reproduction seems to be working:
babel-jest
as a transformer. It works as documented. If a third party transformer does not work for you, Jest can’t help here.The ‘ReferenceError’ issue should be gone. (Note that I did not try to make your test pass, that is not in the scope of the issue.)
For anyone who finds this later: I switched it over to babel-jest. Still ran into issues, had to change the mock to:
Repo has been updated. Test is passing.