[TestUtils] shallowRenderer does not automatically call componentDidMount after that
See original GitHub issueHi I got a case. When i try to use shallowRenderer to test this component. ex: test console.log has been called. It won’t work.
- component.jsx
class TestComponent extends React.Component {
componentDidMount() {
console.log('componentDidMount');
}
render() {
return <div/>
}
- component-test.js
it('Should call console.log after componentDidMount', () => {
spyOn('console', 'log')
shallow(<TestComponent />)
expect(console.log).toHaveBeenCalled()
});
Issue Analytics
- State:
- Created 8 years ago
- Reactions:1
- Comments:7 (2 by maintainers)
Top Results From Across the Web
Is componentDidMount supposed to run with shallow ...
I'm on version 3.3.0 and componentDidMount is not getting called during a shallow render but componentWillMount does. Anyone have an idea what's ...
Read more >Test Utilities - React
Traverse all components in tree and accumulate all components where test(component) is true . This is not that useful on its own, but...
Read more >react-addons-test-utils | Yarn - Package Manager
TestUtils have been moved to react-dom/test-utils; Shallow renderer has been moved to react-test-renderer/shallow. Display full readme Display full readme ...
Read more >How to get started writing unit tests for React with jest ... - JS.dev
The following hooks do not behave as expected when using shallow : useEffect() and useLayoutEffect() doesn't get called; useCallback() doesn't ...
Read more >check the render method of `router.consumer`. - You.com
Also, note that my issue was in my App.test.js file so it may not apply to OP's specific use case ... It no...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@jimfb the full tree is rendered when testing leaf nodes. There was an attempt in #4993 to have shallow rendering call all lifecycle methods, but it was backed out because of a
refs
issue.It’s also confusing that there’s an
shallowRenderer.unmount()
method which will callcomponentWillUnmount()
. One likely assumes thatcomponentWillUnmount()
will always be called aftercomponentDidMount()
.The fact that shallow render just renders one level deep doesn’t imply
componentDidMount()
won’t be called. And this isn’t said anywhere on React or Enzyme documentation.