Skip to content

Commit 2ed08b4

Browse files
committed
test looks-same
1 parent 5891deb commit 2ed08b4

File tree

3 files changed

+63
-10
lines changed

3 files changed

+63
-10
lines changed

packages/devextreme-cli/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
"eslint-stylish": "^0.2.0",
6767
"jest": "^29.7.0",
6868
"jest-image-snapshot": "^6.4.0",
69+
"looks-same": "^9.0.1",
6970
"prettier": "^2.8.8",
7071
"rimraf": "^2.7.1",
7172
"tree-kill": "^1.2.2",

packages/devextreme-cli/testing/app-template.test.shared.js

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const path = require('path');
22
const waitOn = require('wait-on');
33
const ip = require('ip');
4-
4+
const { compareImages } = require('./utils/imageComparator');
55
const getBrowser = require('./utils/puppeteer').getBrowser;
66
const { viewports, themes, layouts } = require('./constants');
77
const DevServer = require('./dev-server');
@@ -94,12 +94,19 @@ module.exports = (env, { port = 8080, urls = {} } = {}) => {
9494
const customConfig = { threshold: 0.012, includeAA: true };
9595

9696
function compareSnapshot(image, name, overrideConfig = {}) {
97-
expect(image).toMatchImageSnapshot({
98-
customDiffConfig: { ...customConfig, ...overrideConfig },
99-
customSnapshotIdentifier: `${layout}-${theme}-${viewportName}-${name}-snap`,
100-
customDiffDir: diffSnapshotsDir,
101-
storeReceivedOnFailure: true,
102-
customReceivedDir: diffSnapshotsDir
97+
const snapshotName = `${layout}-${theme}-${viewportName}-${name}`;
98+
const snapshotPath = path.join(diffSnapshotsDir, `${snapshotName}.png`);
99+
const diffPath = path.join(diffSnapshotsDir, `${snapshotName}.diff.png`);
100+
101+
return compareImages({
102+
imageBuffer: image,
103+
snapshotPath,
104+
diffPath,
105+
threshold: overrideConfig.threshold ?? customConfig.threshold,
106+
}).then(({ equal, created }) => {
107+
if (!equal) {
108+
throw new Error(`Image mismatch for "${snapshotName}". See diff at: ${diffPath}`);
109+
}
103110
});
104111
}
105112

@@ -155,11 +162,11 @@ module.exports = (env, { port = 8080, urls = {} } = {}) => {
155162
// await page.waitForTimeout(3000);
156163
// }
157164

158-
async function takeScreenshot(options) {
159-
// await hideFooter();
165+
async function takeScreenshot(options) {
160166
return await page.screenshot({
161167
...(options || {}),
162-
captureBeyondViewport: false
168+
captureBeyondViewport: false,
169+
encoding: 'binary'
163170
});
164171
}
165172

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// utils/imageComparator.js
2+
const fs = require('fs');
3+
const path = require('path');
4+
const looksSame = require('looks-same');
5+
6+
function ensureDirSync(dir) {
7+
if (!fs.existsSync(dir)) {
8+
fs.mkdirSync(dir, { recursive: true });
9+
}
10+
}
11+
12+
async function compareImages({ imageBuffer, snapshotPath, diffPath, threshold = 0.01 }) {
13+
ensureDirSync(path.dirname(snapshotPath));
14+
ensureDirSync(path.dirname(diffPath));
15+
16+
// If no snapshot exists, create it
17+
if (!fs.existsSync(snapshotPath)) {
18+
fs.writeFileSync(snapshotPath, imageBuffer);
19+
return { equal: true, created: true };
20+
}
21+
22+
return new Promise((resolve, reject) => {
23+
looksSame(imageBuffer, fs.readFileSync(snapshotPath), {
24+
strict: false,
25+
tolerance: threshold,
26+
}, (err, { equal }) => {
27+
if (err) return reject(err);
28+
29+
if (!equal) {
30+
looksSame.createDiff({
31+
reference: snapshotPath,
32+
current: imageBuffer,
33+
diff: diffPath,
34+
highlightColor: '#ff00ff', // pink
35+
}, () => {
36+
resolve({ equal: false });
37+
});
38+
} else {
39+
resolve({ equal: true });
40+
}
41+
});
42+
});
43+
}
44+
45+
module.exports = { compareImages };

0 commit comments

Comments
 (0)