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.

[Bug]: Jest 29 not respecting `mock` prefix for variables when hoisting

See original GitHub issue

Version

29.1.1

Steps to reproduce

  1. clone https://github.com/jcollum-nutrien/jest-mock-issue
  2. yarn
  3. yarn test

Dupe of #10996, which is locked (but also a year old)

Expected behavior

Variables prefixed with mock should be hoisted, as the docs state.

Actual behavior

Cannot access 'mockInfo' before initialization

yarn run v1.22.15
$ jest
 FAIL  ./logger.spec.ts
  ● Test suite failed to run

    ReferenceError: Cannot access 'mockInfo' before initialization

       5 | jest.mock('./logger', ()=>{
       6 |   return {
    >  7 |     info: mockInfo
         |           ^
       8 |   }
       9 | })
      10 | describe('repro', function () {

      at mockInfo (logger.spec.ts:7:11)
      at Object.<anonymous> (logger.spec.ts:4:1)

Additional context

No response

Environment

npx envinfo --preset jest
npx: installed 1 in 0.74s

  System:
    OS: macOS 12.5.1
    CPU: (10) x64 Apple M1 Pro
  Binaries:
    Node: 14.19.2 - ~/.nvm/versions/node/v14.19.2/bin/node
    Yarn: 1.22.19 - ~/work/jest-mock-issue/node_modules/.bin/yarn
    npm: 6.14.17 - ~/.nvm/versions/node/v14.19.2/bin/npm
  npmPackages:
    jest: 29.1.1 => 29.1.1

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
mrazauskascommented, Sep 29, 2022

With two changes in your reproduction seems to be working:

  1. Use babel-jest as a transformer. It works as documented. If a third party transformer does not work for you, Jest can’t help here.
  2. Please read this section carefully and rework with test:
jest.mock("./logger", () => {
  return jest.fn().mockImplementation(() => {
    return { info: mockInfo };
  });
});

The ‘ReferenceError’ issue should be gone. (Note that I did not try to make your test pass, that is not in the scope of the issue.)

1reaction
jcollum-nutriencommented, Sep 29, 2022

For anyone who finds this later: I switched it over to babel-jest. Still ran into issues, had to change the mock to:

const mockInfo = jest.fn();
jest.mock('./logger', () => {
  return {
    Logger: jest.fn().mockImplementation(() => {
      return {
        info: mockInfo
      }
    })
  }
});

Repo has been updated. Test is passing.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Service mocked with Jest causes "The module factory of jest ...
You need to store your mocked component in a variable with a name prefixed by "mock". This solution is based on the Note...
Read more >
jest mock cannot access before initialization - You.com
Jest mock aws-sdk ReferenceError: Cannot access before initialization ... Jest 29 not respecting `mock` prefix for variables when hoisting#13333.
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.
Read more >
@rushstack/eslint-plugin - npm
Jest module mocking APIs such as "jest.mock()" must be called before the associated module is imported, otherwise they will have no effect.
Read more >
ECMAScript Modules - Jest
Since ESM evaluates static import statements before looking at the code, the hoisting of jest.mock calls that happens in CJS won't work for...
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