The module factory of `jest.mock()` is not allowed to reference any out-of-scope variables

See original GitHub issue

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 😃

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:4
  • Comments:37 (5 by maintainers)

github_iconTop GitHub Comments

208reactions
faucctcommented, Jan 31, 2018

Using jest.doMock instead of jest.mock has helped me.

151reactions
cpojercommented, Mar 19, 2019

you need to do this:

jest.mock(`Picker`, () => {
  const React = require('react');
});

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found