# react-shallow-renderer

> React package for shallow rendering.

Latest version **16.15.0** (published 2022-04-06) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-shallow-renderer
pnpm add react-shallow-renderer
yarn add react-shallow-renderer
bun add react-shallow-renderer
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 16.15.0 |
| Published | 2022-04-06 |
| First published | 2015-10-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 120.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 37 |
| Maintainers | ljharb, gaearon, brianvaughn, trueadm, minh.nguyen |
| Keywords | react, react-native, react-testing |

## Links

- npm: https://www.npmjs.com/package/react-shallow-renderer
- Repository: https://github.com/NMinhNguyen/react-shallow-renderer
- Homepage: https://reactjs.org/
- Issues: https://github.com/NMinhNguyen/react-shallow-renderer/issues
- npm.io page: https://npm.io/package/react-shallow-renderer

## Dependencies (2)

- [react-is](https://npm.io/package/react-is.md) ^16.12.0 || ^17.0.0 || ^18.0.0
- [object-assign](https://npm.io/package/object-assign.md) ^4.1.1

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 16.15.0 (latest) — 2022-04-06
- 16.14.1 — 2020-10-21
- 16.14.0 — 2020-10-20
- 16.13.1 — 2020-03-16
- 16.13.0 — 2020-02-28
- 16.12.0 — 2020-02-26
- 1.1.4 — 2015-10-10
- 1.1.3 — 2015-10-10
- 1.1.2 — 2015-10-10
- 1.1.1 — 2015-10-10
- 1.1.0 — 2015-10-10
- 1.0.1 — 2015-10-10
- 1.0.0 — 2015-10-10

## README

# `react-shallow-renderer`

[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/NMinhNguyen/react-shallow-renderer/blob/master/LICENSE)
[![npm version](https://img.shields.io/npm/v/react-shallow-renderer)](https://www.npmjs.com/package/react-shallow-renderer)
[![CircleCI](https://img.shields.io/circleci/build/github/NMinhNguyen/react-shallow-renderer)](https://circleci.com/gh/NMinhNguyen/react-shallow-renderer/tree/master)

When writing unit tests for React, shallow rendering can be helpful. Shallow rendering lets you render a component "one level deep" and assert facts about what its render method returns, without worrying about the behavior of child components, which are not instantiated or rendered. This does not require a DOM.

## Installation

```sh
# npm
npm install react-shallow-renderer --save-dev

# Yarn
yarn add react-shallow-renderer --dev
```

## Usage

For example, if you have the following component:

```jsx
function MyComponent() {
  return (
    <div>
      <span className="heading">Title</span>
      <Subcomponent foo="bar" />
    </div>
  );
}
```

Then you can assert:

```jsx
import ShallowRenderer from 'react-shallow-renderer';
// in your test:
const renderer = new ShallowRenderer();
renderer.render(<MyComponent />);
const result = renderer.getRenderOutput();
expect(result.type).toBe('div');
expect(result.props.children).toEqual([
  <span className="heading">Title</span>,
  <Subcomponent foo="bar" />,
]);
```

Shallow testing currently has some limitations, namely not supporting refs.

> Note:
>
> We also recommend checking out Enzyme's [Shallow Rendering API](https://airbnb.io/enzyme/docs/api/shallow.html). It provides a nicer higher-level API over the same functionality.

## Reference

### `shallowRenderer.render()`

You can think of the shallowRenderer as a "place" to render the component you're testing, and from which you can extract the component's output.

`shallowRenderer.render()` is similar to [`ReactDOM.render()`](https://reactjs.org/docs/react-dom.html#render) but it doesn't require DOM and only renders a single level deep. This means you can test components isolated from how their children are implemented.

### `shallowRenderer.getRenderOutput()`

After `shallowRenderer.render()` has been called, you can use `shallowRenderer.getRenderOutput()` to get the shallowly rendered output.

You can then begin to assert facts about the output.

---
_Source: https://npm.io/package/react-shallow-renderer · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
