TypeScript icon, indicating that this package has built-in type declarations

1.2.0 • Public • Published


Testing helpers for use with React's shallowRender test utils.

npm version Build Status Coverage Status MIT Licensed


npm install skin-deep

This lib should work on any version of React since 0.14. To allow for greater flexibility by users, no peerDependencies are included in the package.json. Your dependencies need to be as follows:

For React < 15.5:

  • react
  • react-addons-test-utils

For React >= 15.5:

  • react
  • react-dom
  • react-test-renderer

Quick Start

var React = require('react');
var MyComponent = React.createClass({
  displayName: 'MyComponent',
  render: function() {
    return (
        <li><a href="/">Home</a></li>
        <li><a href="/abc">Draw</a></li>
        <li><a href="/def">Away</a></li>
var assert = require('assert');
var sd = require('skin-deep');
var tree = sd.shallowRender(<MyComponent />);
var homeLink = tree.subTree('a', { href: '/' });
assert.equal(homeLink.type, 'a');
assert.equal(homeLink.props.href, '/');
assert.equal(homeLink.text(), 'Home');

Breaking Changes in 1.0

  • React 0.13 is no longer supported
  • Rendering a function (which would be called) is no longer supported, this only existed for React 0.13's owner-based context
  • subTreeLike has been renamed to subTree
  • everySubTreeLike has been renamed to everySubTree
  • The original subTree has been removed, exact can be used to get this behaviour back, but I don't recommend you do.
  • The original subTreeLike has been removed, exact can be used to get this behaviour back, but I don't recommend you do.
  • removed findNode, use subTree instead
  • removed textIn, use subTree().text() instead
  • removed fillField, use subTree().props.onChange instead
  • removed findComponent, use subTree() instead
  • removed findComponentLike, use subTree() instead
  • toString() uses a completely different approach now. Previously it would use React's string rendering and produce the HTML including expanded children. Now it produces a pretty-printed representation of the result of the render.
  • reRender() now takes props instead of a ReactElement.


The goal of skin-deep is to provide higher level functionality built on top of the Shallow Rendering test utilities provided by React.

By default, shallow rendering gives you a way to see what a component would render without continuing along into rendering its children. This is a very powerful baseline, but in my opinion it isn't enough to create good UI tests. You either have to assert on the whole rendered component, or manually traverse the tree like this:

assert(rendered.props.children[1].props.children[2].children, 'Click Here');

By their nature user interfaces change a lot - sometimes these changes are to behaviour, but sometimes they're simply changes to wording or minor display changes. Ideally, we'd want non-brittle UI tests which can survive these superficial changes, but still check that the application behaves as expected.


Use the shallowRender function to get a tree you can interact with.

var sd = require('skin-deep');
var tree = sd.shallowRender(<MyComponent />);

You can now inspect the the tree to see its contents

// -> ReactElement, same as normal shallow rendering
// -> The component type of the root element
// -> The props of the root element

The real benefits of skin deep come from the ability to extract small portions of the tree with a jQuery-esque API. You can then assert only on these sub-trees.

Extracting portions of the result

Extraction methods all take a CSS-esque selector as the first argument. This is commonly a component or tag name, but can also be a class or ID selector. The special value '*' can be used to match anything.

The second (optional) argument is the matcher, this can be an object to match against props, or a predicate function which will be passed each node and can decide whether to include it.

// -> the first Button component
// -> all the button components
// -> the first component with class of button-primary
// -> the first component with id of submit-button
tree.subTree('Button', { type: 'submit' });
// -> the first Button component with type=submit
tree.subTree('*', { type: 'button' });
// -> All components / elements with type=button
tree.subTree('*', function(node) { return node.props.size > 20; });
// -> All components / elements with size prop above 20

Triggering events

There's no DOM involved, so events could be a bit tricky - but as we're just using data, we can call functions directly!

var MyButton = React.createClass({
  clicked: function(e) {
  render: function() {
    return <button onClick={this.clicked}>Click {this.props.n}</button>;
var tree = sd.shallowRender(<MyButton />);
  target: {
    innerHTML: 'Whatever you want!'

Going deeper

Sometimes shallow rendering isn't enough - often you'll want to have some integration tests which can render a few layers of your application. I prefer not to have to use a full browser or jsdom for this sort of thing - so we introduced the dive method. This allows you to move down the tree, recursively shallow rendering as needed.

var MyList = React.createClass({
  render: function() {
    return <ul>{[1,2,3].map(function(n) { return <MyItem n={n} />; })}</ul>;
var MyItem = React.createClass({
  render: function() {
    return <li><MyButton>{this.props.n}</MyButton></li>;
var MyButton = React.createClass({
  render: function() {
    return <button>Click {this.props.n}</button>;
var tree = sd.shallowRender(<MyList />);
var buttonElement = tree.dive(['MyItem', 'MyButton']);
assert(buttonElement.text(), 'Click 1');

Use with test frameworks

TODO: flesh this out a bit more

Skin deep doesn't care which test framework you use, it just gives you the data you need to make assertions.

If you want to take this further, should be pretty simple to extend your favorite assertion library to be skin-deep aware.

As we use tend to use chai, there's a chai plugin bundled inside this package. You can use it via chai.use(require('skin-deep/chai')).

API Docs

.shallowRender(element [, context])

Get a tree instance by shallow-rendering a renderable ReactElement.

  • element {ReactElement} - element to render
  • context {object} - optional context

Returns tree



Access the type of the rendered root element.

Returns ReactComponent class or string.


Access the props of the rendered root element.

Returns object

tree.reRender(props [, context])

Re-render the element with new props into the same tree as the previous render. Useful for testing how a component changes over time in response to new props from its parent.

  • props {object} - the new props, which will replace the previous ones
  • context {object} - optional context

Returns null


Access the textual content of the rendered root element including any text of its children. This method doesn't understand CSS, or really anything about HTML rendering, so might include text which wouldn't be displayed to the user.

If any components are found in the tree, their textual representation will be a stub like <Widget />. This is because they could do anything with their props, and thus are not really suitable for text assertions. If you have any suggestions for how to make it easier to do text assertions on custom components, please let me know via issues.

Returns string


Produce a friendly JSX-esque representation of the rendered tree.

This is not really suitable for asserting against as it will lead to very brittle tests. Its main purpose is supposed to be for printing out nice debugging information, eg "no <selector> found in <tree.toString()>".

Returns string


Access the rendered component tree. This is the same result you would get using shallow rendering without skin-deep.

You usually shouldn't need to use this.

Returns ReactElement


Access the mounted instance of the component.

Returns object

tree.subTree(selector [, matcher])

Extract a portion of the rendered component tree. If multiple nodes match the selector, will return the first.

  • selector {Selector} - how to find trees
  • matcher {Matcher} - optional additional conditions

Returns tree or false

tree.everySubTree(selector [, matcher])

Extract multiple portions of the rendered component tree.

  • selector {Selector} - how to find trees
  • matcher {Matcher} - optional additional conditions

Returns array of trees


"Dive" into the rendered component tree, rendering the next level down as it goes. See Going Deeper for an example.

Returns tree Throws if the path cannot be found.

Using Selectors


Using Matchers



Create a matcher which only accepts nodes that have exactly those props passed in - no extra props.

  • props {object} - to match against

Returns function


A magic value which can be used in a prop matcher that will allow any value to be matched. It will still fail if the key doesn't exist


{ abc: sd.any }
// Will match each of the following
<Component abc="1" />
<Component abc={100} />
<Component abc={function(){}} />
// but not
<Component />
<Component def="1" />

.hasClass(node, className)

Helper function to check if a node has the HTML class specified. Exported in case you want to use this in a custom matcher.

Package Sidebar


npm i skin-deep

Weekly Downloads






Last publish


  • andrew_h
  • glenjamin