Skip to content

Shallow Rendering

In one line

Shallow rendering shows a component without its children — useful for isolating a component's logic from its children.

In simple words

Shallow rendering is a way to test a component without rendering its child components. It renders only the component itself — children appear as stub elements with their type but no content.

For example, if you have <Profile> which renders <Avatar> and <UserInfo>, shallow rendering would show <Profile> with <Avatar /> and <UserInfo /> as placeholder elements, but wouldn't render what's inside Avatar or UserInfo.

The benefit: you test the component's own logic in isolation, without worrying about its children's behavior. If a child has a bug, your component's test still passes.

The downside: you're not testing the real rendered output. Modern testing philosophy (React Testing Library) prefers full rendering because it tests real behavior. Shallow rendering was popular with enzyme but is now discouraged by the React team.

Example

javascript
// Shallow rendering — test a component without its children

import { shallow } from '@testing-library/react'; // or enzyme
import { UserCard } from './UserCard';
import { Avatar } from './Avatar';  // child component

test('UserCard renders name without rendering Avatar', () => {
  const wrapper = shallow(<UserCard name="Alice" avatarUrl="x.png" />);

  // Avatar is NOT actually rendered — just stubbed as <Avatar />
  expect(wrapper.find(Avatar).length).toBe(1);
  expect(wrapper.find('.name').text()).toBe('Alice');

  // Avatar's internal logic is NOT exercised — that's the point
  // of shallow rendering: isolate the component under test.
});

Shallow rendering renders only one level deep: UserCard is rendered but its child Avatar is stubbed (not actually rendered). This isolates the component under test from its children's behavior.

Related terms

Related Resources

Related Lessons

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to glossary