question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

🐛 Bug Report

Spying on mocked methods as described in docs, throws babel-plugin-jest-hoist: The module factory of jest.mock() is not allowed to reference any out-of-scope variables.:

const mockPlaySoundFile = jest.fn();
jest.mock('./sound-player', () => {
 return jest.fn().mockImplementation(() => {
   return {playSoundFile: mockPlaySoundFile};
   // Now we can track calls to playSoundFile
 });
});

https://jestjs.io/docs/en/es6-class-mocks#spying-on-methods-of-our-class

To Reproduce

Steps to reproduce the behavior:

Follow the docs here https://jestjs.io/docs/en/es6-class-mocks#spying-on-methods-of-our-class

Expected behavior

What is documented should work: https://jestjs.io/docs/en/es6-class-mocks#spying-on-methods-of-our-class

Link to repl or repo (highly encouraged)

Similiar issue https://github.com/facebook/jest/issues/2567

envinfo

npx: installed 1 in 1.27s

  System:
    OS: macOS 10.15.3
    CPU: (12) x64 Intel(R) Core(TM) i9-8950HK CPU @ 2.90GHz
  Binaries:
    Node: 10.19.0 - /usr/local/bin/node
    Yarn: 1.12.3 - /usr/local/bin/yarn
    npm: 6.13.4 - /usr/local/bin/npm
  npmPackages:
    jest: ^24.9.0 => 24.9.0

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5

github_iconTop GitHub Comments

8reactions
AndyOGocommented, Mar 31, 2020

@SimenB Thanks for your answer.

Please check the docs, I think especially this page and the coding example is misleading, in addition it does not mention the mock prefix 🤔 https://jestjs.io/docs/en/es6-class-mocks#spying-on-methods-of-our-class

Also the Readme of babel-plugin-jest-hoist doesn’t mention the mock prefix 🤔 https://github.com/facebook/jest/tree/master/packages/babel-plugin-jest-hoist

But the source code has a comment about it:

// We also allow variables prefixed with mock as an escape-hatch. https://github.com/facebook/jest/blob/master/packages/babel-plugin-jest-hoist/src/index.ts#L14

… and an implementation 🤔 https://github.com/facebook/jest/blob/master/packages/babel-plugin-jest-hoist/src/index.ts#L122

6reactions
AndyOGocommented, Mar 31, 2020

Alright, it seems all mocked functions need to be prefixed with mock*, but if I do that, the value of the mock is undefined 🤔

out-of-scope Error:

const foo = jest.fn();
jest.mock('./foo', () => ({
  // throws: babel-plugin-jest-hoist: The module factory of jest.mock() is not allowed to reference any out-of-scope variables.
  foo,
}))

Prefixing with mock does not throw, but value of mock is undefined:

// prefixing with mock avoids out-of-scope error to be thrown
// but value of foo will be undefined
const mockFoo = jest.fn();
jest.mock('./foo', () => ({
  foo: mockFoo,
}))
Read more comments on GitHub >

github_iconTop Results From Across the Web

The module factory of `jest.mock()` is not allowed to ...
babel-plugin-jest-hoist : The module factory of `jest.mock()` is not allowed to reference any out-of-scope variables. Invalid variable access: ...
Read more >
Service mocked with Jest causes "The module factory of ...
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 >
Service mocked with Jest causes “The module factory of ...
js: babel-plugin-jest-hoist: The module factory of jest.mock() is not allowed to reference any out-of-scope variables. Invalid variable access: VocabularyEntry.
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