Files
nebula.js/test/rendering/testUtils.js
Tobias Åström 32d600e88b chore: convert rendering tests to es6 (#1658)
* chore: convert rendering tests to es6

* chore: fix cjs config usage

* chore: fix cjs config usage

* chore: fix verify translations

* chore: babel and jest es6

* chore: aw cjs

* chore: aw cjs

* chore: aw cjs

* chore: fix path
2024-12-30 14:40:28 +01:00

37 lines
1.3 KiB
JavaScript

import path from 'path';
import jimp from 'jimp';
export async function looksLike(fileName, capturedPath) {
const artifactsPath = path.resolve(__dirname, './__artifacts__/');
const storedPath = path.resolve(artifactsPath, 'baseline', fileName);
const stored = await jimp.read(storedPath);
const captured = await jimp.read(capturedPath);
const distance = jimp.distance(stored, captured);
const diff = jimp.diff(stored, captured);
if (distance > 0.001 || diff.percent > 0.007) {
await captured.writeAsync(path.resolve(artifactsPath, 'regression', fileName));
await diff.image.writeAsync(path.resolve(artifactsPath, 'diff', fileName));
throw new Error(`Images differ too much - distance: ${distance}, percent: ${diff.percent}`);
}
}
/**
* Utility function for ensuring that each action is awaited before executing the next one.
* @param {function[]} items An array of items (e.g. selectors) that will be sent into the action function, iteratively.
* @returns {Promise} Resolves true when done.
*/
export async function execSequence(items, action) {
const takeAction = async (index = 0) => {
if (index >= items.length) {
return true; // done
}
const nextItem = items[index];
await action(nextItem);
return takeAction(index + 1);
};
return takeAction();
}