babel-plugin-jest-hoist: The module factory of `jest.mock()` is not allowed to reference any out-of-scope variables.
See original GitHub issue🐛 Bug Report
Spying on mocked methods as described in docs, throws babel-plugin-jest-hoist: The module factory of jest.mock() is not allowed to reference any out-of-scope variables.:
const mockPlaySoundFile = jest.fn(); jest.mock('./sound-player', () => { return jest.fn().mockImplementation(() => { return {playSoundFile: mockPlaySoundFile}; // Now we can track calls to playSoundFile }); });
https://jestjs.io/docs/en/es6-class-mocks#spying-on-methods-of-our-class
To Reproduce
Steps to reproduce the behavior:
Follow the docs here https://jestjs.io/docs/en/es6-class-mocks#spying-on-methods-of-our-class
Expected behavior
What is documented should work: https://jestjs.io/docs/en/es6-class-mocks#spying-on-methods-of-our-class
Link to repl or repo (highly encouraged)
Similiar issue https://github.com/facebook/jest/issues/2567
envinfo
npx: installed 1 in 1.27s
System:
OS: macOS 10.15.3
CPU: (12) x64 Intel(R) Core(TM) i9-8950HK CPU @ 2.90GHz
Binaries:
Node: 10.19.0 - /usr/local/bin/node
Yarn: 1.12.3 - /usr/local/bin/yarn
npm: 6.13.4 - /usr/local/bin/npm
npmPackages:
jest: ^24.9.0 => 24.9.0
Issue Analytics
- State:
- Created 3 years ago
- Comments:5
Top Results From Across the Web
The module factory of `jest.mock()` is not allowed to ...
babel-plugin-jest-hoist : The module factory of `jest.mock()` is not allowed to reference any out-of-scope variables. Invalid variable access: ...
Read more >Service mocked with Jest causes "The module factory of ...
mock() is not allowed to reference any out-of-scope variables. I'm using babel with ES6 syntax, jest and enzyme. I have a simple component ......
Read more >Two ways to fix the Jest test error “the module factory of `jest ...
The module factory of “jest.mock()” is not allowed to reference any out-of-scope variables. Fix 1. Prepend your jest.fn() variable ...
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. Prepend your jest.fn ......
Read more >Service mocked with Jest causes “The module factory of ...
js: babel-plugin-jest-hoist: The module factory of jest.mock() is not allowed to reference any out-of-scope variables. Invalid variable access: VocabularyEntry.
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 Free
Top 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

@SimenB Thanks for your answer.
Please check the docs, I think especially this page and the coding example is misleading, in addition it does not mention the
mockprefix 🤔 https://jestjs.io/docs/en/es6-class-mocks#spying-on-methods-of-our-classAlso the Readme of
babel-plugin-jest-hoistdoesn’t mention themockprefix 🤔 https://github.com/facebook/jest/tree/master/packages/babel-plugin-jest-hoistBut the source code has a comment about it:
… and an implementation 🤔 https://github.com/facebook/jest/blob/master/packages/babel-plugin-jest-hoist/src/index.ts#L122
Alright, it seems all mocked functions need to be prefixed with
mock*, but if I do that, the value of the mock isundefined🤔out-of-scopeError:Prefixing with
mockdoes not throw, but value of mock isundefined: