Skip to content

Commit

Permalink
How about using test and assert natively from Node.js? (#126)
Browse files Browse the repository at this point in the history
* How about using test and assert natively from Node.js?

* Forget about engines
  • Loading branch information
paazmaya authored Jul 13, 2023
1 parent 0a1e1b3 commit cfed207
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 683 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
},
"devDependencies": {
"eslint": "^8.40.0",
"eslint-config-mourner": "^3.0.0",
"tape": "^5.6.3"
"eslint-config-mourner": "^3.0.0"
},
"scripts": {
"pretest": "eslint index.js bin/pixelmatch test/test.js",
Expand Down
37 changes: 17 additions & 20 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
'use strict';

const assert = require('node:assert/strict');
const test = require('node:test');
const fs = require('node:fs');
const path = require('node:path');

const PNG = require('pngjs').PNG;
const fs = require('fs');
const test = require('tape').test;
const path = require('path');
const match = require('../.');

const options = {threshold: 0.05};
Expand All @@ -24,30 +26,27 @@ diffTest('6a', '6b', '6diff', options, 51);
diffTest('6a', '6a', '6empty', {threshold: 0}, 0);
diffTest('7a', '7b', '7diff', {diffColorAlt: [0, 255, 0]}, 2448);

test('throws error if image sizes do not match', (t) => {
t.throws(() => match(new Uint8Array(8), new Uint8Array(9), null, 2, 1), 'Image sizes do not match');
t.end();
test('throws error if image sizes do not match', () => {
assert.throws(() => match(new Uint8Array(8), new Uint8Array(9), null, 2, 1), 'Image sizes do not match');
});

test('throws error if image sizes do not match width and height', (t) => {
t.throws(() => match(new Uint8Array(9), new Uint8Array(9), null, 2, 1), 'Image data size does not match width/height');
t.end();
test('throws error if image sizes do not match width and height', () => {
assert.throws(() => match(new Uint8Array(9), new Uint8Array(9), null, 2, 1), 'Image data size does not match width/height');
});

test('throws error if provided wrong image data format', (t) => {
test('throws error if provided wrong image data format', () => {
const err = 'Image data: Uint8Array, Uint8ClampedArray or Buffer expected';
const arr = new Uint8Array(4 * 20 * 20);
const bad = new Array(arr.length).fill(0);
t.throws(() => match(bad, arr, null, 20, 20), err);
t.throws(() => match(arr, bad, null, 20, 20), err);
t.throws(() => match(arr, arr, bad, 20, 20), err);
t.end();
assert.throws(() => match(bad, arr, null, 20, 20), err);
assert.throws(() => match(arr, bad, null, 20, 20), err);
assert.throws(() => match(arr, arr, bad, 20, 20), err);
});

function diffTest(imgPath1, imgPath2, diffPath, options, expectedMismatch) {
const name = `comparing ${imgPath1} to ${imgPath2}, ${JSON.stringify(options)}`;

test(name, (t) => {
test(name, () => {
const img1 = readImage(imgPath1);
const img2 = readImage(imgPath2);
const {width, height} = img1;
Expand All @@ -60,12 +59,10 @@ function diffTest(imgPath1, imgPath2, diffPath, options, expectedMismatch) {
writeImage(diffPath, diff);
} else {
const expectedDiff = readImage(diffPath);
t.ok(diff.data.equals(expectedDiff.data), 'diff image');
assert.ok(diff.data.equals(expectedDiff.data), 'diff image');
}
t.equal(mismatch, expectedMismatch, 'number of mismatched pixels');
t.equal(mismatch, mismatch2, 'number of mismatched pixels without diff');

t.end();
assert.equal(mismatch, expectedMismatch, 'number of mismatched pixels');
assert.equal(mismatch, mismatch2, 'number of mismatched pixels without diff');
});
}

Expand Down
Loading

0 comments on commit cfed207

Please sign in to comment.