All Projects → storybookjs → Testing React

storybookjs / Testing React

Licence: mit
Testing utilities that allow you to reuse your Storybook stories in your React unit tests!

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Testing React

Cra Recipe
Step-by-step guide to bootstrap a CRA app from scratch.
Stars: ✭ 158 (-7.6%)
Mutual labels:  storybook
Crna Recipe
Step-by-step guide to bootstrap a React Native app from scratch
Stars: ✭ 174 (+1.75%)
Mutual labels:  storybook
Lerna Yarn Workspaces Monorepo
🐉 A Monorepo with multiple packages and a shared build, test, and release process.
Stars: ✭ 201 (+17.54%)
Mutual labels:  storybook
React Rough
🐇 React Components for Rough.js
Stars: ✭ 164 (-4.09%)
Mutual labels:  storybook
2019 12
🎟 급증하는 트래픽에도 안정적인 예약 서비스, Atomic Pattern을 적용한 재사용 가능한 컴포넌트, 실용적인 Testing을 주제로 하는 이벤트 서비스
Stars: ✭ 169 (-1.17%)
Mutual labels:  storybook
Storybook Addon Jest
REPO/PACKAGE MOVED - React storybook addon that show component jest report
Stars: ✭ 177 (+3.51%)
Mutual labels:  storybook
Workflow Reactjs
My workflow with ReactJS + Webpack 3+
Stars: ✭ 150 (-12.28%)
Mutual labels:  storybook
Addon Jsx
This Storybook addon show you the JSX / template of the story
Stars: ✭ 209 (+22.22%)
Mutual labels:  storybook
Storybook Addon Vue Info
Info addon for Vue components
Stars: ✭ 174 (+1.75%)
Mutual labels:  storybook
Koa Vue Notes Web
🤓 This is a simple SPA built using Koa as the backend, Vue as the first frontend, and React as the second frontend. Features MySQL integration, user authentication, CRUD note actions, and Vuex store modules.
Stars: ✭ 200 (+16.96%)
Mutual labels:  storybook
Awesome Storybook
A collection of awesome resources about @storybookjs ecosystem 🎨
Stars: ✭ 165 (-3.51%)
Mutual labels:  storybook
Storybook Addon Styled Component Theme
storybook addon
Stars: ✭ 168 (-1.75%)
Mutual labels:  storybook
Learnstorybook.com
Static site and content for Storybook tutorials
Stars: ✭ 2,291 (+1239.77%)
Mutual labels:  storybook
Creevey
Cross-browser screenshot testing tool for Storybook with fancy UI Runner.
Stars: ✭ 155 (-9.36%)
Mutual labels:  storybook
Vuesion
Vuesion is a boilerplate that helps product teams build faster than ever with fewer headaches and modern best practices across engineering & design.
Stars: ✭ 2,510 (+1367.84%)
Mutual labels:  storybook
Babel Plugin React Docgen
📝 Babel plugin to add react-docgen info into your code.
Stars: ✭ 156 (-8.77%)
Mutual labels:  storybook
Gatsby Wordpress Themes
🎨 Gatsby WordPress Theme
Stars: ✭ 175 (+2.34%)
Mutual labels:  storybook
Addon Smart Knobs
🧠 This Storybook plugin uses @storybook/addon-knobs but creates the knobs automatically based on PropTypes.
Stars: ✭ 215 (+25.73%)
Mutual labels:  storybook
Svelte Storybook Tailwind
A starter template for Svelte, TailwindCSS and Storybook. You can easily start your project with this template, instead of wasting time figuring out configurations for each integration.
Stars: ✭ 204 (+19.3%)
Mutual labels:  storybook
Assistant
✏️ Generate human quality code from your design. supports flutter, react, vue, figma, sketch, xd
Stars: ✭ 199 (+16.37%)
Mutual labels:  storybook

Storybook React Testing

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


The problem

You are using Storybook for you components and writing tests for them with jest, most likely alongside Enzyme or React testing library. In your Storybook stories, you already defined the scenarios of your components. You also set up the necessary decorators (theming, routing, state management, etc.) to make them all render correctly. When you're writing tests, you also end up defining scenarios of your components, as well as setting up the necessary decorators. By doing the same thing twice, you feel like you're spending too much effort, making writing and maintaining stories/tests become less like fun and more like a burden.

The solution

@storybook/testing-react is a solution to reuse your Storybook stories in your React tests. By reusing your stories in your tests, you have a catalog of component scenarios ready to be tested. All args and decorators from your story and its meta, and also global decorators, will be composed by this library and returned to you in a simple component. This way, in your unit tests, all you have to do is select which story you want to render, and all the necessary setup will be already done for you. This is the missing piece that allows for better shareability and maintenance between writing tests and writing Storybook stories.

Installation

This library should be installed as one of your project's devDependencies:

via npm

npm install --save-dev @storybook/testing-react

or via yarn

yarn add --dev @storybook/testing-react

Setup

Storybook CSF

This library requires you to be using Storybook's Component Story Format (CSF), which is the recommended way to write stories since Storybook 6.

Global config

This is an optional step. If you don't have global decorators, there's no need to do this. However, if you do, this is a necessary step for your global decorators to be applied.

If you have global decorators/parameters/etc and want them applied to your stories when testing them, you first need to set this up. You can do this by adding to or creating a jest setup file:

// setupFile.js <-- this will run before the tests in jest.
import { setGlobalConfig } from '@storybook/testing-react';
import * as globalStorybookConfig from './.storybook/preview'; // path of your preview.js file

setGlobalConfig(globalStorybookConfig);

For the setup file to be picked up, you need to pass it as an option to jest in your test command:

// package.json
{
  "test": "react-scripts test --setupFiles ./setup.js"
}

Usage

composeStories

composeStories will process all stories from the component you specify, compose args/decorators in all of them and return an object containing the composed stories.

If you use the composed story (e.g. PrimaryButton), the component will render with the args that are passed in the story. However, you are free to pass any props on top of the component, and those props will override the default values passed in the story's args.

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

// Every component that is returned maps 1:1 with the stories, but they already contain all decorators from story level, meta level and global level.
const { Primary, Secondary } = composeStories(stories);

test('renders primary button with default args', () => {
  render(<Primary />);
  const buttonElement = screen.getByText(
    /Text coming from args in stories file!/i
  );
  expect(buttonElement).not.toBeNull();
});

test('renders primary button with overriden props', () => {
  render(<Primary>Hello world</Primary>); // you can override props and they will get merged with values from the Story's args
  const buttonElement = screen.getByText(/Hello world/i);
  expect(buttonElement).not.toBeNull();
});

composeStory

You can use composeStory if you wish to apply it for a single story rather than all of your stories. You need to pass the meta (default export) as well.

import { render } from '@testing-library/react';
import { composeStory } from '@storybook/testing-react';
import Meta, { Primary as PrimaryStory } from './Button.stories';

// Returns a component that already contain all decorators from story level, meta level and global level.
const Primary = composeStory(PrimaryStory, Meta);

test('onclick handler is called', async () => {
  const onClickSpy = jest.fn();
  render(<Primary onClick={onClickSpy} />);
  const buttonElement = screen.getByRole('button');
  buttonElement.click();
  expect(onClickSpy).toHaveBeenCalled();
});

Typescript

@storybook/testing-react is typescript ready and provides autocompletion to easily detect all stories of your component:

component autocompletion

It also provides the props of the components just as you would normally expect when using them directly in your tests:

props autocompletion

Disclaimer

For the types to be automatically picked up, your stories must be typed. See an example:

import React from 'react';
import { Story, Meta } from '@storybook/react';

import { Button, ButtonProps } from './Button';

export default {
  title: 'Components/Button',
  component: Button,
} as Meta;

// Story<Props> is the key piece needed for typescript validation
const Template: Story<ButtonProps> = args => <Button {...args} />;

export const Primary = Template.bind({});
Primary.args = {
  children: 'foo',
  size: 'large',
};

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].