mirror of
https://github.com/storybookjs/storybook.git
synced 2025-04-08 11:11:53 +08:00
63 lines
2.1 KiB
Plaintext
63 lines
2.1 KiB
Plaintext
```js
|
|
// storybook.test.js
|
|
// @vitest-environment jsdom
|
|
|
|
import path from 'path';
|
|
import { describe, expect, test } from 'vitest';
|
|
|
|
// Replace your-testing-library with one of the supported testing libraries (e.g., react, vue)
|
|
import { render } from '@testing-library/your-testing-library';
|
|
|
|
// Adjust the import based on the supported framework or Storybook's testing libraries (e.g., react, vue3)
|
|
import { composeStories } from '@storybook/your-framework';
|
|
|
|
const compose = (entry) => {
|
|
try {
|
|
return composeStories(entry);
|
|
} catch (error) {
|
|
throw new Error(
|
|
`There was an issue composing stories for the module: ${JSON.stringify(entry)}, ${error}`,
|
|
);
|
|
}
|
|
};
|
|
function getAllStoryFiles() {
|
|
// Place the glob you want to match your story files
|
|
const storyFiles = Object.entries(
|
|
import.meta.glob('./stories/**/*.(stories|story).@(js|jsx|mjs|ts|tsx)', {
|
|
eager: true,
|
|
}),
|
|
);
|
|
|
|
return storyFiles.map(([filePath, storyFile]) => {
|
|
const storyDir = path.dirname(filePath);
|
|
const componentName = path.basename(filePath).replace(/\.(stories|story)\.[^/.]+$/, '');
|
|
return { filePath, storyFile, componentName, storyDir };
|
|
});
|
|
}
|
|
describe('Stories Snapshots', () => {
|
|
getAllStoryFiles().forEach(({ storyFile, componentName }) => {
|
|
const meta = storyFile.default;
|
|
const title = meta.title || componentName;
|
|
|
|
describe(title, () => {
|
|
const stories = Object.entries(compose(storyFile)).map(([name, story]) => ({ name, story }));
|
|
|
|
if (stories.length <= 0) {
|
|
throw new Error(
|
|
`No stories found for this module: ${title}. Make sure there is at least one valid story for this module.`,
|
|
);
|
|
}
|
|
|
|
stories.forEach(({ name, story }) => {
|
|
test(name, async () => {
|
|
const mounted = render(story());
|
|
// Ensures a consistent snapshot by waiting for the component to render by adding a delay of 1 ms before taking the snapshot.
|
|
await new Promise((resolve) => setTimeout(resolve, 1));
|
|
expect(mounted.container).toMatchSnapshot();
|
|
});
|
|
});
|
|
});
|
|
});
|
|
});
|
|
```
|