Skip to content
This repository was archived by the owner on Jan 31, 2024. It is now read-only.

Commit 9b987c7

Browse files
authored
Merge pull request #18 from storybookjs/fix/error-on-unsupported-format
fix: throw error on unsupported formats
2 parents 7365aef + 8c0df86 commit 9b987c7

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,22 @@ yarn add --dev @storybook/testing-react
3434

3535
### Storybook CSF
3636

37-
This library requires you to be using Storybook's [Component Story Format (CSF)](https://storybook.js.org/docs/react/api/csf), which is the recommended way to write stories since Storybook 6.
37+
This library requires you to be using Storybook's [Component Story Format (CSF)](https://storybook.js.org/docs/react/api/csf) and [hoisted CSF annotations](https://github.com/storybookjs/storybook/blob/next/MIGRATION.md#hoisted-csf-annotations), which is the recommended way to write stories since Storybook 6.
38+
39+
Essentially, if your stories look similar to this, you're good to go!
40+
41+
```jsx
42+
// CSF: default export (meta) + named exports (stories)
43+
export default {
44+
title: 'Example/Button',
45+
component: Button,
46+
};
47+
48+
const Primary = args => <Button {...args} />; // or with Template.bind({})
49+
Primary.args = {
50+
primary: true,
51+
};
52+
```
3853

3954
### Global config
4055

example/src/stories/Button.test.tsx

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import addons, { useChannel } from '@storybook/addons';
2+
import addons from '@storybook/addons';
33
import { render, screen } from '@testing-library/react';
44

55
import { composeStories, composeStory } from '../../../dist/index';
@@ -75,3 +75,30 @@ test('should pass with decorators that ne addons channel', () => {
7575
const buttonElement = screen.getByText(/Hello world/i);
7676
expect(buttonElement).not.toBeNull();
7777
});
78+
79+
describe('Unsupported formats', () => {
80+
test('should throw error StoryFn.story notation', () => {
81+
const UnsupportedStory = () => <div>hello world</div>;
82+
UnsupportedStory.story = { parameters: {} }
83+
84+
const UnsupportedStoryModule: any = {
85+
default: {},
86+
UnsupportedStory
87+
}
88+
89+
expect(() => {
90+
composeStories(UnsupportedStoryModule)
91+
}).toThrow();
92+
});
93+
94+
test('should throw error with non component stories', () => {
95+
const UnsupportedStoryModule: any = {
96+
default: {},
97+
UnsupportedStory: 123
98+
}
99+
100+
expect(() => {
101+
composeStories(UnsupportedStoryModule)
102+
}).toThrow();
103+
});
104+
})

src/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ export function composeStory<GenericArgs>(
6565
);
6666
}
6767

68+
if((story as any).story !== undefined) {
69+
throw new Error(
70+
`StoryFn.story object-style annotation is not supported. @storybook/testing-react expects hoisted CSF stories.
71+
https://github.com/storybookjs/storybook/blob/next/MIGRATION.md#hoisted-csf-annotations`
72+
);
73+
}
74+
6875
const finalStoryFn = (context: StoryContext) => {
6976
const { passArgsFirst = true } = context.parameters;
7077
if (!passArgsFirst) {

0 commit comments

Comments
 (0)