enableHooks not sufficient if jest config contains "resetMocks: true"
See original GitHub issueHello!
This is a really useful library. Thanks for making it. While trying to integrate this, I realized the steps suggested in the docs don’t work as expected if the jest config as resetMocks
set to true
: https://jestjs.io/docs/en/configuration#resetmocks-boolean
My setupHooks.js
file:
import enableHooks from 'jest-react-hooks-shallow';
enableHooks(jest);
It seems that the resetMocks
option clears the mocks setup by this library before every test. I fixed it by using the now deprecated reenableHooks
function in the following way:
import enableHooks, { reenableHooks } from 'jest-react-hooks-shallow';
enableHooks(jest);
beforeEach(() => {
reenableHooks();
});
I also tried putting just the enableHooks
in a beforeEach
block, but didn’t seem to work. The above combination is the only thing that worked for me.
I’m wondering if you would consider “re-introducing” the deprecated function since it seems to solve for a particular case? Also, do you mind sharing why it was deprecated?
Issue Analytics
- State:
- Created 3 years ago
- Comments:11 (5 by maintainers)
Top GitHub Comments
@ike18t Yes I had a workaround but I believe it got lost when my PR was closed. https://github.com/mikeborozdin/jest-react-hooks-shallow/pull/27/commits/0ea696bf9647abb2490648f285b9879d64dc07e3#diff-71521ce9284c7f9fae19af048f19e230b1034eee8d40ea818af7f2733eff7a05L14
See the
beforeEach
I removed there beforepreviousCalls.clear
was my solution.@mikeborozdin did you not include that change in the PR intentionally?
We are having a simliar issue, we are using
{ dontMockByDefault: true; } and withHooks()
Our tests that usemount
also callresetAllMocks
which resets the useEffect mock to no implementation and breaks the tests