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

Created on 4 May 2018  ·  24Comments  ·  Source: facebook/react-native

Environment


My Environment is as below.

Scanning folders for symlinks in /Users/sirjmkitavi/Andela/playground/react-native/ball-animation/node_modules (25ms)

Environment:
  OS: macOS High Sierra 10.13.2
  Node: 10.0.0
  Yarn: 1.6.0
  npm: 5.6.0
  Watchman: 4.9.0
  Xcode: Xcode 9.2 Build version 9C40b
  Android Studio: Not Found

Packages: (wanted => installed)
  react: ^16.3.2 => 16.3.2
  react-native: ^0.55.3 => 0.55.3

Steps to Reproduce

  • Initialise react-native application. Preferably using react-native-clie

i.e react-native init SampleProj

  • Ensure you have atleast one test.
  • Run yarn test

Expected Behavior


The expected behaviour is for the tests to run successfully. To either fail or pass.

Actual Behavior


All test suites fail throwing the error below

 /Users/{user}/pulse/pulse-mobile-app/node_modules/react-native/jest/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: console
...

The error message continues to say

Whitelisted objects: Array, ArrayBuffer, Boolean, DataView, Date, Error, EvalError, Float32Array,
Float64Array, Function, Generator, GeneratorFunction, Infinity, Int16Array, Int32Array, Int8Array,
InternalError, Intl, JSON, Map, Math, NaN, Number, Object, Promise, Proxy, RangeError,
ReferenceError, Reflect, RegExp, Set, String, Symbol, SyntaxError, TypeError, URIError, Uint16Array,
Uint32Array, Uint8Array, Uint8ClampedArray, WeakMap, WeakSet, arguments, expect, jest, require,
undefined, DTRACE_NET_SERVER_CONNECTION, DTRACE_NET_STREAM_END,
DTRACE_HTTP_SERVER_REQUEST, DTRACE_HTTP_SERVER_RESPONSE,
DTRACE_HTTP_CLIENT_REQUEST, DTRACE_HTTP_CLIENT_RESPONSE, global, process, Buffer,
clearImmediate, clearInterval, clearTimeout, setImmediate, setInterval, setTimeout.
    Note: This is a precaution to guard against uninitialized mock variables.
If it is ensured that the mock is required lazily, variable names prefixed with `mock` are permitted.

      at invariant (node_modules/babel-plugin-jest-hoist/build/index.js:14:11)
      at newFn (node_modules/babel-traverse/lib/visitors.js:276:21)
      at NodePath._call (node_modules/babel-traverse/lib/path/context.js:76:18)
      at NodePath.call (node_modules/babel-traverse/lib/path/context.js:48:17)
      at NodePath.visit (node_modules/babel-traverse/lib/path/context.js:105:12)
      at TraversalContext.visitQueue (node_modules/babel-traverse/lib/context.js:150:16)
JavaScript Locked

Most helpful comment

Downgrading my version of yarn didn't change anything.
However, downgrading my node version from 10 to 8 solved the error.

Not sure why, but you guys really need to take a look at this.

All 24 comments

Downgrading my version of yarn didn't change anything.
However, downgrading my node version from 10 to 8 solved the error.

Not sure why, but you guys really need to take a look at this.

👍 /home/circleci/repo/node_modules/react-native/jest/setup.js: babel-plugin-jest-hoist: The module factory ofjest.mock()is not allowed to reference any out-of-scope variables.. I cant build RN app on CircleCI.

Full Error log

/home/circleci/repo/node_modules/react-native/jest/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: console
    Whitelisted objects: Array, ArrayBuffer, Boolean, DataView, Date, Error, EvalError, Float32Array, Float64Array, Function, Generator, GeneratorFunction, Infinity, Int16Array, Int32Array, Int8Array, InternalError, Intl, JSON, Map, Math, NaN, Number, Object, Promise, Proxy, RangeError, ReferenceError, Reflect, RegExp, Set, String, Symbol, SyntaxError, TypeError, URIError, Uint16Array, Uint32Array, Uint8Array, Uint8ClampedArray, WeakMap, WeakSet, arguments, expect, jest, require, undefined, global, process, Buffer, clearImmediate, clearInterval, clearTimeout, setImmediate, setInterval, setTimeout.
    Note: This is a precaution to guard against uninitialized mock variables. If it is ensured that the mock is required lazily, variable names prefixed with `mock` are permitted.

      at invariant (node_modules/babel-plugin-jest-hoist/build/index.js:14:11)
      at newFn (node_modules/babel-traverse/lib/visitors.js:276:21)
      at NodePath._call (node_modules/babel-traverse/lib/path/context.js:76:18)
      at NodePath.call (node_modules/babel-traverse/lib/path/context.js:48:17)
      at NodePath.visit (node_modules/babel-traverse/lib/path/context.js:105:12)
      at TraversalContext.visitQueue (node_modules/babel-traverse/lib/context.js:150:16)

