enzyme: Unable to find node on an unmounted component
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.
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 16
- Comments: 16 (6 by maintainers)
I’m running into this same issue with
mount
.Updating
enzyme-adapter-react-16
to1.5.0
solved this for me; see #1813 .So far Enzyme 3 has been fine and I test a LOT. I use it in React 16 just fine.
In enzyme v3,
shallow
calls all lifecycle methods by default. When you don’t want to callcomponentDidMount
, you can usedisableLifecycleMethods
option.@dj-marko "when I do it I do it on production"™ 😃
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.First,
componentDidMount
should be an instance method, not an own property - justcomponentDidMount() {
However, I’m not sure why that would cause this issue. Does it work with react 15 and enzyme 3?