11import { randomUUID } from 'node:crypto' ;
2- import { readFile } from 'node:fs/promises' ;
32import { join } from 'node:path' ;
43
54import { isLocalRun } from '../../configurator' ;
@@ -14,24 +13,18 @@ import {getFullPackConfig} from '../config';
1413import { E2edError } from '../error' ;
1514import { writeFile } from '../fs' ;
1615import { setReadonlyProperty } from '../object' ;
16+ import { getDimensionsString , getPngDimensions } from '../screenshot' ;
1717
18+ import { getEmptyAdditionalLogFields } from './getEmptyAdditionalLogFields' ;
1819import { getScreenshotMeta } from './getScreenshotMeta' ;
20+ import { writeScreenshotFromPath } from './writeScreenshotFromPath' ;
1921
20- import type { FilePathFromRoot , Selector , ToMatchScreenshotOptions , Url } from '../../types/internal' ;
22+ import type { FilePathFromRoot , Selector , ToMatchScreenshotOptions } from '../../types/internal' ;
2123
2224import type { Expect } from './Expect' ;
2325
2426import { expect as playwrightExpect } from '@playwright/test' ;
2527
26- type AdditionalLogFields = {
27- actualScreenshotId : string | undefined ;
28- actualScreenshotUrl : Url | undefined ;
29- diffScreenshotId : string | undefined ;
30- diffScreenshotUrl : Url | undefined ;
31- expectedScreenshotId : string ;
32- expectedScreenshotUrl : Url | undefined ;
33- } ;
34-
3528/**
3629 * Checks that the selector screenshot matches the one specified by `expectedScreenshotId`.
3730 * @internal
@@ -44,8 +37,7 @@ export const toMatchScreenshot = async (
4437) : Promise < void > => {
4538 const actualValue = context . actualValue as Selector ;
4639 const { description} = context ;
47- const { getScreenshotUrlById, readScreenshot, writeScreenshot} =
48- getFullPackConfig ( ) . matchScreenshot ;
40+ const { getScreenshotUrlById, readScreenshot} = getFullPackConfig ( ) . matchScreenshot ;
4941
5042 const assertId = randomUUID ( ) ;
5143 const screenshotFileName = `${ assertId } .png` ;
@@ -54,14 +46,7 @@ export const toMatchScreenshot = async (
5446 screenshotFileName ,
5547 ) as FilePathFromRoot ;
5648
57- const additionalLogFields : AdditionalLogFields = {
58- actualScreenshotId : undefined ,
59- actualScreenshotUrl : undefined ,
60- diffScreenshotId : undefined ,
61- diffScreenshotUrl : undefined ,
62- expectedScreenshotId,
63- expectedScreenshotUrl : undefined ,
64- } ;
49+ const additionalLogFields = getEmptyAdditionalLogFields ( { expectedScreenshotId} ) ;
6550
6651 setReadonlyProperty ( context , 'additionalLogFields' , additionalLogFields ) ;
6752
@@ -70,13 +55,17 @@ export const toMatchScreenshot = async (
7055 let expectedScreenshotFound = false ;
7156
7257 if ( expectedScreenshotId ) {
73- additionalLogFields . expectedScreenshotUrl = getScreenshotUrlById ( expectedScreenshotId ) ;
58+ additionalLogFields . expected . url = getScreenshotUrlById ( expectedScreenshotId ) ;
7459
7560 const expectedScreenshot = await readScreenshot ( expectedScreenshotId , meta ) ;
7661
7762 if ( expectedScreenshot !== undefined ) {
7863 expectedScreenshotFound = true ;
7964
65+ additionalLogFields . expected . dimensions = getDimensionsString (
66+ getPngDimensions ( expectedScreenshot ) ,
67+ ) ;
68+
8069 if ( ! isLocalRun ) {
8170 await writeFile ( screenshotPath , expectedScreenshot ) ;
8271 }
@@ -112,20 +101,19 @@ export const toMatchScreenshot = async (
112101 const actualScreenshotPath = join ( output , `${ assertId } -actual.png` ) as FilePathFromRoot ;
113102 const diffScreenshotPath = join ( output , `${ assertId } -diff.png` ) as FilePathFromRoot ;
114103
115- const actualScreenshot = await readFile ( actualScreenshotPath ) ;
116- const actualScreenshotId = await writeScreenshot ( actualScreenshot , meta ) ;
117-
118- additionalLogFields . actualScreenshotId = actualScreenshotId ;
119- additionalLogFields . actualScreenshotUrl = getScreenshotUrlById ( actualScreenshotId ) ;
120-
121- const diffScreenshot = await readFile ( diffScreenshotPath ) ;
122- const diffScreenshotId = await writeScreenshot ( diffScreenshot , {
123- ...meta ,
124- actual : actualScreenshotId ,
104+ const actualScreenshotId = await writeScreenshotFromPath ( {
105+ additionalLogFields,
106+ meta,
107+ path : actualScreenshotPath ,
108+ type : 'actual' ,
125109 } ) ;
126110
127- additionalLogFields . diffScreenshotId = diffScreenshotId ;
128- additionalLogFields . diffScreenshotUrl = getScreenshotUrlById ( diffScreenshotId ) ;
111+ await writeScreenshotFromPath ( {
112+ additionalLogFields,
113+ meta : { ...meta , actual : actualScreenshotId } ,
114+ path : diffScreenshotPath ,
115+ type : 'diff' ,
116+ } ) ;
129117 } catch ( secondError ) {
130118 throw new E2edError ( errorMessage , { secondError} ) ;
131119 }
@@ -138,11 +126,12 @@ export const toMatchScreenshot = async (
138126 }
139127
140128 try {
141- const actualScreenshot = await readFile ( screenshotPath ) ;
142- const actualScreenshotId = await writeScreenshot ( actualScreenshot , meta ) ;
143-
144- additionalLogFields . actualScreenshotId = actualScreenshotId ;
145- additionalLogFields . actualScreenshotUrl = getScreenshotUrlById ( actualScreenshotId ) ;
129+ await writeScreenshotFromPath ( {
130+ additionalLogFields,
131+ meta,
132+ path : screenshotPath ,
133+ type : 'actual' ,
134+ } ) ;
146135 } catch ( secondError ) {
147136 throw new E2edError ( message , { secondError} ) ;
148137 }
0 commit comments