What is Shallow Renderer in React testing?

Shallow Renderer

The Shallow Renderer is a tool for React testing that allows you to render React components one level deep. This means that it renders only the component you specify and none of its children. It's part of the React Testing Library or Enzyme, which are popular testing utilities for React applications.

What the Shallow Renderer does and how it's used?

Here's a more detailed look at what the Shallow Renderer does and how it's used:

  1. Component Isolation: The primary purpose of the Shallow Renderer is to isolate the component you want to test. By rendering only the component being tested and not its children, you can focus your test specifically on that component's behavior and output.
  2. No Child Components: When you use the Shallow Renderer to render a component, any child components within it are not rendered. Instead, they are represented as placeholders. This helps keep the test focused and prevents unnecessary complexity that might arise from rendering child components.
  3. Mocking Child Components: In cases where the behavior of child components is irrelevant to the test, the Shallow Renderer automatically mocks them. This means that even though the child components are not rendered, their presence is acknowledged, allowing the test to proceed without errors related to missing components.
  4. DOM Representation: The Shallow Renderer provides a lightweight representation of the rendered component's DOM structure. This representation is typically simpler compared to a full DOM rendering, making it easier to assert specific expectations about the component's output.
  5. Limited Depth: As the name suggests, the Shallow Renderer only renders components one level deep. This means that if a component renders another component, only the immediate child is rendered, and any further nested components are not rendered. This depth limitation helps maintain the focus on the component being tested.
  6. Testing Presentational Components: Shallow rendering is particularly useful for testing presentational components, which primarily deal with rendering UI elements based on props. Since presentational components tend to have less complex behavior and dependencies, shallow rendering suffices for testing their rendering logic.
  7. Performance Benefits: Shallow rendering can offer performance benefits compared to full DOM rendering, especially when dealing with large component trees. By rendering only the component under test and skipping its children, the test execution time can be reduced

Let's dive deeper into the Shallow Renderer with some code examples. I'll demonstrate how to use the Shallow Renderer with both React Testing Library and Enzyme, two popular testing libraries for React.

Using Shallow Renderer with React Testing Library

// MyComponent.js
import React from 'react';

const MyComponent = ({ name }) => {
  return <div>Hello, {name}!</div>;
};

export default MyComponent;
// MyComponent.test.js
import React from 'react';
import { render } from '@testing-library/react';
import MyComponent from './MyComponent';

describe('MyComponent', () => {
  it('renders without crashing', () => {
    render(<MyComponent name="World" />);
  });

  it('displays the correct text', () => {
    const { getByText } = render(<MyComponent name="World" />);
    expect(getByText('Hello, World!')).toBeInTheDocument();
  });
});

Using Shallow Renderer with Enzyme

// MyComponent.js
import React from 'react';

const MyComponent = ({ name }) => {
  return <div>Hello, {name}!</div>;
};

export default MyComponent;
// MyComponent.test.js
import React from 'react';
import { shallow } from 'enzyme';
import MyComponent from './MyComponent';

describe('MyComponent', () => {
  it('renders without crashing', () => {
    shallow(<MyComponent name="World" />);
  });

  it('displays the correct text', () => {
    const wrapper = shallow(<MyComponent name="World" />);
    expect(wrapper.text()).toEqual('Hello, World!');
  });
});

In both examples, we have a simple functional component MyComponent, that takes a name prop and displays a greeting message. We then have corresponding test files where we use the Shallow Renderer to test the component.

  • In the React Testing Library example, we import render from @testing-library/react and use it to render the component. We then use getByText to find the text we expect and assert its presence in the document.
  • In the Enzyme example, we import shallow from the enzyme and use it to shallow render the component. We then use Enzyme's text() method to get the rendered text content of the component and assert it against our expected value.

These examples demonstrate how to use the Shallow Renderer in both React Testing Library and Enzyme to test React components in isolation