/
snapper.int.js
37 lines (31 loc) · 1.33 KB
/
snapper.int.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
const path = require('path');
const jimp = require('jimp');
const differ = () => {
const artifacts = path.resolve(__dirname, '../__artifacts__/');
return {
async looksLike(name, captured) {
const file = `${path.basename(__dirname)}-${name}`;
const stored = await jimp.read(path.resolve(artifacts, 'baseline', file));
const distance = jimp.distance(stored, captured);
const diff = jimp.diff(stored, captured);
if (distance > 0.001 || diff.percent > 0.001) {
await captured.writeAsync(path.resolve(artifacts, 'regression', file));
throw new Error(`Images differ too much - distance: ${distance}, percent: ${diff.percent}`);
}
},
};
};
const d = differ();
describe('snapper', () => {
const object = '[data-type="bar"]';
const selector = `${object} .nebulajs-sn`;
it('should capture an image of a bar', async () => {
await page.goto(`${process.env.BASE_URL}/snaps/snapper.html`);
await page.waitForSelector(selector, { visible: true });
await page.click(selector);
await page.waitForSelector(`${object}[data-captured]`, { visible: true });
const imgSrc = await page.$eval(`${object}[data-captured]`, el => el.getAttribute('data-captured'));
const captured = await jimp.read(`${process.env.BASE_URL}${imgSrc}`);
await d.looksLike('bar.png', captured);
});
});