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.

wrapper.update not working when mocking a custom hook

See original GitHub issue

Current behavior

When mocking a custom hook, the component is not updated when calling wrapper.update().

Example:

component.jsx

import React from 'react';
import useCustomHook from './custom-hook';

export function MyComponent() {
  const hookResult = useCustomHook();

  return <span>{hookResult}</span>;
}

component.test.jsx

import { shallow } from 'enzyme';
import React from 'react';
import { MyComponent } from './component';
import useCustomHook from './custom-hook';

jest.mock('./custom-hook', () => jest.fn(() => 'bananas'));

it('should update the component', () => {
  expect(wrapper.children().text()).toBe('bananas');

  useCustomHook.mockReturnValueOnce('apples');

  wrapper.update();

  expect(wrapper.children().text()).toBe('apples');
})

Expected behavior

The above test passes.

I can get it to work when I replace the wrapper.update() with a wrapper.setProps({}) (which is kinda weird). So why is the component not re-rendering? According to the docs, wrapper.update() is forcing a re-render. But I guess some optimization kicks in here and prevents the re-render.

Your environment

API

  • shallow
  • mount
  • render

Version

library version
enzyme 3.10.0
react 16.8.6
react-dom 16.8.3
react-test-renderer 16.8.6
adapter (below)

Adapter

  • enzyme-adapter-react-16
  • enzyme-adapter-react-16.3
  • enzyme-adapter-react-16.2
  • enzyme-adapter-react-16.1
  • enzyme-adapter-react-15
  • enzyme-adapter-react-15.4
  • enzyme-adapter-react-14
  • enzyme-adapter-react-13
  • enzyme-adapter-react-helper
  • others ( )

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

8reactions
robertknightcommented, Jul 17, 2019

@marcus13371337 - This issue is nothing to do with hooks. The documentation for wrapper.update is simply incorrect.

From your test cases:

✕ cant handle named export without any updates (17ms)

This is expected. When you changed the mock return value from your hook, the component wasn’t re-rendered and the Enzyme tree was not updated.

✕ cant handle named export with wrapper update (4ms)

This is also expected. wrapper.update() refreshes Enzyme’s view of the rendered component tree, but it doesn’t re-render the component. For that you need wrapper.setProps({}).


Thinking about this issue at a higher level, although there are good technical reasons for why Enzyme works the way it does (see the Enzyme v2 => v3 migration guide), this is clearly quite confusing for users. This could perhaps be addressed by providing visual documentation to convey the mental model better, but perhaps there are other changes that could be made to avoid the issue?

3reactions
robertknightcommented, Jul 19, 2019

Is that the same “thing” as doing wrapper.mount() or are there any differences between those approaches?

The docs say that wrapper.mount() unmounts and re-mounts the component, whereas setProps({}) re-renders (aka. “updates”) the component with new props, which won’t trigger lifecycle hooks related to mounting and unmounting.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to mock a custom hook inside of a React component you ...
To mock your custom hook using jest. import * as useCustomHook from '../hooks/useCustomHooks' const spy = jest.spyOn(useCustomHook ...
Read more >
Mocking React hooks when unit testing using Jest
Below I mock the base-fetch module which is responsible for making requests to the SWAPI endpoints and returning a JSON object. Instead of...
Read more >
A Complete Guide to Testing React Hooks - Toptal
This article explores how to test React Hooks and outlines an eight-step testing plan you could employ to test your own projects.
Read more >
Advanced Hooks - React Hooks Testing Library
Simple and complete React hooks testing utilities that encourage good testing practices. ... We can use the wrapper option for renderHook to do...
Read more >
Testing Custom React Hooks with Jest -- newline - Fullstack.io
That way, your tests failing means the problem is with the UI and not somewhere else. How do I mock my hook's dependencies?#....
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