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.

[TestUtils] shallowRenderer does not automatically call componentDidMount after that

See original GitHub issue

Hi 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:closed
  • Created 8 years ago
  • Reactions:1
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
ericfcommented, Dec 22, 2015

@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 call componentWillUnmount(). One likely assumes that componentWillUnmount() will always be called after componentDidMount().

0reactions
AntonioRedondocommented, Feb 17, 2017

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.

Read more comments on GitHub >

github_iconTop 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 >

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