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.

enableHooks not sufficient if jest config contains "resetMocks: true"

See original GitHub issue

Hello!

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:closed
  • Created 3 years ago
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
FedericoDiRosacommented, Dec 28, 2020

@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 before previousCalls.clear was my solution.

@mikeborozdin did you not include that change in the PR intentionally?

1reaction
thejuancommented, Jul 8, 2020

We are having a simliar issue, we are using { dontMockByDefault: true; } and withHooks() Our tests that use mount also call resetAllMocks which resets the useEffect mock to no implementation and breaks the tests

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jest Coverage fails when using jest.config resetMocks: 'true'
In answer to your issue - your reset/restore are within the coverageThreshold object, move them outside.
Read more >
Jest Addon | Storybook: Frontend workshop for UI development
React storybook addon that show component jest report. ... need to consider that if your tests fail, the script receives a non-0 exit...
Read more >
Shallow Rendering & React Hooks - Mike Borozdin's Blog
import enableHooks from 'jest-react-hooks-shallow'; ... So if you have a component like this: ... toContain('Button clicked: true'); });.
Read more >
Set Up Jest and React Testing Library for Unit Testing
Setting Configs. Dependencies. These are the packages that you need. You can install it separately or add the code below into the devDependencies...
Read more >
Watch Plugins - Jest
The Jest watch plugin system provides a way to hook into specific parts of Jest and to define watch mode menu prompts that...
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