When should you use render and shallow in Enzyme / React tests?


Testing Problem Overview

prior to posting this question, I tried to search in sqa stackexchange but I found no post about shallow and render there, so I hope someone can help me out here.

When should I use shallow and render in testing react components? Based on the airbnb docs, I've made some opinions on the difference of the two:

  1. Since shallow is testing components as a unit, so it should be used for 'parent' components. (ex. Tables, Wrappers, etc.)

  2. Render is for child components.

The reason I asked this question, is that I'm having a hard time to figure out which one I should use (though the docs say that they're very similar)

So, how do I know which one to use in a specific scenario?

Testing Solutions

Solution 1 - Testing

As per the Enzyme docs:

mount(<Component />) for Full DOM rendering is ideal for use cases where you have components that may interact with DOM apis, or may require the full lifecycle in order to fully test the component (ie, componentDidMount etc.)


shallow(<Component />) for Shallow rendering is useful to constrain yourself to testing a component as a unit, and to ensure that your tests aren't indirectly asserting on behavior of child components.


render which is used to render react components to static HTML and analyze the resulting HTML structure.

You can still see the underlying "nodes" in a shallow render, so for example, you can do something like this (slightly contrived) example using AVA as the spec runner:

let wrapper = shallow(<TagBox />);

const props = {
    toggleValue: sinon.spy()

test('it should render two top level nodes', t => {
    t.is(wrapper.children().length, 2);

test('it should safely set all props and still render two nodes', t => {
    t.is(wrapper.children().length, 2);

test('it should call toggleValue when an x class is clicked', t => {

Notice that rendering, setting props and finding selectors and even synthetic events are all supported by shallow rendering, so most times you can just use that.

But, you won't be able to get the full lifecycle of the component, so if you expect things to happen in componentDidMount, you should use mount(<Component />);

This test uses Sinon to spy on the component's componentDidMount

test.only('mount calls componentDidMount', t => {

    class Test extends Component {
        constructor (props) {
        componentDidMount() {
        render () {
            return (
                <div />

    const componentDidMount = sinon.spy(Test.prototype, 'componentDidMount');
    const wrapper = mount(<Test />);



The above will not pass with shallow rendering or render

render will provide you with the html only, so you can still do stuff like this:

test.only('render works', t => {

    // insert Test component here...

    const rendered = render(<Test />);
    const len = rendered.find('div').length;
    t.is(len, 1);

hope this helps!

Solution 2 - Testing

The difference between shallow() and mount() is that  shallow() tests components in isolation from the child components they render while mount()goes deeper and tests a component's children.

For shallow() this means that if the parent component renders another component that fails to render, then a shallow() rendering on the parent will still pass.


All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCyvalView Question on Stackoverflow
Solution 1 - Testing4m1rView Answer on Stackoverflow
Solution 2 - TestingShyam KumarView Answer on Stackoverflow