generated from storybookjs/addon-kit
-
Notifications
You must be signed in to change notification settings - Fork 63
/
test-runner.ts
49 lines (41 loc) 路 1.4 KB
/
test-runner.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import { toMatchImageSnapshot } from 'jest-image-snapshot';
import { getStoryContext, waitForPageReady } from '../dist/playwright/hooks';
import type { TestRunnerConfig } from '../dist';
const snapshotsDir = process.env.SNAPSHOTS_DIR || '__snapshots__';
const customSnapshotsDir = `${process.cwd()}/${snapshotsDir}`;
const skipSnapshots = process.env.SKIP_SNAPSHOTS === 'true';
const config: TestRunnerConfig = {
logLevel: 'verbose',
tags: {
exclude: ['exclude'],
include: [],
skip: ['skip'],
},
setup() {
expect.extend({ toMatchImageSnapshot });
},
async postVisit(page, context) {
// Get entire context of a story, including parameters, args, argTypes, etc.
const { parameters } = await getStoryContext(page, context);
if (parameters?.tests?.disableSnapshots) {
return;
}
if (skipSnapshots) {
return;
}
await waitForPageReady(page);
// Visual snapshot tests
const image = await page.screenshot({ fullPage: true });
expect(image).toMatchImageSnapshot({
customSnapshotsDir,
customSnapshotIdentifier: context.id,
failureThreshold: 0.03,
failureThresholdType: 'percent',
});
const elementHandler = (await page.$('#root')) || (await page.$('#storybook-root'));
const innerHTML = await elementHandler?.innerHTML();
// HTML snapshot tests
expect(innerHTML).toMatchSnapshot();
},
};
export default config;