Skip to content

Commit 03125d1

Browse files
committed
refactor: use fixed regression pixel allowance
1 parent 29a85e0 commit 03125d1

2 files changed

Lines changed: 7 additions & 26 deletions

File tree

test/regression/compare.js

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -91,21 +91,6 @@ async function stopODiffServer(server) {
9191
}
9292
}
9393

94-
/**
95-
* @param {string} file
96-
* @returns {Promise<number>}
97-
*/
98-
async function readPngWidth(file) {
99-
const handle = await fs.open(file);
100-
try {
101-
const header = Buffer.alloc(24);
102-
await handle.read(header, 0, header.length, 0);
103-
return header.readUInt32BE(16);
104-
} finally {
105-
await handle.close();
106-
}
107-
}
108-
10994
/**
11095
* @param {ReadonlyArray<string>} list
11196
* @param {{ workerCount?: number }=} options
@@ -181,14 +166,13 @@ export async function renderScreenshots(list, options = {}) {
181166

182167
/**
183168
* @param {ReadonlyArray<string>} list
184-
* @param {{ ODiffServer?: ODiffServerConstructor, readWidth?: (file: string) => Promise<number> }=} options
169+
* @param {{ ODiffServer?: ODiffServerConstructor }=} options
185170
* @returns {Promise<MatchResult[]>}
186171
*/
187172
export async function compareScreenshots(list, options = {}) {
188173
/** @type {MatchResult[]} */
189174
const results = [];
190175
const ODiffServerClass = options.ODiffServer ?? ODiffServer;
191-
const readWidth = options.readWidth ?? readPngWidth;
192176
const server = new ODiffServerClass();
193177
try {
194178
for (const name of list) {
@@ -215,9 +199,7 @@ export async function compareScreenshots(list, options = {}) {
215199
);
216200
let isMatch = result.match;
217201
if (!result.match && result.reason === 'pixel-diff') {
218-
const width = await readWidth(originalPath);
219-
const allowance = width <= 16 ? 3 : 4;
220-
isMatch = result.diffCount <= allowance;
202+
isMatch = result.diffCount <= 4;
221203
if (isMatch && process.env.NO_DIFF == null) {
222204
await fs.rm(diffPath, { force: true });
223205
}

test/regression/compare.test.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ describe('withCleanup', () => {
3333
});
3434

3535
describe('compareScreenshots', () => {
36-
test('preserves the width-dependent pixel allowance', async () => {
36+
test('allows up to four differing pixels', async () => {
3737
const odiffResults = /** @type {import('odiff-bin').ODiffResult[]} */ ([
3838
{ match: true },
3939
{
4040
match: false,
4141
reason: 'pixel-diff',
42-
diffCount: 3,
42+
diffCount: 4,
4343
diffPercentage: 1,
4444
},
4545
{
@@ -68,16 +68,15 @@ describe('compareScreenshots', () => {
6868

6969
await expect(
7070
compareScreenshots(
71-
['exact.svg', 'small.svg', 'wide.svg', 'changed.svg', 'layout.svg'],
71+
['exact.svg', 'first.svg', 'second.svg', 'changed.svg', 'layout.svg'],
7272
{
7373
ODiffServer: FakeODiffServer,
74-
readWidth: async (file) => (file.endsWith('small.svg.png') ? 16 : 17),
7574
},
7675
),
7776
).resolves.toEqual([
7877
{ name: 'exact.svg', isMatch: true },
79-
{ name: 'small.svg', isMatch: true },
80-
{ name: 'wide.svg', isMatch: true },
78+
{ name: 'first.svg', isMatch: true },
79+
{ name: 'second.svg', isMatch: true },
8180
{ name: 'changed.svg', isMatch: false },
8281
{ name: 'layout.svg', isMatch: false },
8382
]);

0 commit comments

Comments
 (0)