Skip to content

[RFC] feat(test-runner-visual-regression): make "update" strict and add "buildCache" for more repeatable tests #1339

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

Merged
merged 2 commits into from
Mar 11, 2021
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
8 changes: 8 additions & 0 deletions .changeset/violet-gifts-turn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@web/test-runner-core": patch
"@web/test-runner-visual-regression": minor
"@web/test-runner": patch
---

Add `buildCache` option to the visual regression config to support always saving the "current" screenshot.
Make the `update` option in the visual regression config _strict_, and only save "current" shots as "baseline" when it is set to `true`.
2 changes: 1 addition & 1 deletion packages/test-runner-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@
"@types/co-body": "^5.1.0",
"@types/convert-source-map": "^1.5.1",
"@types/debounce": "^1.2.0",
"@types/istanbul-reports": "^3.0.0",
"@types/istanbul-lib-coverage": "^2.0.3",
"@types/istanbul-reports": "^3.0.0",
"@types/uuid": "^8.3.0",
"@web/browser-logs": "^0.2.1",
"@web/dev-server-core": "^0.3.6",
Expand Down
7 changes: 7 additions & 0 deletions packages/test-runner-visual-regression/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ export interface VisualRegressionPluginOptions {
* the image with the current baseline.
*/
update: boolean;
/**
* Whether to build a new cache separate from the baseline
* of the new/failed images. Useful for removed and/or
* purposely updated tests.
*/
buildCache: boolean;
/**
* The base directory to write images to.
*/
Expand Down Expand Up @@ -107,6 +113,7 @@ async function saveImage({ filePath, content }: SaveImageArgs) {

export const defaultOptions: VisualRegressionPluginOptions = {
update: false,
buildCache: false,
baseDir: 'screenshots',
diffOptions: {},

Expand Down
30 changes: 23 additions & 7 deletions packages/test-runner-visual-regression/src/visualDiffCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export async function visualDiffCommand(
name: baselineName,
});

if (!baselineImage || options.update) {
if (options.update) {
await options.saveBaseline({
filePath: resolveImagePath(baseDir, baselineName),
baseDir,
Expand All @@ -44,6 +44,25 @@ export async function visualDiffCommand(
const diffName = options.getDiffName({ browser, name });
const failedName = options.getFailedName({ browser, name });

const saveFailed = async () => {
await options.saveFailed({
filePath: resolveImagePath(baseDir, failedName),
baseDir,
name: failedName,
content: image,
});
};

if (!baselineImage) {
await saveFailed();

return {
errorMessage: 'There was no baseline image to compare against.',
diffPercentage: -1,
passed: false,
};
}

const { diffImage, diffPercentage } = await options.getImageDiff({
name,
baselineImage,
Expand All @@ -59,13 +78,10 @@ export async function visualDiffCommand(
name: diffName,
content: diffImage,
});
}

await options.saveFailed({
filePath: resolveImagePath(baseDir, failedName),
baseDir,
name: failedName,
content: image,
});
if (!passed || options.buildCache) {
await saveFailed();
}

return {
Expand Down