Skip to content

Commit 395c605

Browse files
feat(test-runner-visual-regression): add testName to GetNameArgs (#1657)
1 parent e56d2db commit 395c605

File tree

5 files changed

+29
-8
lines changed

5 files changed

+29
-8
lines changed

.changeset/gold-needles-jump.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@web/test-runner-visual-regression': patch
3+
---
4+
5+
Supply testFile to extensibility points in visualDiffPlugin, making it easy to store snapshot images alongside test files

packages/test-runner-visual-regression/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ type PixelMatchOptions = PixelMatchParams[5];
7676
export interface GetNameArgs {
7777
browser: string;
7878
name: string;
79+
testFile: string;
7980
}
8081

8182
export interface ImageArgs {

packages/test-runner-visual-regression/src/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ type PixelMatchOptions = PixelMatchParams[5];
1111
export interface GetNameArgs {
1212
browser: string;
1313
name: string;
14+
testFile: string;
1415
}
1516

1617
export interface ImageArgs {

packages/test-runner-visual-regression/src/visualDiffCommand.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,19 @@ export interface VisualDiffCommandResult {
1717
passed: boolean;
1818
}
1919

20+
export interface VisualDiffCommandContext {
21+
browser: string;
22+
testFile: string;
23+
}
24+
2025
export async function visualDiffCommand(
2126
options: VisualRegressionPluginOptions,
2227
image: Buffer,
23-
browser: string,
2428
name: string,
29+
{ browser, testFile }: VisualDiffCommandContext,
2530
): Promise<VisualDiffCommandResult> {
2631
const baseDir = path.resolve(options.baseDir);
27-
const baselineName = options.getBaselineName({ browser, name });
32+
const baselineName = options.getBaselineName({ browser, name, testFile });
2833

2934
const baselineImage = await options.getBaseline({
3035
filePath: resolveImagePath(baseDir, baselineName),
@@ -42,8 +47,8 @@ export async function visualDiffCommand(
4247
return { diffPercentage: -1, passed: true };
4348
}
4449

45-
const diffName = options.getDiffName({ browser, name });
46-
const failedName = options.getFailedName({ browser, name });
50+
const diffName = options.getDiffName({ browser, name, testFile });
51+
const failedName = options.getFailedName({ browser, name, testFile });
4752
const diffFilePath = resolveImagePath(baseDir, diffName);
4853

4954
const saveFailed = async () => {

packages/test-runner-visual-regression/src/visualRegressionPlugin.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ import type { PlaywrightLauncher } from '@web/test-runner-playwright';
44
import type { WebdriverLauncher } from '@web/test-runner-webdriver';
55

66
import { defaultOptions, VisualRegressionPluginOptions } from './config';
7-
import { visualDiffCommand, VisualDiffCommandResult } from './visualDiffCommand';
7+
import {
8+
visualDiffCommand,
9+
VisualDiffCommandContext,
10+
VisualDiffCommandResult,
11+
} from './visualDiffCommand';
812
import { VisualRegressionError } from './VisualRegressionError';
913

1014
interface Payload {
@@ -49,6 +53,11 @@ export function visualRegressionPlugin(
4953
return;
5054
}
5155

56+
const context: VisualDiffCommandContext = {
57+
testFile: session.testFile,
58+
browser: session.browser.name,
59+
};
60+
5261
if (session.browser.type === 'puppeteer') {
5362
const browser = session.browser as ChromeLauncher;
5463
const page = browser.getPage(session.id);
@@ -67,7 +76,7 @@ export function visualRegressionPlugin(
6776
}
6877

6978
const screenshot = (await element.screenshot({ encoding: 'binary' })) as Buffer;
70-
return visualDiffCommand(mergedOptions, screenshot, session.browser.name, payload.name);
79+
return visualDiffCommand(mergedOptions, screenshot, payload.name, context);
7180
}
7281

7382
if (session.browser.type === 'playwright') {
@@ -88,7 +97,7 @@ export function visualRegressionPlugin(
8897
}
8998

9099
const screenshot = await element.screenshot();
91-
return visualDiffCommand(mergedOptions, screenshot, session.browser.name, payload.name);
100+
return visualDiffCommand(mergedOptions, screenshot, payload.name, context);
92101
}
93102

94103
if (session.browser.type === 'webdriver') {
@@ -106,7 +115,7 @@ export function visualRegressionPlugin(
106115
`;
107116

108117
const screenshot = await browser.takeScreenshot(session.id, locator);
109-
return visualDiffCommand(mergedOptions, screenshot, session.browser.name, payload.name);
118+
return visualDiffCommand(mergedOptions, screenshot, payload.name, context);
110119
}
111120

112121
throw new Error(

0 commit comments

Comments
 (0)