Reuse annotation var

This commit is contained in:
Michael Telatynski 2025-06-20 10:28:04 +01:00
parent 78bc53ef0a
commit 75cf1ee738
2 changed files with 10 additions and 3 deletions

View File

@ -19,6 +19,8 @@ import {
import { sanitizeForFilePath } from "playwright-core/lib/utils";
import { extname } from "node:path";
import { ANNOTATION } from "../stale-screenshot-reporter.js";
// Based on https://github.com/microsoft/playwright/blob/2b77ed4d7aafa85a600caa0b0d101b72c8437eeb/packages/playwright/src/util.ts#L206C8-L210C2
function sanitizeFilePathBeforeExtension(filePath: string): string {
const ext = extname(filePath);
@ -68,8 +70,7 @@ export const expect = baseExpect.extend<Expectations>({
await style?.evaluate((tag) => tag.remove());
testInfo.annotations.push({
// `_` prefix hides it from the HTML reporter
type: "_screenshot",
type: ANNOTATION,
description: testInfo.snapshotPath(screenshotName),
});

View File

@ -16,6 +16,12 @@ import path from "node:path";
import { type Reporter, type TestCase } from "@playwright/test/reporter";
import { type FullConfig } from "@playwright/test";
/**
* The annotation type used to mark screenshots in tests.
* `_` prefix hides it from the HTML reporter
*/
export const ANNOTATION = "_screenshot";
class StaleScreenshotReporter implements Reporter {
private readonly snapshotRoots = new Set<string>();
private readonly screenshots = new Set<string>();
@ -34,7 +40,7 @@ class StaleScreenshotReporter implements Reporter {
this.failing = true;
}
for (const annotation of test.annotations) {
if (annotation.type === "_screenshot" && annotation.description) {
if (annotation.type === ANNOTATION && annotation.description) {
this.screenshots.add(annotation.description);
}
}