-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: use jest & playwright testing 3d plots (#16)
* chore: use jest & playwright testing 3d plots * chore: add missing dep * chore: fix jest environment error * chore: fix jest environment error * chore: modify snapshots dirname * chore: modify snapshots dirname * chore: adjust snapshots dir * chore: adjust snapshots dir * chore: adjust snapshots dir
- Loading branch information
Showing
36 changed files
with
345 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
name: build | ||
|
||
on: [push] | ||
|
||
jobs: | ||
build: | ||
runs-on: macOS-latest | ||
defaults: | ||
run: | ||
working-directory: ./3d | ||
|
||
steps: | ||
- name: Checkout | ||
uses: actions/[email protected] | ||
|
||
- name: Setup Node.js environment | ||
uses: actions/[email protected] | ||
with: | ||
node-version: "16" | ||
|
||
- name: Install Playwright browsers | ||
run: npx playwright install --with-deps | ||
|
||
- name: Run CI | ||
run: | | ||
npm install | ||
npm run ci | ||
- name: Upload snapshots to GitHub Actions Artifacts | ||
if: ${{ failure() }} | ||
uses: actions/upload-artifact@v3 | ||
with: | ||
name: snapshots | ||
path: | | ||
3d/__tests__/e2e/snapshots/*-actual.* | ||
3d/__tests__/e2e/snapshots/*-diff.* | ||
retention-days: 1 |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,7 @@ node_modules | |
esm | ||
lib | ||
dist | ||
|
||
playwright-report | ||
*-actual.png | ||
*-diff.png |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { chromium, devices } from "playwright"; | ||
import * as tests from "../plots"; | ||
import "./utils/useSnapshotMatchers"; | ||
import { sleep } from "./utils/sleep"; | ||
|
||
describe("Charts", () => { | ||
Object.keys(tests).forEach((key) => { | ||
it(key, async () => { | ||
// Setup | ||
const browser = await chromium.launch({ | ||
args: ["--headless", "--no-sandbox"], | ||
}); | ||
const context = await browser.newContext(devices["Desktop Chrome"]); | ||
const page = await context.newPage(); | ||
|
||
await page.addInitScript(() => { | ||
window["USE_PLAYWRIGHT"] = 1; | ||
}); | ||
|
||
// Go to test page served by vite devServer. | ||
const url = `http://localhost:${globalThis.PORT}/?name=${key}`; | ||
await page.goto(url); | ||
|
||
await sleep(300); | ||
|
||
// Chart already rendered, capture into buffer. | ||
const buffer = await page.locator("canvas").screenshot(); | ||
|
||
const dir = `${__dirname}/snapshots`; | ||
try { | ||
const maxError = 0; | ||
await expect(buffer).toMatchCanvasSnapshot(dir, key, { maxError }); | ||
} finally { | ||
await context.close(); | ||
await browser.close(); | ||
} | ||
}); | ||
}); | ||
}); |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export function sleep(n: number) { | ||
return new Promise((resolve) => { | ||
setTimeout(resolve, n); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import * as fs from "fs"; | ||
import * as path from "path"; | ||
import pixelmatch from "pixelmatch"; | ||
import { PNG } from "pngjs"; | ||
|
||
export type ToMatchCanvasSnapshotOptions = { | ||
maxError?: number; | ||
}; | ||
|
||
function writePNG(buffer: Buffer, path: string) { | ||
const png = PNG.sync.read(buffer); | ||
fs.writeFileSync(path, PNG.sync.write(png)); | ||
} | ||
|
||
/** | ||
* diff between PNGs | ||
*/ | ||
function diff(src: string, target: string, diff: string, maxError = 0, showMismatchedPixels = true) { | ||
const img1 = PNG.sync.read(fs.readFileSync(src)); | ||
const img2 = PNG.sync.read(fs.readFileSync(target)); | ||
const { width, height } = img1; | ||
|
||
let diffPNG: PNG | null = null; | ||
let output: Buffer | null = null; | ||
if (showMismatchedPixels) { | ||
diffPNG = new PNG({ width, height }); | ||
output = diffPNG.data; | ||
} | ||
|
||
// @see https://github.com/mapbox/pixelmatch#pixelmatchimg1-img2-output-width-height-options | ||
const mismatch = pixelmatch(img1.data, img2.data, output, width, height, { | ||
threshold: 0.1, | ||
}); | ||
|
||
if (showMismatchedPixels && mismatch > maxError && diffPNG) { | ||
fs.writeFileSync(diff, PNG.sync.write(diffPNG)); | ||
} | ||
|
||
return mismatch; | ||
} | ||
|
||
// @see https://jestjs.io/docs/26.x/expect#expectextendmatchers | ||
export async function toMatchCanvasSnapshot( | ||
buffer: Buffer, | ||
dir: string, | ||
name: string, | ||
options: ToMatchCanvasSnapshotOptions = {}, | ||
): Promise<{ message: () => string; pass: boolean }> { | ||
const { maxError = 0 } = options; | ||
const namePath = path.join(dir, name); | ||
const actualPath = path.join(dir, `${name}-actual.png`); | ||
const expectedPath = path.join(dir, `${name}.png`); | ||
const diffPath = path.join(dir, `${name}-diff.png`); | ||
|
||
try { | ||
if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true }); | ||
if (!fs.existsSync(expectedPath)) { | ||
if (process.env.CI === "true") { | ||
throw new Error(`Please generate golden image for ${namePath}`); | ||
} | ||
console.warn(`! generate ${namePath}`); | ||
writePNG(buffer, expectedPath); | ||
return { | ||
message: () => `generate ${namePath}`, | ||
pass: true, | ||
}; | ||
} else { | ||
writePNG(buffer, actualPath); | ||
const error = diff(actualPath, expectedPath, diffPath, maxError); | ||
if (error <= maxError) { | ||
if (fs.existsSync(diffPath)) fs.unlinkSync(diffPath); | ||
fs.unlinkSync(actualPath); | ||
return { | ||
message: () => `match ${namePath}`, | ||
pass: true, | ||
}; | ||
} | ||
return { | ||
message: () => `mismatch ${namePath} (error: ${error}) `, | ||
pass: false, | ||
}; | ||
} | ||
} catch (e) { | ||
return { | ||
message: () => `${e}`, | ||
pass: false, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { toMatchCanvasSnapshot, ToMatchCanvasSnapshotOptions } from "./toMatchCanvasSnapshot"; | ||
|
||
declare global { | ||
// eslint-disable-next-line @typescript-eslint/no-namespace | ||
namespace jest { | ||
interface Matchers<R> { | ||
toMatchCanvasSnapshot(dir: string, name: string, options?: ToMatchCanvasSnapshotOptions): R; | ||
} | ||
} | ||
} | ||
|
||
expect.extend({ | ||
toMatchCanvasSnapshot, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.