-
Notifications
You must be signed in to change notification settings - Fork 84
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add docs about how to use Ladle and Playwright for local visual snapshot testing #94
Comments
WhatHi, we have achieved that (with CI support) in our project. What we have is visual regression from screenshots, automatically generated from How to
import { test as base } from "@playwright/test";
import got from "got";
interface StoriesMeta {
stories: {
[key: string]: {
name: string;
levels: string[];
};
};
}
const LADLE_URL = "http://localhost:61000" as const;
export const test = base.extend<{ storiesMeta: StoriesMeta; ladleUrl: string }>(
{
// had to, cannot do (_, use) as first argument has to be destructured (not
// sure why though)
/* eslint-disable-next-line @typescript-eslint/no-unused-vars */
storiesMeta: async ({ page: _ }, use) => {
const storiesMeta = await got(`${LADLE_URL}/meta.json`)
.json()
.catch(() => {
/* eslint-disable-next-line no-console */
console.error("Unable to setup stories meta");
});
use(storiesMeta as StoriesMeta).catch(() => {
/* eslint-disable-next-line no-console */
console.error("Unable to setup stories meta");
});
},
ladleUrl: LADLE_URL,
}
);
export { expect } from "@playwright/test";
// importing via ".js" extension due to convoluted setup.
// only required in playwright tests!
import { expect, test } from "./test.js";
test("visual regression of Ladle stories", async ({
page,
storiesMeta,
ladleUrl,
}) => {
for (const storySlug of Object.keys(storiesMeta.stories)) {
const story = storiesMeta.stories[storySlug];
await test.step(story.name, async () => {
// console.log(story, storySlug);
await page.goto(`${ladleUrl}/?mode=preview&story=${storySlug}`);
await page.waitForSelector("[data-storyloaded]");
await expect(page).toHaveScreenshot(`${storySlug}.png`);
});
}
});
// scripts in package.json
{
"scripts": {
"e2e:dev": "playwright test -c src/playwright",
"e2e:ci": "start-server-and-test 'pnpm ladle dev' http://localhost:61000 'pnpm e2e:dev'"
}
} # we use GitLab CI
stories visual regression tests:
image: mcr.microsoft.com/playwright:v1.23.4-focal
stage: test
script:
- pnpm e2e:ci
needs:
- linting
artifacts:
when: always
paths:
- test-results/**
expire_in: 2 days |
Nice, I have something similar WIP here: #178 Just need to make some updates and write an article about it. |
No description provided.
The text was updated successfully, but these errors were encountered: