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

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)

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 26
  • Comments: 24 (17 by maintainers)

Most upvoted 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.

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.

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

Having similar issue here after upgrading to node version 10.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.

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

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

@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 ' +

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

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

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

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

    "jest": "23.0.0-charlie.1"