Skip to content
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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(core): add visual regression testing #962

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
feat: yarn screenshot
etowahadams committed Sep 25, 2023
commit 020852f4a0b20691d3d24d09efce3f02f6b72ea9
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import puppeteer, { Page, Browser } from 'puppeteer';
import { examples } from '../editor/example';
import { examples } from '../../editor/example';
import * as fs from 'fs';

import { beforeAll } from 'vitest';
@@ -66,7 +66,7 @@ beforeAll(async () => {
* Loop over all examples and take a screenshot
*/
Object.entries(examples)
// .filter(([name]) => name === 'ALIGNMENT') // we only want to see the broken example now
// .filter(([name]) => name === 'doc_text') // we only want to see the broken example now
.forEach(([name, example]) => {
test(
name,
@@ -78,7 +78,7 @@ Object.entries(examples)
const component = await page.waitForSelector('.gosling-component');
await page.waitForNetworkIdle({ idleTime: 2000 });
await delay(2000); // wait 2 seconds for rendering to complete. TODO: see if we can implement javascript API subscription which fires when rendering is done
await component!.screenshot({ path: `img/visual-regression/${name}.png` });
await component!.screenshot({ path: `img/visual-regression/screenshots/${name}.png` });
},
20000
);
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -29,6 +29,7 @@
"preview": "vite preview",
"check": "tsc --noEmit",
"test": "vitest",
"image": "vitest --mode image",
"coverage": "vitest run --coverage",
"format": "eslint src/ editor/ --fix && prettier 'editor/**/*.css' --write",
"schema": "node scripts/generate-schemas.mjs",
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -42,7 +42,7 @@
"editor/index.tsx",
"embed/index.ts"
],
"include": ["src", "src/**/*.d.ts", "editor", "img/visual-regression.test.ts"],
"include": ["src", "src/**/*.d.ts", "editor", "img/visual-regression/visual-regression.test.ts"],
"exclude": [
"node_modules",
"public",
25 changes: 25 additions & 0 deletions vite.config.js
Original file line number Diff line number Diff line change
@@ -73,6 +73,9 @@ const external = [...Object.keys(pkg.dependencies), ...Object.keys(pkg.peerDepen
dep => !skipExt.has(dep)
);

/**
* Used when yarn build-lib is run
*/
const esm = defineConfig({
build: {
emptyOutDir: false,
@@ -101,9 +104,13 @@ const dev = defineConfig({
plugins: [bundleWebWorker, manualInlineWorker]
});

/**
* This config is used when vitest is run
*/
const testing = defineConfig({
resolve: { alias },
test: {
exclude: ['./img/visual-regression/*'],
globals: true,
setupFiles: [path.resolve(__dirname, './scripts/setup-vitest.js')],
environment: 'jsdom',
@@ -121,9 +128,27 @@ const testing = defineConfig({
}
});

/**
* This config is used to take screenshots of every example Gosling spec using a headless browser (puppeteer)
*/
const image = defineConfig({
test: {
include: ['./img/visual-regression/*'],
globals: true,
environment: 'jsdom',
threads: false,
environmentOptions: {
jsdom: {
resources: 'usable'
}
}
}
});

export default ({ command, mode }) => {
if (command === 'build' && mode === 'lib') return esm;
if (mode == 'test') return testing;
if (mode == 'image') return image;
if (mode === 'editor') {
dev.plugins.push(reactRefresh());
}