React Testing: Intro to Shallow Rendering

InstructorTrevor Miller

Share this video with your friends

Send Tweet

In this lesson, we walk through how to use one of React's Test Utilities (from the react-addons-test-utils package) called "Shallow Rendering". This lets us render our React component one level deep - without a DOM - so that we can write tests for it. It works kind of like ReactDOM.render, where the shallow renderer is a temporary place to "hold" your rendered component so that you can assert things about its output. Tests written using the shallow renderer are great for stateless or "dumb" components that simply have their props passed to them from a parent container or "smart" component. These shallow renderer tests work especially well with stateless function components. They also work well for "unit" tests where you want to make sure your code works in isolation.

NOTE: The React team has recommended composing the majority of your apps using these stateless "dumb" components, so the majority of lessons in this course will focus on writing simple unit tests for these stateless components using Shallow Rendering. If you also want to write tests for the stateful components that are tied to different components and state and can't be tested in isolation, you may want to look at using a DOM (with something like Karma or jsdom) and React's other test utilities like renderIntoDocument and Simulate. However, I've found that it is helpful to try to compose most of your project with simple, isolated, stateless or "pure" components that can be unit tested with Shallow Rendering, and then wrap these components with a few stateful or "impure" components that you can either not worry about testing (what I do most of the time because it is difficult to test stateful components), or write separate integration and functional tests for them using different tools.

uleen
~ 9 years ago

amazing vim customization) can you share your config?

Trevor Millerinstructor
~ 9 years ago

Hi Uleen,

Thanks! Its actually mostly vanilla Vim; here my minimal .vimrc: https://github.com/trevordmiller/settings/blob/master/dotfiles/.vimrc

squirm-life
~ 8 years ago

I get an error:

  1. CoolComponent should...: TypeError: Can't add property context, object is not extensible at [object Object].ReactCompositeComponentMixin.mountComponent (node_modules/react/lib/ReactCompositeComponent.js:154:18) at [object Object].wrapper [as mountComponent] (node_modules/react/lib/ReactPerf.js:70:21) at [object Object].ReactShallowRenderer._render (node_modules/react/lib/ReactTestUtils.js:392:14) at [object Object].ReactShallowRenderer.render (node_modules/react/lib/ReactTestUtils.js:376:8) at Context.<anonymous> (src/jsx/lifelock/components/credit/charts/utils/CoolComponent.spec.js:17:14)

import React from 'react'; import TestUtils from 'react-addons-test-utils'; import expect from 'expect'; // import getHistoryDates from './getHistoryDates';

const CoolComponent = ({greeting}) => (

<div> <h1>Greeting</h1> <p>{greeting}</p> </div> );

describe('CoolComponent', () => {

it('should...', () => { const renderer = TestUtils.createRenderer(); renderer.render(<CoolComponent greeting='1453853919143' />); const output = renderer.getRenderOutput(); console.log(output); }); });

tnl-matt
~ 6 years ago

In React 16...

npm install react-test-renderer --save-dev then... import { createRenderer } from 'react-test-renderer/shallow';