That is related to Node.js version for sure. It started appearing right after upgrading from 9.x to 10.0 and fixed itself when downgraded to 9.x.

@borisyankov Can confirm, all my tests fail on 10.0 but pass on 9.x

Same case, upgrade from node 9.x to 10.x resulted in error below

```
FAIL src/services/Helpers/test.js
● Test suite failed to run

/Users/ilja/Documents/Repositories/skimitar/node_modules/react-native/jest/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: console
Whitelisted objects: Array, ArrayBuffer, Boolean, DataView, Date, Error, EvalError, Float32Array, Float64Array, Function, Generator, GeneratorFunction, Infinity, Int16Array, Int32Array, Int8Array, InternalError, Intl, JSON, Map, Math, NaN, Number, Object, Promise, Proxy, RangeError, ReferenceError, Reflect, RegExp, Set, String, Symbol, SyntaxError, TypeError, URIError, Uint16Array, Uint32Array, Uint8Array, Uint8ClampedArray, WeakMap, WeakSet, arguments, expect, jest, require, undefined, DTRACE_NET_SERVER_CONNECTION, DTRACE_NET_STREAM_END, DTRACE_HTTP_SERVER_REQUEST, DTRACE_HTTP_SERVER_RESPONSE, DTRACE_HTTP_CLIENT_REQUEST, DTRACE_HTTP_CLIENT_RESPONSE, global, process, Buffer, clearImmediate, clearInterval, clearTimeout, setImmediate, setInterval, setTimeout.
Note: This is a precaution to guard against uninitialized mock variables. If it is ensured that the mock is required lazily, variable names prefixed with `mock` are permitted.

```

Having similar issue here after upgrading to node version 10.X

Same issue with Node v10.1.0. Downgraded to Node v8.11.1 via homebrew and tests are passing again.

Maybe it's a problem with jest, I tried to report it to jest when I have time.

For anyone want to use with node 10, update jest in package.json

    "jest": "23.0.0-charlie.1"

I have reported to jest in https://github.com/facebook/jest/issues/6196. If you want to fix this asap, please upvote that issue.

Same problem while running Jest tests on Node v10.1.0 downgrading to 8.x.x fixed the issue

@hramos Jest has released a new version, I think we can close this.

@grabbou Do you think a new release is needed or put this in change log ?

Basically you need the latest jest ( 22.4.4 released on today ) to support node 10 for react native.

We do not support Node 10.x yet, but this will get resolved at the time of support (which is to happen soon anyway)

From I test, jest is the only problem I run into before they release 22.4.4.

@hramos I'm using node 8.9 with latest react-native (0.56.0-rc.1) and still seeing this error.

The simplest workaround is to patch babel-plugin-jest-hoist as shown below. But if the babel folks don't like this, then it looks like the correct fix is to use a lowercase 'm' in jest/MockNativeMethods.js and make corresponding changes in other files.

--- node_modules/babel-plugin-jest-hoist/build/index.js.orig    2018-06-14 17:36:51.000000000 -0700
+++ node_modules/babel-plugin-jest-hoist/build/index.js 2018-06-14 17:37:09.000000000 -0700
@@ -112,7 +112,7 @@
       if (!found) {
         invariant(
           (scope.hasGlobal(name) && WHITELISTED_IDENTIFIERS[name]) ||
-            /^mock/.test(name) ||
+            /^[Mm]ock/.test(name) ||
             // Allow istanbul's coverage variable to pass.
             /^(?:__)?cov/.test(name),
           'The module factory of `jest.mock()` is not allowed to ' +

^ @pvdz

@dlowder-salesforce Can you try latest nodejs 8 and jest ?

cc / @simenb, @thymikee, do you recall if we had a fix for this? I think I've recently seen something about whitelisting console.

Yeah, fixed in 23.0.0: https://github.com/facebook/jest/pull/6075 (It's in the changelog 😉)

    babel-plugin-jest-hoist: The module factory of `jest.mock()` is not allowed to reference any out-of-scope variables.
    Invalid variable access: MockNativeMethods

My error is about MockNativeMethods

@mjesun @SimenB console was fixed, but the mock* variables being whitelisted too could be updated on Jest side to also honor uppercase Mock*. I don't see a reason why not to allow it.

I'm using react-native with expo and just did:

"jest-expo": "28.0.0"

node 10.6.0

Was this page helpful?
0 / 5 - 0 ratings