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:
Expected behavior
No invalid hook call
Link to repl or repo (highly encouraged)
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:
- Created 4 years ago
- Reactions:66
- Comments:17
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I have also been running into this issue in a particularly nasty way. We use the
resetModules
config option globally, so tricks around callingjest.isolateModules()
don’t work well. Instead, I came up with a workaround that globally excludes React from jest’sresetModules
behavior.In a file specified in the
setupFiles
config option, add the following: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:
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
orexcludeFromResetModules
option.@kapral18 We are also experiencing the same issue. Have you found a work around for this?