The module factory of `jest.mock()` is not allowed to reference any out-of-scope variables
See original GitHub issueI’m using the snippet from #1960 to mock Picker in RN
import React, {Component} from 'react';
jest.mock(`Picker`, () => {
// ...etc
});
Works fine in Jest 17, throws following error in Jest 18:
/Users/simonsmith/Sites/new-look/newlookapp/test/unit/setup.js: babel-plugin-jest-hoist: The module factory of `jest.mock()` is not allowed to reference any out-of-scope variables.
Invalid variable access: React
Whitelisted objects: Array, ArrayBuffer, ..... etc
I’m using React 15.4.2 and RN 0.40
I tried babel-jest@test and they run as expected but all my snapshots fail, looks like more props are coming through which is probably unrelated to this.
Anything I can do to fix this now or should I wait for the next release for babel-jest?
Thanks 😃
Issue Analytics
- State:
- Created 7 years ago
- Reactions:4
- Comments:37 (5 by maintainers)
Top Results From Across the Web
Service mocked with Jest causes "The module factory of jest ...
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 >Jest.mock is not allowed to reference variables - JS-HowTo
Service mocked with Jest causes "The module factory of jest.mock() is not allowed to reference any out-of-scope variables"
Read more >ES6 Class Mocks - Jest
Jest can be used to mock ES6 classes that are imported into files you want to test. ES6 classes are constructor functions with...
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
Using
jest.doMockinstead ofjest.mockhas helped me.you need to do this:
This used to be a bug that we fixed. In a mock you can only require things locally and you aren’t allowed to access external variables.