Skip to content

Commit 216b23a

Browse files
hkfbCopilot
andauthored
test: replace story screenshots with DOM snapshots (#2819)
Fixes #2817. Part 2 of 2 — follows #2818, and is now rebased onto master. Stories that are not rendered in a webgl canvas are now verified with DOM snapshots instead of, or in addition to, image snapshots: - The storybook test-runner snapshots the `#storybook-root` inner HTML unless a story is tagged `no-dom-test`. - Webgl based stories are tagged `no-dom-test`, since their canvas content is not represented in the DOM. - Pure DOM stories are tagged `no-screenshot-test`, and their image snapshots are removed (12 PNGs). - `.storybook/test-runner.ts` is now fully typed, so its `@ts-expect-error` directives and its `tsconfig.typecheck.json` exclusion are removed. This is a port of #2619 onto current master. Stories added since that PR was written (`WellsLayerSubs`, `PolylineGroupLayer`, `GroupTreePlot.interactive`) are tagged consistently with their siblings. ### Verification Snapshots were generated with `npm run compose:storybook:test:update`. A full `npm run compose:storybook:test` run passes all 187 DOM snapshots, and a targeted re-run of the 12 DOM-snapshot suites passes 23/23 snapshots, confirming the DOM output is deterministic against the static storybook build. Note: the `WellLogViewer` / `SyncLogViewer` image snapshots differ in my local environment, and Chromium intermittently fails with `page.screenshot: Protocol error (Unable to capture screenshot)`. Both reproduce on the base branch without these changes. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 885e3a5 commit 216b23a

69 files changed

Lines changed: 9015 additions & 8 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

typescript/.storybook/test-runner.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1-
// @ts-expect-error TS7016
21
import { toMatchImageSnapshot } from "jest-image-snapshot";
32

4-
import { getStoryContext, type TestRunnerConfig } from "@storybook/test-runner";
3+
import type { Page } from "@playwright/test";
4+
import {
5+
getStoryContext,
6+
type TestContext,
7+
type TestRunnerConfig,
8+
} from "@storybook/test-runner";
59

610
// https://github.com/mapbox/pixelmatch#pixelmatchimg1-img2-output-width-height-options
711
const customDiffConfig = {};
812

9-
// @ts-expect-error TS7006
10-
const screenshotTest = async (page, context) => {
13+
const screenshotTest = async (page: Page, context: TestContext) => {
1114
let previousScreenshot: Buffer = Buffer.from("");
1215

1316
let stable = false;
@@ -27,7 +30,6 @@ const screenshotTest = async (page, context) => {
2730
}
2831
}
2932

30-
// @ts-expect-error TS2551
3133
expect(previousScreenshot).toMatchImageSnapshot({
3234
customSnapshotIdentifier: context.id,
3335
// https://www.npmjs.com/package/jest-image-snapshot/v/4.0.2#-api
@@ -38,6 +40,12 @@ const screenshotTest = async (page, context) => {
3840
});
3941
};
4042

43+
const domSnapshotTest = async (page: Page) => {
44+
const elementHandler = await page.$("#storybook-root");
45+
const innerHTML = elementHandler ? await elementHandler.innerHTML() : "";
46+
expect(innerHTML).toMatchSnapshot();
47+
};
48+
4149
const config: TestRunnerConfig = {
4250
setup() {
4351
jest.retryTimes(2);
@@ -55,6 +63,11 @@ const config: TestRunnerConfig = {
5563
if (!storyContext.tags.includes("no-screenshot-test")) {
5664
await screenshotTest(page, context);
5765
}
66+
67+
// Run DOM snapshot test unless no-dom-test is specified
68+
if (!storyContext.tags.includes("no-dom-test")) {
69+
await domSnapshotTest(page);
70+
}
5871
},
5972
};
6073

typescript/package-lock.json

Lines changed: 40 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

typescript/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
"@mui/material": "^7.3.8",
6161
"@mui/system": "^7.3.8",
6262
"@nx/jest": "^22.7.5",
63+
"@playwright/test": "^1.60.0",
6364
"@storybook/addon-docs": "^9.1.20",
6465
"@storybook/addon-links": "^9.1.20",
6566
"@storybook/addon-webpack5-compiler-babel": "^4.0.1",
@@ -75,6 +76,7 @@
7576
"@types/d3-interpolate": "^3.0.4",
7677
"@types/geojson": "^7946.0.14",
7778
"@types/jest": "^29.5.12",
79+
"@types/jest-image-snapshot": "^6.4.1",
7880
"@types/lodash": "^4.17.5",
7981
"@types/react": "^18.3.12",
8082
"@types/react-dom": "^18.3.1",

typescript/packages/group-tree-plot/src/storybook/GroupTreePlot.interactive.stories.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
const stories: Meta = {
1414
component: GroupTreePlot,
1515
title: "GroupTreePlot/Interactive Demo",
16-
tags: ["autodocs"],
16+
tags: ["autodocs", "no-dom-test"],
1717
};
1818
export default stories;
1919

typescript/packages/group-tree-plot/src/storybook/GroupTreePlot.stories.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
const stories: Meta<GroupTreePlotProps> = {
1515
component: GroupTreePlot,
1616
title: "GroupTreePlot/Demo",
17+
tags: ["no-dom-test"],
1718
argTypes: {
1819
selectedDateTime: {
1920
description:

typescript/packages/subsurface-viewer/src/storybook/components/DistanceScale.stories.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { DistanceScale } from "../../components/DistanceScale";
55
const stories: Meta = {
66
component: DistanceScale,
77
title: "SubsurfaceViewer / Components / DistanceScale",
8+
tags: ["no-screenshot-test"],
89
};
910
export default stories;
1011

typescript/packages/subsurface-viewer/src/storybook/components/InfoCard.stories.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { type LayerPickInfo } from "../../layers/utils/layerTools";
99
const stories: Meta = {
1010
component: InfoCard,
1111
title: "SubsurfaceViewer/Components/InfoCard",
12+
tags: ["no-screenshot-test"],
1213
};
1314
export default stories;
1415

0 commit comments

Comments
 (0)