Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to find node on an unmounted component #1411

Closed
expobrain opened this issue Dec 4, 2017 · 16 comments
Closed

Unable to find node on an unmounted component #1411

expobrain opened this issue Dec 4, 2017 · 16 comments

Comments

@expobrain
Copy link

expobrain commented Dec 4, 2017

With Enzyme 3.x and React 16 the following code stopped working:

import React from 'react'
import ReactDOM from 'react-dom'
import { shallow } from 'enzyme'


class MyComponent extends React.Component {

  componentDidMount() {
    const node = ReactDOM.findDOMNode(this)  // <--- error here
  }

  render() {
    return <span>Hello</span>
  }
}


describe('test', () => {

  it('shallow render', () => {
    const wrapper = shallow(<MyComponent />)

    expect(wrapper.find('span').text()).toBe('Hello')
  })
})

Now the error is:

 Invariant Violation: Unable to find node on an unmounted component.
@ljharb
Copy link
Member

ljharb commented Dec 5, 2017

First, componentDidMount should be an instance method, not an own property - just componentDidMount() {

However, I'm not sure why that would cause this issue. Does it work with react 15 and enzyme 3?

@expobrain
Copy link
Author

@ljharb I was playing around with the implementation of componentDidMount() an I posted the incorrect one. I've updated the code for completeness.

I've tested with React 15 and and Enzyme 3.2 and the error is sligthly different:

 Invariant Violation: getNodeFromInstance: Invalid argument.

Is this facebook/react#7371 probably the cause?

@koba04
Copy link
Contributor

koba04 commented Dec 6, 2017

In enzyme v3, shallow calls all lifecycle methods by default.
When you don't want to call componentDidMount, you can use disableLifecycleMethods option.

shallow(<MyComponent />, {disableLifecycleMethods: true}); 

@ljharb
Copy link
Member

ljharb commented Dec 6, 2017

@expobrain yes, I think that is the cause. We may need to add functionality to the react adapters for 15+ to support https://reactjs.org/blog/2016/11/16/react-v15.4.0.html#mocking-refs-for-snapshot-testing.

@expobrain
Copy link
Author

@ljharb cool, thank you. In the meanwhile I can apply @koba04 solution to my code

@dschinkel
Copy link

dschinkel commented Jan 8, 2018

So far Enzyme 3 has been fine and I test a LOT. I use it in React 16 just fine.

@dj-marko
Copy link

@expobrain but what if you wanted to test lifecycle method ?

@expobrain
Copy link
Author

@dj-marko "when I do it I do it on production"(tm) :-)

TBF on my test I din't had yet the need to test the lifecycle method yet, however when I'll need it I'll check again with the latest version of enzyme+enzyme-adapter-react-16 can support them or not yet.

@cantino
Copy link

cantino commented May 24, 2018

I'm running into this same issue with mount.

@EthanJStark
Copy link

Updating enzyme-adapter-react-16 to 1.5.0 solved this for me; see #1813 .

@ljharb
Copy link
Member

ljharb commented Sep 20, 2018

Closing; happy to reopen if it's still reproducible on latest enzyme/adapters.

@ljharb ljharb closed this as completed Sep 20, 2018
@lmerotta-zz
Copy link

I'm using shallow() to render my component in one of my tests, and unless I disable lifecycle callbacks I get the same error. Here is an excerpt of my code:

componentDidMount() {
    this.DOMElement = ReactDOM.findDOMNode(this) // <-- this breaks
    this.scrollElement.addEventListener("scroll", lodash.throttle(() => {
      this.hasScrolled();
    }, 200))
}

and my test:

const component = shallow(<TipeeTopbar getConfig={getConfig} isModuleActive={isModuleActive} />)


expect(component).toMatchSnapshot()

If I add {disableLifecycleMethods: true} to shallow() it works fine, but I need to test the lifecycles, and the scroll handler added in componentDidMount(). I already tried updating enzyme-adapter-react-16 to 1.5.0 but that doesn't change anything

@ljharb
Copy link
Member

ljharb commented Sep 21, 2018

I’d expect that, since you’d need a DOM available to find a DOM node. You’ll either have to use jsdom, or disable lifecycle methods for that test.

@lmerotta-zz
Copy link

I am using jsdom actually when running my tests, but unfortunately it doesn't change anything.

@ljharb
Copy link
Member

ljharb commented Sep 21, 2018

Can you file a new issue, and include component code as well as the stack trace when it breaks?

@lmerotta-zz
Copy link

lmerotta-zz commented Sep 24, 2018

Sure! Here it is: #1835

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants