UNPKG

@o3r/testing

Version:

The module provides testing (e2e, unit test) utilities to help you build your own E2E pipeline integrating visual testing.

60 lines 2.71 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.VisualTestingPlaywrightReporter = void 0; const promises_1 = require("node:fs/promises"); const node_path_1 = require("node:path"); /** * Playwright reporter for visual testing */ class VisualTestingPlaywrightReporter { constructor(options) { this.outputFile = options.outputFile || (0, node_path_1.join)(process.cwd(), 'playwright-reports/visual-testing/report.json'); } /** * @inheritdoc */ printsToStdio() { return false; } /** * @inheritdoc */ onBegin(_, suite) { this.suite = suite; } /** * @inheritdoc */ async onEnd() { const fileNameExtractor = /snapshot:\s*(?<dir>(?:[\w-]+[\\/]+)*)(?<filename>[\w-]+)\.(?<extension>\w+)/i; const screenshotsToUpdate = this.suite.allTests().flatMap((test) => test.results.flatMap((result) => result.errors .reduce((acc, error) => { if (error.matcherResult?.name === 'toHaveScreenshot' && typeof error.matcherResult.actual === 'string' && typeof error.matcherResult.expected === 'string' && !error.matcherResult.pass) { // Playwright <= 1.51 acc.push({ actual: error.matcherResult.actual, expected: error.matcherResult.expected }); } else if (fileNameExtractor.test(error.message)) { // Playwright > 1.51 const { dir, filename, extension } = fileNameExtractor.exec(error.message).groups; if (filename && extension) { const attachmentMatcherActual = new RegExp(`${dir}${filename}-actual.${extension}`); const attachmentMatcherExpected = new RegExp(`${dir}${filename}-expected.${extension}`); const actual = result.attachments.find((attachment) => attachmentMatcherActual.test(attachment.name)); const expected = result.attachments.find((attachment) => attachmentMatcherExpected.test(attachment.name)); if (actual?.path && expected?.path) { acc.push({ actual: actual.path, expected: expected.path }); } } } return acc; }, []))); await (0, promises_1.mkdir)((0, node_path_1.dirname)(this.outputFile), { recursive: true }); return (0, promises_1.writeFile)(this.outputFile, JSON.stringify(screenshotsToUpdate, null, 2)); } } exports.VisualTestingPlaywrightReporter = VisualTestingPlaywrightReporter; exports.default = VisualTestingPlaywrightReporter; //# sourceMappingURL=index.js.map