1- // 'use strict';
1+ 'use strict' ;
22
3- // const fs = require('node:fs');
3+ const fs = require ( 'node:fs' ) ;
44
5- // const pixelThreshold = 0.2; // threshold error in one pixel
6- // const maxFailedPixels = 100; // total failed pixels
5+ const pixelThreshold = 0.2 ; // threshold error in one pixel
6+ const maxFailedPixels = 100 ; // total failed pixels
77
8- // const makePathDiff = (name) => `test/__diff__/${name}.png`;
9- // const makePathExpected = (name) => `test/__diff__/${name}__expected__.png`;
10- // const makePathActual = (name) => `test/__diff__/${name}__actual__.png`;
11- // const makePathExport = (name) => `__screenshots__/${name}.png`;
8+ const makePathDiff = ( name ) => `test/__diff__/${ name } .png` ;
9+ const makePathExpected = ( name ) => `test/__diff__/${ name } __expected__.png` ;
10+ const makePathActual = ( name ) => `test/__diff__/${ name } __actual__.png` ;
11+ const makePathExport = ( name ) => `__screenshots__/${ name } .png` ;
1212
1313
14- // const allocBuffer = (doc) => {
15- // const memSize = doc.w * doc.h * 4; // estimated number of bytes
16- // return Buffer.allocUnsafeSlow(memSize);
17- // };
14+ const allocBuffer = ( doc ) => {
15+ const memSize = doc . w * doc . h * 4 ; // estimated number of bytes
16+ return Buffer . allocUnsafeSlow ( memSize ) ;
17+ } ;
1818
19- // const getImage = (doc, Image) => {
20- // try {
21- // const storage = { data: allocBuffer(doc) };
19+ const getImage = ( doc , Image ) => {
20+ try {
21+ const storage = { data : allocBuffer ( doc ) } ;
2222
23- // doc.context.readPixels(
24- // 0, 0,
25- // doc.w, doc.h,
26- // doc.context.RGBA,
27- // doc.context.UNSIGNED_BYTE,
28- // storage
29- // );
23+ doc . context . readPixels (
24+ 0 , 0 ,
25+ doc . w , doc . h ,
26+ doc . context . RGBA ,
27+ doc . context . UNSIGNED_BYTE ,
28+ storage
29+ ) ;
3030
31- // const img = Image.fromPixels(doc.w, doc.h, 32, storage.data);
32- // return img;
33- // } catch (error) {
34- // console.error(error);
35- // return null;
36- // }
37- // };
31+ const img = Image . fromPixels ( doc . w , doc . h , 32 , storage . data ) ;
32+ return img ;
33+ } catch ( error ) {
34+ console . error ( error ) ;
35+ return null ;
36+ }
37+ } ;
3838
3939
40- // const makeScreenshot = (name, doc, Image) => {
41- // console.info(`Screenshot: ${name}`);
42- // const img = getImage(doc, Image);
43- // if (img) {
44- // img.save(makePathExport(name));
45- // console.info(`Screenshot: ${name} generated`);
46- // }
47- // };
40+ const makeScreenshot = ( name , doc , Image ) => {
41+ console . info ( `Screenshot: ${ name } ` ) ;
42+ const img = getImage ( doc , Image ) ;
43+ if ( img ) {
44+ img . save ( makePathExport ( name ) ) ;
45+ console . info ( `Screenshot: ${ name } generated` ) ;
46+ }
47+ } ;
4848
49- // const compareScreenshot = async (name, doc, Image) => {
50- // const path = makePathExport(name);
51- // if (!fs.existsSync(path)) {
52- // console.error(`Warning! No such screenshot: ${name}.`);
53- // return false;
54- // }
49+ const compareScreenshot = async ( name , doc , Image ) => {
50+ const path = makePathExport ( name ) ;
51+ if ( ! fs . existsSync ( path ) ) {
52+ console . error ( `Warning! No such screenshot: ${ name } .` ) ;
53+ return false ;
54+ }
5555
56- // const actualImage = getImage(doc, Image);
57- // if (!actualImage) {
58- // return false;
59- // }
56+ const actualImage = getImage ( doc , Image ) ;
57+ if ( ! actualImage ) {
58+ return false ;
59+ }
6060
61- // const expectedImage = await Image.loadAsync(path);
61+ const expectedImage = await Image . loadAsync ( path ) ;
6262
63- // const diff = allocBuffer(doc);
63+ const diff = allocBuffer ( doc ) ;
6464
65- // let numFailedPixels = 0;
65+ let numFailedPixels = 0 ;
6666
67- // try {
68- // const { default: pixelmatch } = await import('pixelmatch');
67+ try {
68+ const { default : pixelmatch } = await import ( 'pixelmatch' ) ;
6969
70- // numFailedPixels = pixelmatch(
71- // expectedImage.data,
72- // actualImage.data,
73- // diff,
74- // actualImage.width,
75- // actualImage.height,
76- // {
77- // threshold: pixelThreshold,
78- // alpha: 0.3,
79- // diffMask: false,
80- // diffColor: [255, 0, 0],
81- // },
82- // );
83- // } catch (error) {
84- // console.error(error);
85- // return false;
86- // }
70+ numFailedPixels = pixelmatch (
71+ expectedImage . data ,
72+ actualImage . data ,
73+ diff ,
74+ actualImage . width ,
75+ actualImage . height ,
76+ {
77+ threshold : pixelThreshold ,
78+ alpha : 0.3 ,
79+ diffMask : false ,
80+ diffColor : [ 255 , 0 , 0 ] ,
81+ } ,
82+ ) ;
83+ } catch ( error ) {
84+ console . error ( error ) ;
85+ return false ;
86+ }
8787
88- // const pathDiff = makePathDiff(name);
88+ const pathDiff = makePathDiff ( name ) ;
8989
90- // if (numFailedPixels) {
91- // console.warn(`Screenshot: ${name} - ${numFailedPixels}/${maxFailedPixels}.`);
92- // const pathExpected = makePathExpected(name);
93- // const pathActual = makePathActual(name);
94- // actualImage.save(pathActual);
95- // expectedImage.save(pathExpected);
90+ if ( numFailedPixels ) {
91+ console . warn ( `Screenshot: ${ name } - ${ numFailedPixels } /${ maxFailedPixels } .` ) ;
92+ const pathExpected = makePathExpected ( name ) ;
93+ const pathActual = makePathActual ( name ) ;
94+ actualImage . save ( pathActual ) ;
95+ expectedImage . save ( pathExpected ) ;
9696
97- // const diffImage = Image.fromPixels(doc.w, doc.h, 32, diff);
98- // diffImage.save(pathDiff);
97+ const diffImage = Image . fromPixels ( doc . w , doc . h , 32 , diff ) ;
98+ diffImage . save ( pathDiff ) ;
9999
100- // const isError = numFailedPixels >= maxFailedPixels;
101- // console[isError ? 'error' : 'warn']([
102- // `Screenshot: ${name}.`,
103- // `Failed pixels: ${numFailedPixels}/${maxFailedPixels}.`,
104- // `Diff written: ${pathDiff}.`,
105- // ].join('\n'));
100+ const isError = numFailedPixels >= maxFailedPixels ;
101+ console [ isError ? 'error' : 'warn' ] ( [
102+ `Screenshot: ${ name } .` ,
103+ `Failed pixels: ${ numFailedPixels } /${ maxFailedPixels } .` ,
104+ `Diff written: ${ pathDiff } .` ,
105+ ] . join ( '\n' ) ) ;
106106
107- // return !isError;
108- // }
107+ return ! isError ;
108+ }
109109
110- // return true;
111- // };
110+ return true ;
111+ } ;
112112
113113
114- // const screenshot = async (name, doc, Image) => {
115- // try {
116- // const path = makePathExport(name);
114+ const screenshot = async ( name , doc , Image ) => {
115+ try {
116+ const path = makePathExport ( name ) ;
117117
118- // const isCi = !!process.env['CI'];
119- // const hasFile = fs.existsSync(path);
118+ const isCi = ! ! process . env [ 'CI' ] ;
119+ const hasFile = fs . existsSync ( path ) ;
120120
121- // if (!hasFile && !isCi) {
122- // await makeScreenshot(name, doc, Image);
123- // return true;
124- // }
121+ if ( ! hasFile && ! isCi ) {
122+ await makeScreenshot ( name , doc , Image ) ;
123+ return true ;
124+ }
125125
126- // return compareScreenshot(name, doc, Image);
127- // } catch (error) {
128- // console.error(error);
129- // return false;
130- // }
131- // };
126+ return compareScreenshot ( name , doc , Image ) ;
127+ } catch ( error ) {
128+ console . error ( error ) ;
129+ return false ;
130+ }
131+ } ;
132132
133- // module.exports = { screenshot };
133+ module . exports = { screenshot } ;
0 commit comments