Skip to content

Commit db67399

Browse files
authored
Add custom path example (#78)
1 parent 654fd66 commit db67399

File tree

12 files changed

+82
-9
lines changed

12 files changed

+82
-9
lines changed

API.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,26 @@ In this example, differencify will got to different pages and compare screenshot
338338
```
339339
In this example, differencify will got to different pages and compare screenshots with reference screenshots.
340340
341+
## Custom test path
342+
343+
```js
344+
(async () => {
345+
const differencify = new Differencify({ imageSnapshotPath: './custom_test_path' });
346+
const target = differencify.init({ chain: false });
347+
await target.launch();
348+
const page = await target.newPage();
349+
await page.setViewport({ width: 1600, height: 1200 });
350+
await page.goto('http://example.com/');
351+
await page.waitFor(1000);
352+
const image = await page.screenshot();
353+
const result = await target.toMatchSnapshot(image);
354+
await page.close();
355+
console.log(result); // True or False
356+
console.log(result2); // True or False
357+
})();
358+
```
359+
In this example, you can specify the custom path for storing images.
360+
341361
342362
343363

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## [1.3.1] - 2018-6-6
2+
### Fixed
3+
- Bug fixed for custom test path
4+
15
## [1.3.1] - 2018-5-25
26
### Fixed
37
- Bug fixed for testName not being picked up correctly

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ RUN npm install differencify
188188

189189
## Links
190190

191-
See [examples](examples) for usages and CI integration with jest
191+
See [examples](./src/integration.tests) for usages and CI integration with jest
192192

193193
Visit project [Gitter Chat](https://gitter.im/differencify/QA) for general Q/A around project
194194

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "differencify",
3-
"version": "1.3.1",
3+
"version": "1.3.2",
44
"description": "Perceptual diffing tool",
55
"main": "dist/index.js",
66
"scripts": {

src/__snapshots__/sanitiser.test.js.snap

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ exports[`sanitiser sanitise global configuration sanitise if no config provided
44
Object {
55
"debug": false,
66
"imageSnapshotPath": "differencify_reports",
7+
"imageSnapshotPathProvided": false,
78
"mismatchThreshold": 0.001,
89
"saveDifferencifiedImage": true,
910
}
@@ -13,6 +14,7 @@ exports[`sanitiser sanitise global configuration sanitise if puppeteer specific
1314
Object {
1415
"debug": false,
1516
"imageSnapshotPath": "differencify_reports",
17+
"imageSnapshotPathProvided": false,
1618
"mismatchThreshold": 0.001,
1719
"saveDifferencifiedImage": true,
1820
}
@@ -22,6 +24,7 @@ exports[`sanitiser sanitise global configuration sanitise if screenshots config
2224
Object {
2325
"debug": false,
2426
"imageSnapshotPath": "differencify_reports",
27+
"imageSnapshotPathProvided": false,
2528
"mismatchThreshold": 0.001,
2629
"saveDifferencifiedImage": true,
2730
}

src/compareImage.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ const saveDiff = (diff, diffPath) =>
1414
diff.image.write(diffPath, cb);
1515
});
1616

17-
const compareImage = async (capturedImage, globalConfig, testConfig) => {
18-
const prefixedLogger = logger.prefix(testConfig.testName);
17+
const getSnapshotsDir = (testConfig, globalConfig) => {
1918
let testRoot;
2019
if (testConfig.isJest) {
2120
testRoot = path.dirname(testConfig.testPath);
@@ -26,8 +25,16 @@ const compareImage = async (capturedImage, globalConfig, testConfig) => {
2625
fs.mkdirSync(testRoot);
2726
}
2827
}
28+
return path.join(testRoot, '__image_snapshots__');
29+
};
30+
31+
const compareImage = async (capturedImage, globalConfig, testConfig) => {
32+
const prefixedLogger = logger.prefix(testConfig.testName);
33+
34+
const snapshotsDir = globalConfig.imageSnapshotPathProvided ?
35+
globalConfig.imageSnapshotPath :
36+
getSnapshotsDir(testConfig, globalConfig);
2937

30-
const snapshotsDir = path.join(testRoot, '__image_snapshots__');
3138
const snapshotPath = path.join(snapshotsDir, `${testConfig.testName}.snap.${testConfig.imageType || 'png'}`);
3239

3340
const diffDir = path.join(snapshotsDir, '__differencified_output__');

src/compareImage.test.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable prefer-object-spread/prefer-object-spread */
12
import Jimp from 'jimp';
23
import fs from 'fs';
34
import compareImage from './compareImage';
@@ -33,6 +34,7 @@ jest.mock('./utils/logger', () => ({
3334

3435
const mockConfig = {
3536
imageSnapshotPath: './differencify_report',
37+
imageSnapshotPathProvided: false,
3638
saveDifferencifiedImage: true,
3739
mismatchThreshold: 0.01,
3840
};
@@ -74,6 +76,22 @@ describe('Compare Image', () => {
7476
expect(result).toEqual({ updated: true });
7577
expect(fs.writeFileSync).toHaveBeenCalledWith('/parent/__image_snapshots__/test.snap.png', Object);
7678
});
79+
it('respects to imageSnapshotPath when in jest mode', async () => {
80+
const newGlobalConfig = Object.assign({}, mockConfig,
81+
{
82+
imageSnapshotPath: './someImagePath',
83+
imageSnapshotPathProvided: true,
84+
});
85+
const result = await compareImage(Object, newGlobalConfig, {
86+
isUpdate: true,
87+
isJest: true,
88+
testName: 'test',
89+
testPath: '/src/test.js',
90+
imageType: 'png',
91+
});
92+
expect(result).toEqual({ updated: true });
93+
expect(fs.writeFileSync).toHaveBeenCalledWith('./someImagePath/test.snap.png', Object);
94+
});
7795
});
7896

7997
describe('non-jest mode', () => {
@@ -210,10 +228,8 @@ describe('Compare Image', () => {
210228
write: mockWrite,
211229
},
212230
});
213-
// eslint-disable-next-line prefer-object-spread/prefer-object-spread
214231
const result = await compareImage(
215232
Object,
216-
// eslint-disable-next-line prefer-object-spread/prefer-object-spread
217233
Object.assign({}, mockConfig, { saveDifferencifiedImage: true }),
218234
mockTestConfig,
219235
);

src/index.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ describe('Differencify', () => {
7979
mismatchThreshold: 0.001,
8080
saveDifferencifiedImage: true,
8181
imageSnapshotPath: 'differencify_reports',
82+
imageSnapshotPathProvided: false,
8283
},
8384
{
8485
chain: false,
25.5 KB
Loading

src/integration.tests/integration.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,4 +185,23 @@ describe('Differencify', () => {
185185
await page.close();
186186
expect(result).toEqual(true);
187187
}, 20000);
188+
it('Custom test path', async () => {
189+
const customDifferencify = new Differencify({
190+
imageSnapshotPath: './src/integration.tests/__image_snapshots__/custom_test_path',
191+
debug: true,
192+
});
193+
await customDifferencify.launchBrowser({ args: ['--no-sandbox', '--disable-setuid-sandbox'] });
194+
const target = customDifferencify.init({
195+
chain: false,
196+
});
197+
const page = await target.newPage();
198+
await page.setViewport({ width: 1600, height: 1200 });
199+
await page.goto('http://example.com/');
200+
await page.waitFor(1000);
201+
const image = await page.screenshot();
202+
const result = await target.toMatchSnapshot(image);
203+
await page.close();
204+
await customDifferencify.cleanup();
205+
expect(result).toEqual(true);
206+
}, 20000);
188207
});

0 commit comments

Comments
 (0)