Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 25 additions & 7 deletions e2e/visual.e2e.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,34 @@
import { mkdirSync, writeFileSync } from 'node:fs';
import { expect, test, type Browser } from '@playwright/test';
import pixelmatch from 'pixelmatch';
import { PNG } from 'pngjs';
import { preparePersona, screens, type Persona } from './screens';

/**
* 시각 회귀 검사. 기준 빌드(`VISUAL_BASE_URL`, 보통 PR의 base 브랜치)와 이 빌드에서 같은 화면을
* 전체 페이지로 찍어 바이트 단위로 비교한다. 리팩터링은 화면이 한 픽셀도 바뀌면 안 된다.
* 전체 페이지로 찍어 픽셀 단위로 비교한다. 리팩터링은 화면이 바뀌면 안 된다.
* 단, 둥근 모서리 같은 곳의 안티에일리어싱은 같은 빌드끼리도 실행마다 몇 픽셀씩 달라질 수 있어
* pixelmatch의 안티에일리어싱 판별로 그런 픽셀은 차이로 세지 않는다. 크기가 다르거나
* 안티에일리어싱이 아닌 픽셀이 하나라도 다르면 실패한다.
* 디자인을 일부러 바꾸는 PR은 `visual-change` 라벨을 붙여 CI에서 이 검사를 건너뛴다.
* 다른 화면은 `test-results/visual/`에 기준(base)과 현재(head) 이미지를 남긴다.
* 다른 화면은 `test-results/visual/`에 기준(base)·현재(head)·차이(diff) 이미지를 남긴다.
*/

const baseUrl = process.env.VISUAL_BASE_URL;
const outDir = 'test-results/visual';

/** 다른 픽셀 수를 돌려준다(안티에일리어싱 제외). 크기가 다르면 Infinity. */
function countDifferentPixels(base: Buffer, head: Buffer, diffPath?: string): number {
if (base.equals(head)) return 0;
const a = PNG.sync.read(base);
const b = PNG.sync.read(head);
if (a.width !== b.width || a.height !== b.height) return Number.POSITIVE_INFINITY;
const diff = new PNG({ width: a.width, height: a.height });
const count = pixelmatch(a.data, b.data, diff.data, a.width, a.height, { threshold: 0.1, includeAA: false });
if (count > 0 && diffPath) writeFileSync(diffPath, PNG.sync.write(diff));
return count;
}

async function shoot(browser: Browser, origin: string, persona: Persona, path: string) {
const context = await browser.newContext({ viewport: { width: 1440, height: 900 }, reducedMotion: 'reduce' });
await preparePersona(context, persona, { freezeTime: true });
Expand Down Expand Up @@ -40,11 +57,12 @@ test('리팩터링 전후 화면이 픽셀 단위로 같다', async ({ browser,
const pair = () => Promise.all([shoot(browser, baseUrl!, persona, path), shoot(browser, baseURL!, persona, path)]);
let [base, head] = await pair();
// 폰트·이미지 로딩 타이밍으로 한 번 흔들릴 수 있어 다르면 다시 찍어 확인한다.
if (!base.equals(head)) [base, head] = await pair();
if (!base.equals(head)) {
different.push(`${persona} ${path}`);
const name = `${persona}${path}`.replace(/[^a-z0-9]+/gi, '_');
mkdirSync(outDir, { recursive: true });
if (countDifferentPixels(base, head) > 0) [base, head] = await pair();
const name = `${persona}${path}`.replace(/[^a-z0-9]+/gi, '_');
mkdirSync(outDir, { recursive: true });
const count = countDifferentPixels(base, head, `${outDir}/${name}.diff.png`);
if (count > 0) {
different.push(`${persona} ${path} (${count === Number.POSITIVE_INFINITY ? '크기 다름' : `${count}px`})`);
writeFileSync(`${outDir}/${name}.base.png`, base);
writeFileSync(`${outDir}/${name}.head.png`, head);
}
Expand Down
53 changes: 53 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@
"devDependencies": {
"@feature-sliced/steiger-plugin": "0.8.2",
"@playwright/test": "1.65.1",
"@types/pngjs": "6.0.5",
"@types/react": "^19.3.0",
"@types/react-dom": "^19.3.0",
"@vitejs/plugin-react": "^6.3.0",
"fake-indexeddb": "^6.2.8",
"oxlint": "1.95.0",
"pixelmatch": "8.0.0",
"pngjs": "7.0.0",
"steiger": "0.10.0",
"typescript": "^7.0.2",
"vite": "^8.3.8",
Expand Down
Loading