Socket
Socket
Sign inDemoInstall

react-test-renderer

Package Overview
Dependencies
7
Maintainers
5
Versions
1662
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    react-test-renderer

React package for snapshot testing.


Version published
Weekly downloads
4.6M
decreased by-6.71%
Maintainers
5
Install size
1.69 MB
Created
Weekly downloads
 

Package description

What is react-test-renderer?

The react-test-renderer package is designed for testing React components in a more efficient and lightweight manner. It provides a renderer that can be used to render React components into pure JavaScript objects without depending on the DOM or a native mobile environment. This makes it ideal for testing components in isolation from the browser or any other external environment.

What are react-test-renderer's main functionalities?

Rendering components to JSON

This feature allows you to render a React component and generate a JSON output of the rendered component. This is useful for snapshot testing, where you can compare the current version of the component with a previously stored snapshot.

import React from 'react';
import renderer from 'react-test-renderer';
const component = renderer.create(<MyComponent />);
let tree = component.toJSON();
expect(tree).toMatchSnapshot();

Testing component output

This feature enables testing of the component's output. You can inspect the rendered component to verify that it has the expected props, state, or behavior. This is useful for ensuring that your component behaves correctly under various conditions.

import React from 'react';
import renderer from 'react-test-renderer';
const component = renderer.create(<MyComponent prop='value' />);
const instance = component.root;
expect(instance.props.prop).toBe('value');

Interacting with component instances

This feature allows you to interact with the component instances directly. You can simulate events or call methods on the component instance and then re-render. This is useful for testing the component's behavior in response to user interactions or lifecycle events.

import React from 'react';
import renderer from 'react-test-renderer';
class MyComponent extends React.Component {
  state = { clicked: false };
  handleClick = () => {
    this.setState({ clicked: true });
  };
  render() {
    return (<button onClick={this.handleClick}>Click me</button>);
  }
}
const component = renderer.create(<MyComponent />);
const button = component.root.findByType('button');
button.props.onClick();
expect(component.toJSON()).toMatchSnapshot();

Other packages similar to react-test-renderer

Changelog

Source

18.3.0 (April 25, 2024)

This release is identical to 18.2 but adds warnings for deprecated APIs and other changes that are needed for React 19.

Read the React 19 Upgrade Guide for more info.

React

  • Allow writing to this.refs to support string ref codemod 909071
  • Warn for deprecated findDOMNode outside StrictMode c3b283
  • Warn for deprecated test-utils methods d4ea75
  • Warn for deprecated Legacy Context outside StrictMode 415ee0
  • Warn for deprecated string refs outside StrictMode #25383
  • Warn for deprecated defaultProps for function components #25699
  • Warn when spreading key #25697
  • Warn when using act from test-utils d4ea75

React DOM

  • Warn for deprecated unmountComponentAtNode 8a015b
  • Warn for deprecated renderToStaticNodeStream #28874

Readme

Source

react-test-renderer

This package provides an experimental React renderer that can be used to render React components to pure JavaScript objects, without depending on the DOM or a native mobile environment.

Essentially, this package makes it easy to grab a snapshot of the "DOM tree" rendered by a React DOM or React Native component without using a browser or jsdom.

Documentation:

https://reactjs.org/docs/test-renderer.html

Usage:

const ReactTestRenderer = require('react-test-renderer');

const renderer = ReactTestRenderer.create(
  <Link page="https://www.facebook.com/">Facebook</Link>
);

console.log(renderer.toJSON());
// { type: 'a',
//   props: { href: 'https://www.facebook.com/' },
//   children: [ 'Facebook' ] }

You can also use Jest's snapshot testing feature to automatically save a copy of the JSON tree to a file and check in your tests that it hasn't changed: https://jestjs.io/blog/2016/07/27/jest-14.html.

Keywords

FAQs

Last updated on 25 Apr 2024

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc