Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@storybook/testing-react

Package Overview
Dependencies
Maintainers
30
Versions
72
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@storybook/testing-react

Testing utilities that allow you to reuse your stories in your unit tests

  • 2.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
168K
increased by8.31%
Maintainers
30
Weekly downloads
 
Created

What is @storybook/testing-react?

@storybook/testing-react is a package that provides utilities for testing React components that are built with Storybook. It allows you to reuse your Storybook stories in your tests, ensuring consistency between your stories and your tests.

What are @storybook/testing-react's main functionalities?

Render a Story

This feature allows you to render a Storybook story in a test environment. The `composeStories` function imports all stories from a file and allows you to use them in your tests. In this example, the `Primary` story from `Button.stories` is rendered and tested.

import { render } from '@testing-library/react';
import { composeStories } from '@storybook/testing-react';
import * as stories from './Button.stories';

const { Primary } = composeStories(stories);

test('renders primary button', () => {
  const { getByText } = render(<Primary />);
  expect(getByText('Primary Button')).toBeInTheDocument();
});

Test Story Interactions

This feature allows you to test interactions within your stories. In this example, the `Clickable` story from `Button.stories` is rendered, and a click event is simulated on the button. The test then checks if the button's text content changes as expected.

import { render, fireEvent } from '@testing-library/react';
import { composeStories } from '@storybook/testing-react';
import * as stories from './Button.stories';

const { Clickable } = composeStories(stories);

test('button click', () => {
  const { getByText } = render(<Clickable />);
  const button = getByText('Click Me');
  fireEvent.click(button);
  expect(button).toHaveTextContent('Clicked');
});

Snapshot Testing

This feature allows you to create snapshot tests for your stories. In this example, the `Primary` story from `Button.stories` is rendered, and a snapshot is created to ensure the component's output does not change unexpectedly.

import { render } from '@testing-library/react';
import { composeStories } from '@storybook/testing-react';
import * as stories from './Button.stories';

const { Primary } = composeStories(stories);

test('primary button snapshot', () => {
  const { asFragment } = render(<Primary />);
  expect(asFragment()).toMatchSnapshot();
});

Other packages similar to @storybook/testing-react

Keywords

FAQs

Package last updated on 21 Apr 2023

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc