storybookjs / testing-react

Testing utilities that allow you to reuse your Storybook stories in your React unit tests!
MIT License
588 stars 24 forks source link

Support for Story.storyName #49

Closed synaptiko closed 2 years ago

synaptiko commented 3 years ago

Storybook allows to specify storyName but when using composeStories this information is not available anymore. I can access it if I import the story directly though.

Example:

// In stories file:
CurrentPlan.storyName = 'Current plan';
...
// In Jest test:
import * as stories from 'components/Header.stories';
import { render } from '@testing-library/react';
import { composeStories } from '@storybook/testing-react';

test.each(Object.values(composeStories(stories)).map((Story) => [Story.storyName!, Story]))(
  'renders Header for %s story',
  (_storyName, Story) => {
    const tree = render(<Story />);
    expect(tree.baseElement).toMatchSnapshot();
  }
);
yannbf commented 2 years ago

Hey @synaptiko thanks a lot for opening this issue! Incredible idea you have over there, which can be used for snapshot testing, SSR, a11y and many more examples. I just released version 1.2.0 which should support that, and I took the liberty to use your example in the README.

Thanks!