|
| 1 | +/** |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + * @flow strict-local |
| 8 | + * @format |
| 9 | + */ |
| 10 | + |
| 11 | +import type {ScreenshotResult} from '../runtime/ScreenshotContext'; |
| 12 | +import type {TerminalEnvironment} from './terminalImages'; |
| 13 | + |
| 14 | +import createImageRow from './imageRow'; |
| 15 | +import saveScreenshotArtifacts from './screenshotArtifacts'; |
| 16 | +import queryImageSupport from './terminalCapabilities'; |
| 17 | +import {encodeTerminalImage, getImageProtocol} from './terminalImages'; |
| 18 | +import { |
| 19 | + DefaultReporter, |
| 20 | + SummaryReporter, |
| 21 | + VerboseReporter, |
| 22 | +} from '@jest/reporters'; |
| 23 | +import fs from 'node:fs'; |
| 24 | +import path from 'node:path'; |
| 25 | +import tty from 'node:tty'; |
| 26 | + |
| 27 | +export type TestResult = { |
| 28 | + readonly failureMessage?: ?string, |
| 29 | + readonly screenshotFailureMessage?: ?string, |
| 30 | + readonly testResults: ReadonlyArray< |
| 31 | + Readonly<{ |
| 32 | + fullName: string, |
| 33 | + status: string, |
| 34 | + screenshotResults?: ReadonlyArray<ScreenshotResult>, |
| 35 | + }>, |
| 36 | + >, |
| 37 | +}; |
| 38 | + |
| 39 | +type AggregatedResult = { |
| 40 | + readonly testResults: ReadonlyArray<TestResult>, |
| 41 | + ... |
| 42 | +}; |
| 43 | + |
| 44 | +interface TerminalOutput { |
| 45 | + readonly isTTY?: boolean; |
| 46 | + readonly columns?: number; |
| 47 | + readonly rows?: number; |
| 48 | + write(string): unknown; |
| 49 | +} |
| 50 | + |
| 51 | +export default function createScreenshotReporter(globalConfig: { |
| 52 | + readonly verbose?: boolean, |
| 53 | + ... |
| 54 | +}): DefaultReporter<TestResult, AggregatedResult> { |
| 55 | + const Reporter: Class<DefaultReporter<TestResult, AggregatedResult>> = |
| 56 | + globalConfig.verbose === true ? VerboseReporter : DefaultReporter; |
| 57 | + const summaryReporter = new SummaryReporter<AggregatedResult>(globalConfig); |
| 58 | + let terminalResults = new WeakMap<TestResult, TestResult>(); |
| 59 | + return new (class ScreenshotReporter extends Reporter { |
| 60 | + #vsCodeImages: ?Promise<boolean> = null; |
| 61 | + |
| 62 | + onRunStart(aggregatedResults: unknown, options: unknown): void { |
| 63 | + this.#vsCodeImages = null; |
| 64 | + terminalResults = new WeakMap(); |
| 65 | + super.onRunStart(aggregatedResults, options); |
| 66 | + summaryReporter.onRunStart(aggregatedResults, options); |
| 67 | + } |
| 68 | + |
| 69 | + async onTestResult( |
| 70 | + test: unknown, |
| 71 | + result: TestResult, |
| 72 | + aggregatedResults: AggregatedResult, |
| 73 | + ): Promise<void> { |
| 74 | + if (process.env.GITHUB_ACTIONS === 'true') { |
| 75 | + const errors = saveScreenshotArtifacts( |
| 76 | + result.testResults |
| 77 | + .filter(testResult => testResult.status === 'failed') |
| 78 | + .flatMap(testResult => testResult.screenshotResults ?? []), |
| 79 | + path.resolve(__dirname, '../build/screenshot-failures'), |
| 80 | + ); |
| 81 | + for (const error of errors) { |
| 82 | + console.warn(error); |
| 83 | + } |
| 84 | + } |
| 85 | + const input = process.stdin; |
| 86 | + const output = process.stderr; |
| 87 | + let terminalResult = result; |
| 88 | + if (output instanceof tty.WriteStream) { |
| 89 | + let supportsVSCodeImages = false; |
| 90 | + if ( |
| 91 | + process.env.TERM_PROGRAM === 'vscode' && |
| 92 | + input instanceof tty.ReadStream && |
| 93 | + getImageProtocol(output.isTTY, process.env, true) != null && |
| 94 | + result.testResults.some( |
| 95 | + testResult => |
| 96 | + testResult.status === 'failed' && |
| 97 | + testResult.screenshotResults?.some( |
| 98 | + screenshot => |
| 99 | + !screenshot.pass && screenshot.receivedPath != null, |
| 100 | + ), |
| 101 | + ) |
| 102 | + ) { |
| 103 | + this.#vsCodeImages ??= queryImageSupport(input, output); |
| 104 | + supportsVSCodeImages = await this.#vsCodeImages; |
| 105 | + } |
| 106 | + terminalResult = reportScreenshotPreviews( |
| 107 | + result, |
| 108 | + output, |
| 109 | + process.env, |
| 110 | + supportsVSCodeImages, |
| 111 | + ); |
| 112 | + } |
| 113 | + terminalResults.set(result, terminalResult); |
| 114 | + await super.onTestResult(test, terminalResult, aggregatedResults); |
| 115 | + } |
| 116 | + |
| 117 | + onRunComplete( |
| 118 | + testContexts: unknown, |
| 119 | + aggregatedResults: AggregatedResult, |
| 120 | + ): void { |
| 121 | + super.onRunComplete(testContexts, aggregatedResults); |
| 122 | + summaryReporter.onRunComplete(testContexts, { |
| 123 | + ...aggregatedResults, |
| 124 | + testResults: aggregatedResults.testResults.map( |
| 125 | + result => terminalResults.get(result) ?? result, |
| 126 | + ), |
| 127 | + }); |
| 128 | + } |
| 129 | + })(globalConfig); |
| 130 | +} |
| 131 | + |
| 132 | +export function reportScreenshotPreviews( |
| 133 | + result: TestResult, |
| 134 | + output: TerminalOutput, |
| 135 | + env: TerminalEnvironment, |
| 136 | + supportsVSCodeImages: boolean = false, |
| 137 | +): TestResult { |
| 138 | + const protocol = getImageProtocol(output.isTTY, env, supportsVSCodeImages); |
| 139 | + if (protocol == null) { |
| 140 | + return result; |
| 141 | + } |
| 142 | + const readErrors = new Map<string, string>(); |
| 143 | + const readImage = (file: string): ?Buffer => { |
| 144 | + try { |
| 145 | + return fs.readFileSync(file); |
| 146 | + } catch (error: unknown) { |
| 147 | + const message = error instanceof Error ? error.message : String(error); |
| 148 | + readErrors.set(file, message.replace(/\p{Cc}/gu, '')); |
| 149 | + return null; |
| 150 | + } |
| 151 | + }; |
| 152 | + const columns = |
| 153 | + output.columns != null && output.columns > 0 ? output.columns : 80; |
| 154 | + const rows = output.rows != null && output.rows > 0 ? output.rows : 30; |
| 155 | + const horizontal = columns >= 60; |
| 156 | + const preview = (png: Buffer) => |
| 157 | + encodeTerminalImage(png, protocol, { |
| 158 | + columns: columns - 2, |
| 159 | + rows: rows - 9, |
| 160 | + tmux: env.TMUX != null, |
| 161 | + }); |
| 162 | + let previews = ''; |
| 163 | + let hasPreview = false; |
| 164 | + let allPreviewsRendered = true; |
| 165 | + for (const test of result.testResults) { |
| 166 | + if (test.status !== 'failed') { |
| 167 | + continue; |
| 168 | + } |
| 169 | + for (const screenshot of test.screenshotResults ?? []) { |
| 170 | + const receivedPath = screenshot.receivedPath; |
| 171 | + if (screenshot.pass || receivedPath == null) { |
| 172 | + continue; |
| 173 | + } |
| 174 | + const panels = [ |
| 175 | + { |
| 176 | + label: 'Expected', |
| 177 | + path: screenshot.expectedPath, |
| 178 | + image: screenshot.isNew ? null : readImage(screenshot.expectedPath), |
| 179 | + unavailable: screenshot.isNew ? 'no baseline' : 'preview unavailable', |
| 180 | + required: !screenshot.isNew, |
| 181 | + }, |
| 182 | + { |
| 183 | + label: 'Received', |
| 184 | + path: receivedPath, |
| 185 | + image: readImage(receivedPath), |
| 186 | + unavailable: 'preview unavailable', |
| 187 | + required: true, |
| 188 | + }, |
| 189 | + { |
| 190 | + label: 'Difference', |
| 191 | + path: screenshot.differencePath, |
| 192 | + image: |
| 193 | + screenshot.differencePath == null |
| 194 | + ? null |
| 195 | + : readImage(screenshot.differencePath), |
| 196 | + unavailable: screenshot.isNew ? 'no baseline' : 'preview unavailable', |
| 197 | + required: !screenshot.isNew, |
| 198 | + }, |
| 199 | + ]; |
| 200 | + const title = test.fullName.replace(/\p{Cc}/gu, ''); |
| 201 | + previews += `\nScreenshot snapshot: ${title}\n`; |
| 202 | + const images = panels |
| 203 | + .map(panel => panel.image) |
| 204 | + .filter(image => image != null); |
| 205 | + const row = |
| 206 | + horizontal && images.length === panels.length |
| 207 | + ? createImageRow(images) |
| 208 | + : null; |
| 209 | + const rowPreview = row == null ? null : preview(row); |
| 210 | + if (rowPreview != null) { |
| 211 | + hasPreview = true; |
| 212 | + const labelWidth = Math.max( |
| 213 | + 8, |
| 214 | + Math.floor((rowPreview.columns - 6) / 3), |
| 215 | + ); |
| 216 | + previews += |
| 217 | + panels.map(panel => panel.label.padEnd(labelWidth)).join(' | ') + |
| 218 | + '\n'; |
| 219 | + previews += rowPreview.sequence + '\n'; |
| 220 | + for (const panel of panels) { |
| 221 | + previews += `${panel.label}: ${panel.path ?? panel.unavailable}\n`; |
| 222 | + } |
| 223 | + } else { |
| 224 | + for (const panel of panels) { |
| 225 | + previews += `${panel.label}: ${panel.path ?? panel.unavailable}\n`; |
| 226 | + const image = panel.image == null ? null : preview(panel.image); |
| 227 | + hasPreview ||= image != null; |
| 228 | + allPreviewsRendered &&= image != null || !panel.required; |
| 229 | + const readError = readErrors.get(panel.path ?? ''); |
| 230 | + previews += |
| 231 | + (image?.sequence ?? |
| 232 | + (readError == null |
| 233 | + ? panel.unavailable |
| 234 | + : `preview unavailable: ${readError}`)) + '\n'; |
| 235 | + } |
| 236 | + } |
| 237 | + } |
| 238 | + } |
| 239 | + if (previews === '') { |
| 240 | + return result; |
| 241 | + } |
| 242 | + try { |
| 243 | + output.write(previews); |
| 244 | + } catch { |
| 245 | + return result; |
| 246 | + } |
| 247 | + if ( |
| 248 | + hasPreview && |
| 249 | + allPreviewsRendered && |
| 250 | + result.screenshotFailureMessage != null |
| 251 | + ) { |
| 252 | + return {...result, failureMessage: result.screenshotFailureMessage}; |
| 253 | + } |
| 254 | + return result; |
| 255 | +} |
0 commit comments