# react-test-wrapper

> A set of classes to make setting up React components for unit tests easy.

Latest version **4.1.0** (published 2024-12-16) · ISC license · 0 weekly downloads

## Install

```sh
npm install react-test-wrapper
pnpm add react-test-wrapper
yarn add react-test-wrapper
bun add react-test-wrapper
```

## Health

**Score 40/100 (D)** — status: maintenance-mode.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 4.1.0 |
| Published | 2024-12-16 |
| First published | 2019-12-11 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Unpacked size | 123.3 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Raice Hannay |
| Maintainers | voodoocreation |
| Keywords | component, react-testing-library, jest, react, react-intl, react-redux, redux, test, tests, tester, testing, unit-test, unit-tests, unit-testing |

## Links

- npm: https://www.npmjs.com/package/react-test-wrapper
- Repository: git@github.com:voodoocreation/react-test-wrapper
- Homepage: https://github.com/voodoocreation/react-test-wrapper#readme
- Issues: https://github.com/voodoocreation/react-test-wrapper/issues
- npm.io page: https://npm.io/package/react-test-wrapper

## Dependencies (2)

- [ts-deepmerge](https://npm.io/package/ts-deepmerge.md) ^7.0.2
- [utility-types](https://npm.io/package/utility-types.md) ^3.11.0

## Alternatives

- [duck](https://npm.io/package/duck.md) — 4.2M weekly downloads
- [ava](https://npm.io/package/ava.md) — 560.2K weekly downloads
- [storybook-addon-module-mock](https://npm.io/package/storybook-addon-module-mock.md) — 71.7K weekly downloads
- [@ethereum-waffle/mock-contract](https://npm.io/package/@ethereum-waffle/mock-contract.md) — 40.0K weekly downloads
- [aws-elasticsearch-connector](https://npm.io/package/aws-elasticsearch-connector.md) — 37.4K weekly downloads

## Recent versions

- 4.1.0 (latest) — 2024-12-16
- 4.0.0 — 2024-01-24
- 3.1.0 — 2022-09-28
- 3.0.5 — 2022-09-15
- 3.0.4 — 2021-07-09
- 3.0.3 — 2021-07-08
- 3.0.2 — 2021-07-08
- 3.0.1 — 2021-07-08
- 3.0.0 — 2021-07-08
- 2.1.1 — 2021-05-12
- 2.1.0 — 2020-11-17
- 2.0.4 — 2020-07-01
- 2.0.3 — 2020-04-01
- 2.0.2 — 2020-03-05
- 2.0.1 — 2020-02-11
- … 10 more at https://npm.io/package/react-test-wrapper/versions

## README

React Test Wrapper
==================

The intention of this package is to provide a simple, clean way of setting up your React components
for unit tests, to reduce test setup boilerplate code, whilst automatically inferring your component
TypeScript definitions (props, instance methods etc) to avoid needing to manually import types and
retype definitions in your tests and also providing better inline IDE autocomplete/validation support.

The classes provided are also able to be extended to add to the API that's available, if your
project requires additional functionality as part of your component test setup.

The concept behind it is that you can create a single instance of the wrapper class at the top of
your test file and define the defaults to use there, then in each test scenario you can reference
the single instance and define the scenario-specific props/children etc. chaining the public methods,
then finally calling the `render` method to return the rendering result.

The scenario-specific definitions are reset each time you call `render`, which
will ensure it reverts back to only the defaults set at the top and prevents scenario data from leaking
between tests.

## Example
```typescript jsx
import { screen, Wrapper } from "react-test-wrapper";

const component = new Wrapper(SomeComponent)
  .withDefaultChildren(<div className="Child" />)
  .withDefaultProps({
    prop1: "Default value 1",
    prop2: "Default value 2"
  });

describe("when testing a scenario", () => {
  let result: ReturnType<typeof component.render>;

  it("renders the component", () => {
    result = component
      .withProps({
        prop1: "Scenario value 1"
      })
      .render();
  });

  it("uses the scenario-specific value for prop1", () => {
    expect(screen.getByText("Scenario value 1")).toBeDefined();
  });

  it("uses the default value for prop2", () => {
    expect(screen.getByText("Default value 2")).toBeDefined();
  });

  it("updates the props", () => {
    result.updateProps({
      prop1: "New scenario value 1"
    });
  });

  it("renders the new prop value", () => {
    expect(screen.getByText("New scenario value 1")).toBeDefined();
  });
});
```

Package contents
----------------
- [`Wrapper`](/docs/react-testing-library/Wrapper.md)
- [`WrapperWithIntl`](/docs/react-testing-library/WrapperWithIntl.md)
- [`WrapperWithRedux`](/docs/react-testing-library/WrapperWithRedux.md)
- [Custom `react-testing-library` queries](/docs/react-testing-library/queries.md)

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