Invalid hook call after `jest.resetModules` for dynamic `require`s

See original GitHub issue

🐛 Bug Report

Invalid hook call when using jest.resetModules or jest.resetModuleRegistry with dynamic require in test

To Reproduce

Steps to reproduce the behavior: steps

Expected behavior

No invalid hook call

Link to repl or repo (highly encouraged)

demo repo

envinfo

  System:
    OS: macOS Mojave 10.14.6
    CPU: (8) x64 Intel(R) Core(TM) i7-4850HQ CPU @ 2.30GHz
  Binaries:
    Node: 10.16.3 - ~/.nvm/versions/node/v10.16.3/bin/node
    Yarn: 1.17.3 - /usr/local/bin/yarn
    npm: 6.11.3 - ~/.nvm/versions/node/v10.16.3/bin/npm
  npmPackages:
    jest: 24.9.0 => 24.9.0

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:66
  • Comments:17

github_iconTop GitHub Comments

33reactions
quickgiantcommented, Jul 6, 2021

I have also been running into this issue in a particularly nasty way. We use the resetModules config option globally, so tricks around calling jest.isolateModules() don’t work well. Instead, I came up with a workaround that globally excludes React from jest’s resetModules behavior.

In a file specified in the setupFiles config option, add the following:

let mockActualReact;

jest.doMock('react', () => {
  if (!mockActualReact) {
    mockActualReact = jest.requireActual('react');
  }
  return mockActualReact;
});

This sets up a mock for React that lazily loads the actual React, and always returns the result of the first initialization. This could also be generalized to implement a set of modules that are globally excluded from resetModules.

Generalized version:

const RESET_MODULE_EXCEPTIONS = [
  'react',
  'react-redux',
];

let mockActualRegistry = {};

RESET_MODULE_EXCEPTIONS.forEach(moduleName => {
  jest.doMock(moduleName, () => {
    if (!mockActualRegistry[moduleName]) {
      mockActualRegistry[moduleName] = jest.requireActual(moduleName);
    }
    return mockActualRegistry[moduleName];
  });
});

Personally, I’ve found this to be a useful construct, and now that the React hook issue is more prevalent, it might be good to have a version of this built in to Jest as something like a persistentModules or excludeFromResetModules option.

17reactions
lukevellacommented, Nov 6, 2019

@kapral18 We are also experiencing the same issue. Have you found a work around for this?

Read more comments on GitHub >

github_iconTop Results From Across the Web

"Invalid hook call" when mocking React HOC with Jest
It looks like this is a bug in Jest: Invalid hook call after `jest.resetModules` for dynamic `require`s. The bug happens when you call...
Read more >
Invalid hook call" when mocking React HOC with Jest-Reactjs
It looks like this is a bug in Jest: Invalid hook call after `jest.resetModules` for dynamic `require`s. The bug happens when you call...
Read more >
Reactjs – “Invalid hook call” when mocking React HOC with Jest ...
It looks like this is a bug in Jest: Invalid hook call after `jest.resetModules` for dynamic `require`s. The bug happens when you call...
Read more >
Fix Invalid Hook Call Warning in React Tests - Sinistra
"Invariant Violation: Hooks can only be called inside the body of a function component." But... you ARE! A likely culprit is that you...
Read more >
Globals - Jest
You don't have to require or import anything to use them. ... Here the afterAll ensures that cleanUpDatabase is called after all tests...
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