mirror of
https://github.com/storybookjs/storybook.git
synced 2025-04-08 10:41:49 +08:00
54 lines
1.5 KiB
Markdown
54 lines
1.5 KiB
Markdown
```js filename=".storybook/test-runner.js" renderer="common" language="js"
|
|
const { waitForPageReady } = require('@storybook/test-runner');
|
|
|
|
const { toMatchImageSnapshot } = require('jest-image-snapshot');
|
|
|
|
const customSnapshotsDir = `${process.cwd()}/__snapshots__`;
|
|
|
|
module.exports = {
|
|
setup() {
|
|
expect.extend({ toMatchImageSnapshot });
|
|
},
|
|
async postVisit(page, context) {
|
|
// Awaits for the page to be loaded and available including assets (e.g., fonts)
|
|
await waitForPageReady(page);
|
|
|
|
// Generates a snapshot file based on the story identifier
|
|
const image = await page.screenshot();
|
|
expect(image).toMatchImageSnapshot({
|
|
customSnapshotsDir,
|
|
customSnapshotIdentifier: context.id,
|
|
});
|
|
},
|
|
};
|
|
```
|
|
|
|
```ts filename=".storybook/test-runner.ts" renderer="common" language="ts"
|
|
import type { TestRunnerConfig } from '@storybook/test-runner';
|
|
|
|
import { waitForPageReady } from '@storybook/test-runner';
|
|
|
|
import { toMatchImageSnapshot } from 'jest-image-snapshot';
|
|
|
|
const customSnapshotsDir = `${process.cwd()}/__snapshots__`;
|
|
|
|
const config: TestRunnerConfig = {
|
|
setup() {
|
|
expect.extend({ toMatchImageSnapshot });
|
|
},
|
|
async postVisit(page, context) {
|
|
// Awaits for the page to be loaded and available including assets (e.g., fonts)
|
|
await waitForPageReady(page);
|
|
|
|
// Generates a snapshot file based on the story identifier
|
|
const image = await page.screenshot();
|
|
expect(image).toMatchImageSnapshot({
|
|
customSnapshotsDir,
|
|
customSnapshotIdentifier: context.id,
|
|
});
|
|
},
|
|
};
|
|
|
|
export default config;
|
|
```
|