jest: The module factory of `jest.mock()` is not allowed to reference any out-of-scope variables
I’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 😃
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 4
- Comments: 37 (10 by maintainers)
Commits related to this issue
- Code fix based on new jest changes based on FB GitHub trace https://github.com/facebook/jest/issues/2567 you can't reference external data inside of mock function. You will get following error Th... — committed to vlad-bezden/React-Design-Patterns-and-Best-Practices by vlad-bezden 7 years ago
- Downgrade node on Travis to avoid a Jest bug. — committed to remix-run/react-router by timdorr 6 years ago
- ci(travis) Pin version of node Node 10 currently breaks builds cause of missing whitelist console in react native jest setup. https://github.com/facebook/jest/issues/2567#issuecomment-384487860 Pinni... — committed to react-native-elements/react-native-elements by iRoachie 6 years ago
- ci(travis) Pin version of node Node 10 currently breaks builds cause of missing whitelist console in react native jest setup. https://github.com/facebook/jest/issues/2567#issuecomment-384487860 Pinni... — committed to react-native-elements/react-native-elements by iRoachie 6 years ago
- ci(travis) Pin version of node (#1158) Node 10 currently breaks builds cause of missing whitelist console in react native jest setup. https://github.com/facebook/jest/issues/2567#issuecomment-3844878... — committed to react-native-elements/react-native-elements by iRoachie 6 years ago
- Add Cancel Button Props Object for iOS (#1159) * docs(website): Set default version shown as 0.19.0 (#1156) * ci(travis) Pin version of node (#1158) Node 10 currently breaks builds cause of mis... — committed to react-native-elements/react-native-elements by deleted user 6 years ago
- v1.0.0-beta5 (#1077) * Remove any occurence of `isRequired` #960 * fix(Title) Move disabled style (#1070) * fix(Title) Move disabled style * reorder styles * fix(types): ButtonGroup - Add... — committed to react-native-elements/react-native-elements by iRoachie 6 years ago
- Upgrade jest to current master to fix facebook/jest#2567 — committed to jerolimov/react-native-with-typescript by jerolimov 6 years ago
- make usage of jest.mock for react-dom conform to defined behavior This helps prepare for using Babel to transpile TypeScript but is good practice regardless. - In `code_intelligence.test.tsx`, the `... — committed to sourcegraph/sourcegraph by sqs 5 years ago
- make usage of jest.mock for react-dom conform to defined behavior (#3943) This helps prepare for using Babel to transpile TypeScript but is good practice regardless. - In `code_intelligence.test.t... — committed to sourcegraph/sourcegraph by sqs 5 years ago
- Switching from AsyncStorage to react-native-async-storage (based on #54) (#55) * Updated react native dependencies. * Updated babel dependencies. Removed the caret from react and react-native beac... — committed to jasonmerino/react-native-simple-store by oas 5 years ago
Using
jest.doMock
instead ofjest.mock
has 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.
yes.
I just stumbled upon this while googling and it seems like I’ve missed this crucial line in the error message along with everyone else:
If it is ensured that the mock is required lazily, variable names prefixed with
mock
are permitted.Just change the name of what you’re mocking to mockYourComponentName
in case anyone copy pastes this and sees it failing in CI (circle/gitlab) and not their local, make sure
React
is a lowercasereact
Upgrading babel-jest with
yarn add --dev babel-jest babel-core regenerator-runtime
fixed this error for me.To explain why: With
jest.resetModules()
you may reset all currently available modules, so when you call require, you’ll get a new version of each module. If you use React from the top level, you’ll end up having potentially two copies of React.Same issue when run with nodejs 10.0.0
The ‘jest.mock’ calls get moved from ‘it’ calls to the outer closure by a preprocessor and it does not work very well. ‘jest.doMock’ calls aren’t affected by a preprocessor.
I meet this problem when I run jest with
nodejs 10.0.0
, just downgraded node version is work.This one usage is ok and there is an escape hatch for it. Call your variable
mockFoo
.Is this really correct? As @nckblu already mentioned above, variables that start with ‘mock’ should be available as an exception. And ‘mockComponent’ should fall into that exception, right?
@khryshyn Jest will automatically hoist jest.mock calls to the top of the module. That’s why your
mockComponent
const is not defined yet when jest.mock runs.To go around this “issue/feature”, I do it in 2 steps as such:
Just await the promise.
No idea how that looks with typescript, but shouldn’t be too different
Seems like such issue still exist and now even workarounds don’t help in create react app application
` ReferenceError: mockComponent is not defined
`
Any idea why
doMock
works andmock
does not? Weird bit for me was also that if I put the variable with name “MockedComponent” I received an error, but when I put “mockedComponent” there was no error, but the reference was “undefined”.@cpojer I want to use
__dirname
variable, it is also not allowed, how can I get it?I don’t want to use a environment involved path, like
/Users/xx/project
That doesn’t have anything to do with node 10, it’s just that we don’t have
console
in the whitelist. PR welcome! We really should just use someglobals
module instead of a manual whitelist…How do you make this work with ES6 modules, which cannot be put inside the function scope?
@ghost23 I think the
mock
prefix exception is only valid when the module factory that returns the mock is a higher-order-function (HOF), and the mock-prefixed variable is referenced at the inner layer. That’s why this technique is generally used to mock classes because classes have constructors which are HOFs naturally.This is mostly covered in calling-jestmock-with-the-module-factory-parameter. Quoted as below for your quick reference.
With newer versions of node (14+), you could hit a different error message as shown in #10996.
The mockimplementation approach mentioned by @maxletourneur is a nice solution.
Last one fixed here: #6075
Only if you call
jest.resetModules()
between the two require calls.