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.

Unmount and then wrapper.mount() again does not work properly

See original GitHub issue

Enzyme: 2.8.2 React: 15.5.4

If we do wrapper.unmount(), and then wrapper.mount(), the component is not mounted properly. I can see an empty console output after mounting again.

Simple plain example as follows

class Foo extends React.Component {
  constructor(props) {
    super(props);
    
  }
  render() {
    return (
      <div className={this.props.id}>
        {this.props.id}
      </div>
    );
  }
}

const wrapper = mount(<Foo id="foo" />);

console.log("log before unmounting", wrapper.mount().debug()); //This logs the component properly

wrapper.unmount();

console.log("Log again after mounting", wrapper.mount().debug());//this logs empty component

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:3
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
ljharbcommented, Jun 12, 2017

This may be fixed by #969

1reaction
lcarditocommented, May 24, 2017

Hey guys,

According to the docs this should work. However the following test fails:

import React from 'react';
import {mount} from "enzyme";

describe('Foo test', () => {

    const Client = {
        callCount: 0,
        incrementCallCount: () => {
            Client.callCount++;
        }
    };

    class Foo extends React.Component {
        constructor(props) {
            super(props);

        }

        componentWillMount() {
            Client.incrementCallCount();
        }

        render() {
            return (
                <div className={this.props.id}>
                    {this.props.id}
                </div>
            );
        }
    }

    it('should increment call count when mounted', () => {
        let wrapper = mount(<Foo />);
        expect(Client.callCount).toBe(1);

        wrapper.mount();
        expect(Client.callCount).toBe(2);
    });

});

Are we doing something wrong?

Cheers!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unmount and then wrapper.mount() again does not work ...
mount(), the component is not mounted properly. I can see an empty console output after mounting again. Simple plain example as follows class ......
Read more >
React.Component
The render() function should be pure, meaning that it does not modify component ... Once a component instance is unmounted, it will never...
Read more >
A Complete Guide to Testing React Hooks - Toptal
We will pick a sufficiently complex hook and work on testing it. ... We use beforeEach and afterEach to mount and unmount our...
Read more >
React - How to Check if a Component is Mounted or Unmounted
Warning: Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your...
Read more >
API | React Native Testing Library - Open Source
Usually you should not need to call unmount as it is done automatically if your test runner supports afterEach hook (like Jest, mocha,...
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