Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
3 tasks done
jmkitavi opened this issue May 3, 2018 · 24 comments · Fixed by jestjs/jest#6505
Closed
3 tasks done
Labels
JavaScript Resolution: Locked This issue was locked by the bot.

Comments

@jmkitavi
Copy link

jmkitavi commented May 3, 2018

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)
@jmkitavi
Copy link
Author

jmkitavi commented May 4, 2018

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.

@tomaskazatel
Copy link

tomaskazatel commented May 4, 2018

👍 /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.. 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)

@borisyankov
Copy link

borisyankov commented May 7, 2018

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.

@DaveVanVliet
Copy link

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

@xzilja
Copy link
Contributor

xzilja commented May 10, 2018

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.

@patlub
Copy link

patlub commented May 11, 2018

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

@codeithuman
Copy link

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

@gengjiawen
Copy link
Contributor

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

@gengjiawen
Copy link
Contributor

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

    "jest": "23.0.0-charlie.1"

@gengjiawen
Copy link
Contributor

I have reported to jest in jestjs/jest#6196. If you want to fix this asap, please upvote that issue.

@carloscuesta
Copy link
Contributor

carloscuesta commented May 16, 2018

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

@gengjiawen
Copy link
Contributor

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

@gengjiawen
Copy link
Contributor

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

@gengjiawen
Copy link
Contributor

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

@grabbou
Copy link
Contributor

grabbou commented May 20, 2018

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

@gengjiawen
Copy link
Contributor

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

@hramos hramos closed this as completed May 24, 2018
@douglowder
Copy link
Contributor

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

@douglowder douglowder reopened this Jun 15, 2018
@hramos
Copy link
Contributor

hramos commented Jun 15, 2018

^ @pvdz

@gengjiawen
Copy link
Contributor

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

@mjesun
Copy link
Contributor

mjesun commented Jun 18, 2018

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

@SimenB
Copy link
Contributor

SimenB commented Jun 18, 2018

Yeah, fixed in 23.0.0: jestjs/jest#6075 (It's in the changelog 😉)

@timwangdev
Copy link
Contributor

    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

@thymikee
Copy link
Contributor

@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.

@Rolando-Barbella
Copy link

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

"jest-expo": "28.0.0"

node 10.6.0

@facebook facebook locked as resolved and limited conversation to collaborators Jul 16, 2018
@react-native-bot react-native-bot added the Resolution: Locked This issue was locked by the bot. label Jun 20, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
JavaScript Resolution: Locked This issue was locked by the bot.
Projects
None yet
Development

Successfully merging a pull request may close this issue.