Skin-Deep React Testing Utilities
Skin-Deep provides testing helpers built on top of React's shallow rendering test utilities. Its primary goal is to offer higher-level functionality for unit testing React components, allowing developers to assert on a component's rendered output without fully mounting it or its children. The current stable version is 1.2.0. This library was designed to work with React versions 0.14 and above, requiring specific React testing utility packages depending on the React version (e.g., `react-addons-test-utils` for <15.5 or `react-test-renderer` for >=15.5). Skin-Deep offers methods to extract portions of the rendered tree, trigger events, and dive into sub-trees, providing a structured way to interact with a shallow-rendered component's virtual DOM. Its release cadence is effectively inactive, with the last update several years ago, making it unsuitable for modern React applications (React 18+).
Common errors
-
TypeError: tree.subTreeLike is not a function
cause Attempting to use the old `subTreeLike` method after upgrading to `skin-deep` v1.0 or later.fixRename `subTreeLike` to `subTree` in your test code: `tree.subTree(...)`. -
TypeError: Cannot read properties of undefined (reading 'textIn')
cause Using the deprecated `textIn` method which was removed in `skin-deep` v1.0.fixUse `subTree().text()` instead: `tree.subTree('.some-selector').text()`. -
Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.
cause This error can occur when using `skin-deep` with modern React if `createClass` components are mixed with functional or class components, or if the React version is too new for the `react-test-renderer` version being used implicitly by `skin-deep`.fixEnsure you are using `skin-deep` with a compatible React version (<= React 16 is generally safer). For modern React (17+), consider migrating to `@testing-library/react` which is actively maintained and designed for current React APIs.
Warnings
- breaking React 0.13 is no longer supported since Skin-Deep v1.0. If you are using an older React version, you must remain on Skin-Deep < 1.0.
- breaking The `subTreeLike` and `everySubTreeLike` methods were renamed to `subTree` and `everySubTree` respectively in Skin-Deep v1.0.
- breaking Several methods including `findNode`, `textIn`, `fillField`, `findComponent`, and `findComponentLike` were removed in Skin-Deep v1.0.
- breaking The `toString()` method's output changed significantly in v1.0, moving from HTML string rendering to a pretty-printed representation of the render result.
- breaking The `reRender()` method now accepts `props` as its argument instead of a full `ReactElement`.
- gotcha Skin-Deep's development has been inactive for several years (last published 8 years ago as of 2026), making it incompatible with modern React versions like React 18+ and their concurrent rendering features. Using it with recent React versions will likely lead to unexpected behavior or errors.
Install
-
npm install skin-deep -
yarn add skin-deep -
pnpm add skin-deep
Imports
- shallowRender
const shallowRender = require('skin-deep').shallowRender;import { shallowRender } from 'skin-deep'; // Or import * as sd from 'skin-deep'; sd.shallowRender(...) - exact
const exact = require('skin-deep').exact;import { exact } from 'skin-deep'; - hasClass
const hasClass = require('skin-deep').hasClass;import { hasClass } from 'skin-deep';
Quickstart
import React from 'react';
import * as sd from 'skin-deep';
import assert from 'assert';
// Define a simple React component
class MyComponent extends React.Component {
render() {
return (
<ul>
<li><a href="/">Home</a></li>
<li><a href="/abc">Draw</a></li>
<li><a href="/def">Away</a></li>
</ul>
);
}
}
// Shallow render the component
const tree = sd.shallowRender(<MyComponent />);
// Find a specific sub-tree using a selector and matcher
const homeLink = tree.subTree('a', { href: '/' });
// Assertions on the extracted sub-tree
assert.equal(homeLink.type, 'a');
assert.equal(homeLink.props.href, '/');
assert.equal(homeLink.text(), 'Home');
console.log('Shallow rendering and assertions successful!');