-
Notifications
You must be signed in to change notification settings - Fork 471
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(demo-cypress): screenshot testing for component tests (#9290)
- Loading branch information
1 parent
8da053b
commit a965dd5
Showing
17 changed files
with
241 additions
and
166 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module.exports = { | ||
ROOT_DIR: 'tests-results', | ||
SCREENSHOTS_DIR: 'snapshots', | ||
REPORT_DIR: '.', | ||
JSON_REPORT: { | ||
FILENAME: 'report-summary', | ||
OVERWRITE: true, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
scripts/visual-testing/combine-cypress-failed-screenshots.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import {readFileSync, writeFileSync} from 'node:fs'; | ||
|
||
import {combineSnapshots} from './combine-snapshots'; | ||
|
||
const ROOT_PATH = 'projects/demo-cypress'; | ||
const TEST_RESULTS_PATH = `${ROOT_PATH}/tests-results`; | ||
|
||
interface TestResult { | ||
status: 'fail' | 'pass'; | ||
name: string; | ||
baselinePath: string; | ||
diffPath: string; | ||
comparisonPath: string; | ||
} | ||
|
||
interface Report { | ||
suites: Array<{tests: TestResult[]}>; | ||
} | ||
|
||
(async function combineCypressFailedScreenshots(): Promise<void> { | ||
const reportSummary = readJSON<Report>(`${TEST_RESULTS_PATH}/report-summary.json`); | ||
|
||
if (!reportSummary) { | ||
return; | ||
} | ||
|
||
const failedTestSnapshots = reportSummary.suites | ||
.map((x) => x.tests) | ||
.flat() | ||
.filter((x) => x.status === 'fail' && x.diffPath); | ||
|
||
for (const {baselinePath, diffPath, comparisonPath, name} of failedTestSnapshots) { | ||
const buffer = await combineSnapshots( | ||
[baselinePath, diffPath, comparisonPath].map((x) => `${ROOT_PATH}/${x}`), | ||
); | ||
|
||
writeFileSync(`${TEST_RESULTS_PATH}/${name}.diff.png`, buffer); | ||
} | ||
})(); | ||
|
||
function readJSON<T>(path: string): T | null { | ||
try { | ||
return JSON.parse(readFileSync(path, 'utf-8')); | ||
} catch { | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// @ts-nocheck It is used in CI only! | ||
/** | ||
* Canvas has difficult installation guide for ARM CPU, including an Apple M1 or M2 | ||
* (not friendly for our external contributors). | ||
* https://github.com/Automattic/node-canvas/issues/1511 | ||
*/ | ||
import {createCanvas, loadImage, version} from 'canvas'; | ||
|
||
export async function combineSnapshots( | ||
imagesPaths: string[], | ||
): Promise<NodeJS.ArrayBufferView> { | ||
console.info('canvas:', version); | ||
|
||
const images = await Promise.all(imagesPaths.map(loadImage)); | ||
const totalWidth = images.reduce((acc: number, {width}) => acc + width, 0); | ||
const maxHeight = Math.max(...images.map(({height}) => height)); | ||
const canvas = createCanvas(totalWidth, maxHeight); | ||
const ctx = canvas.getContext('2d'); | ||
|
||
let prevWidth = 0; | ||
|
||
images.forEach((image) => { | ||
ctx.drawImage(image, prevWidth, 0); | ||
prevWidth += image.width; | ||
}); | ||
|
||
return canvas.toBuffer('image/png'); | ||
} |