-
Notifications
You must be signed in to change notification settings - Fork 475
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore(demo-cypress): screenshot testing for component tests #9290
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
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, | ||
}, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
"private": true, | ||
"devDependencies": { | ||
"@nx/cypress": "19.8.2", | ||
"cypress": "13.15.0" | ||
"cypress": "13.15.0", | ||
"cypress-image-diff-js": "2.2.1" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. According to Cypress documentation:
cypress-image-diff has active support for the last 4 years |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
class="fade" | ||
> | ||
<div tuiFade>Very long value in chip</div> | ||
<div>{{ 123435 | tuiAmount: 'RUB' | async }}</div> | ||
<div>{{ 123456 | tuiAmount: 'RUB' | async }}</div> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just to debug that Playwright screenshot testing still properly works in CI There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
</tui-chip> | ||
|
||
<tui-chip | ||
|
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; | ||
} | ||
} |
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'); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
7.5.0
is not compatible with"moduleResolution": "bundler"