-
Notifications
You must be signed in to change notification settings - Fork 0
/
testCsf.js
131 lines (115 loc) · 3.88 KB
/
testCsf.js
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import "./preview";
import { toMatchImageSnapshot } from "jest-image-snapshot";
import puppeteer from "puppeteer";
import { sanitize } from "@storybook/csf";
import { paramCase } from "change-case";
expect.extend({ toMatchImageSnapshot });
function matches(storyKey, arrayOrRegex) {
if (Array.isArray(arrayOrRegex)) {
return arrayOrRegex.includes(storyKey);
}
return storyKey.match(arrayOrRegex);
}
const noop = () => undefined;
const asyncNoop = () => Promise.resolve(undefined);
export function testCsf(
{ default: defaultExport, ...otherExports },
storyMeta
) {
if (!defaultExport) {
throw new Error("Story file not in CSF, please fix!");
}
const {
args: componentArgs,
decorators: componentDecorators = [],
excludeStories = [],
includeStories,
} = defaultExport;
let storyEntries = Object.entries(otherExports);
if (includeStories) {
storyEntries = storyEntries.filter(([storyKey]) =>
matches(storyKey, includeStories)
);
}
if (excludeStories) {
storyEntries = storyEntries.filter(
([storyKey]) => !matches(storyKey, excludeStories)
);
}
/** @type {import('@storybook/addon-storyshots-puppeteer').ImageSnapshotConfig } */
const config = {
storybookUrl: "http://localhost:6006",
chromeExecutablePath:
process.env.SB_CHROMIUM_PATH || "/usr/local/bin/chrome",
getGotoOptions: noop,
customizePage: asyncNoop,
getCustomBrowser: async function () {
// add some options "no-sandbox" to make it work properly on some Linux systems as proposed here: https://github.com/Googlechrome/puppeteer/issues/290#issuecomment-322851507
const browser = await puppeteer.launch({
args: [
"--no-sandbox ",
"--disable-setuid-sandbox",
"--disable-dev-shm-usage",
],
executablePath: config.chromeExecutablePath,
});
return browser;
},
setupTimeout: 15000,
testTimeout: 15000,
getMatchOptions: noop,
// We consider taking the full page is a reasonable default.
getScreenshotOptions: () => ({ fullPage: true, encoding: "base64" }),
beforeScreenshot: noop,
afterScreenshot: noop,
suite: "storyshots",
...global.imageSnapshotOpts,
};
describe(config.suite, () => {
const { id: firstStoryId } = storyMeta[Object.keys(storyMeta)[0]];
const [kind] = firstStoryId.split("--");
describe(kind, () => {
// Reuse browser and page for each storybook file
let browser;
let page;
beforeAll(async () => {
browser = await config.getCustomBrowser();
page = await browser.newPage();
});
afterAll(async () => {
await page.close();
await browser.close();
});
storyEntries
.filter(
([exportName]) =>
!(Array.isArray(excludeStories)
? excludeStories.includes(exportName)
: exportName.matches(excludeStories))
)
.forEach(([exportName, exported]) => {
const name = exported.story?.name || exported.storyName || exportName;
it(name, async () => {
expect.hasAssertions();
const { id } = storyMeta[exportName];
const [, story] = id.split("--");
const options = {
context: {
kind,
story,
parameters: {},
},
url: `${config.storybookUrl}iframe.html?id=${id}`,
};
await page.goto(options.url, config.getGotoOptions(options));
const element = await config.beforeScreenshot(page, options);
const image = await (element || page).screenshot(
config.getScreenshotOptions(options)
);
await config.afterScreenshot({ image, context: options.context });
expect(image).toMatchImageSnapshot(config.getMatchOptions(options));
});
});
});
});
}