Skip to content

Commit fb25644

Browse files
author
Dan Ristea
committed
Tidy up code + npmignore
1 parent b134be2 commit fb25644

File tree

10 files changed

+40
-19
lines changed

10 files changed

+40
-19
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,5 @@ dist/
3232
# Built docs
3333
docs/
3434
.vscode/
35-
dist/
3635
differencify_report/
3736
screenshots/

.npmignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,13 @@
11
src/
22
examples/
3+
.circleci/
4+
.vscode/
5+
.idea/
6+
.babelrc
7+
.editorconfig
8+
.eslintignore
9+
.gitignore
10+
.eslintrc
11+
.nvmrc
12+
_config.yml
13+
Dockerfile

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
## [0.0.5] - 2017-07-06
3030
### Changed
31-
- Decreased default threashhold to 0.01
31+
- Decreased default threshold to 0.01
3232
- Updated logging to log difference error
3333
- Update Readme.md
3434
- Added a Dockerfile for local/CI usage

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const differencify = new Differencify(GlobalOptions);
3030
```js
3131
async () => {
3232
const result = await differencify.update(TestOptions);
33-
console.log(result); //true if update succeded
33+
console.log(result); //true if update succeeded
3434
}
3535
```
3636
### Validate your changes

src/chromyRunner.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ import actions from './actions';
55
import { configTypes } from './defaultConfig';
66

77
const saveImage = (filename, image, testType, screenshotsPath, testReportPath) => {
8-
const filePath = testType === configTypes.test ? testReportPath : screenshotsPath;
9-
logger.log(`screenshot saved in -> ${filePath}/${filename}.png`);
10-
return fs.writeFileSync(`${filePath}/${filename}.png`, image);
8+
const directory = testType === configTypes.test ? testReportPath : screenshotsPath;
9+
const filePath = `${directory}/${filename}.png`;
10+
logger.log(`screenshot saved in -> ${filePath}`);
11+
return fs.writeFileSync(filePath, image);
1112
};
1213

1314
const run = async (chromy, options, test) => {
@@ -29,7 +30,7 @@ const run = async (chromy, options, test) => {
2930
try {
3031
logger.log('Capturing screenshot of whole DOM');
3132
const png = await chromy.screenshotDocument();
32-
await saveImage(test.name, png, test.type, options.screenshots, options.testReportPath);
33+
saveImage(test.name, png, test.type, options.screenshots, options.testReportPath);
3334
} catch (error) {
3435
logger.error(error);
3536
return false;
@@ -39,7 +40,7 @@ const run = async (chromy, options, test) => {
3940
try {
4041
logger.log('Capturing screenshot of chrome window');
4142
const png = await chromy.screenshot();
42-
await saveImage(test.name, png, test.type, options.screenshots, options.testReportPath);
43+
saveImage(test.name, png, test.type, options.screenshots, options.testReportPath);
4344
} catch (error) {
4445
logger.error(error);
4546
return false;
@@ -49,7 +50,7 @@ const run = async (chromy, options, test) => {
4950
try {
5051
logger.log(`Capturing screenshot of ${action.value} selector`);
5152
const png = await chromy.screenshotSelector(action.value);
52-
await saveImage(test.name, png, test.type, options.screenshots, options.testReportPath);
53+
saveImage(test.name, png, test.type, options.screenshots, options.testReportPath);
5354
} catch (error) {
5455
logger.error(error);
5556
return false;
@@ -59,8 +60,6 @@ const run = async (chromy, options, test) => {
5960
break;
6061
case actions.test:
6162
try {
62-
logger.log(`comparing -> ${options.testReportPath}/${test.name}.png
63-
and ${options.screenshots}/${test.name}.png`);
6463
const result = await compareImage(options, test.name);
6564
logger.log(result);
6665
} catch (error) {

src/chromyRunner.test.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ describe('ChromyRunner', () => {
6969
expect(loggerCalls[0]).toEqual('goto -> www.example.com');
7070
expect(loggerCalls[1]).toEqual('Capturing screenshot of whole DOM');
7171
expect(loggerCalls[2]).toEqual('screenshot saved in -> ./differencify_report/default.png');
72-
expect(loggerCalls[4]).toEqual('Saving the diff image to disk');
7372
expect(writeFileSyncCalls).toEqual(['./differencify_report/default.png', 'png file']);
7473
});
7574
it('Step runner: update action', async () => {

src/compareImage.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ const compareImage = async (options, testName) => {
55
const referenceFile = `${options.screenshots}/${testName}.png`;
66
const testFile = `${options.testReportPath}/${testName}.png`;
77

8+
logger.log(`${testName}: comparing ${referenceFile} and ${testFile}`);
9+
810
let referenceImage;
911
try {
1012
referenceImage = await Jimp.read(referenceFile);
@@ -24,11 +26,17 @@ const compareImage = async (options, testName) => {
2426
if (distance < options.mismatchThreshold || diff.percent < options.mismatchThreshold) {
2527
return `${testName}: no mismatch found ✅`;
2628
}
29+
2730
if (options.saveDifferencifiedImage) {
28-
const diffPath = `${options.testReportPath}/${testName}_differencified.png`;
29-
logger.log(`${testName}: Saving the diff image to disk at ${diffPath}`);
30-
diff.image.write(diffPath);
31+
try {
32+
const diffPath = `${options.testReportPath}/${testName}_differencified.png`;
33+
diff.image.write(diffPath);
34+
logger.log(`${testName}: saved the diff image to disk at ${diffPath}`);
35+
} catch (err) {
36+
throw new Error(`${testName}: failed to save the diff image ${err}`);
37+
}
3138
}
39+
3240
throw new Error(`${testName}: mismatch found❗
3341
Result:
3442
distance: ${distance}

src/index.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export default class Differencify {
2929
createDir(this.configuration.screenshots);
3030
createDir(this.configuration.testReportPath);
3131
}
32+
3233
_createChromeInstance(testConfig) {
3334
const width = testConfig.resolution.width || CHROME_WIDTH;
3435
const height = testConfig.resolution.height || CHROME_HEIGHT;
@@ -41,19 +42,22 @@ export default class Differencify {
4142
});
4243
return chromy;
4344
}
45+
4446
_updateChromeInstances(id, chromy) {
4547
this.chromeInstances[id] = chromy;
4648
this.chromeInstancesId += 1;
4749
}
50+
4851
async _closeChrome(id, chromy) {
4952
try {
5053
logger.log('closing browser');
51-
chromy.close();
54+
await chromy.close();
5255
delete this.chromeInstances[id];
5356
} catch (error) {
5457
logger.error(error);
5558
}
5659
}
60+
5761
async _run(config, type, step) {
5862
const testConfig = sanitiseTestConfiguration(config);
5963
const chromy = this._createChromeInstance(testConfig);
@@ -67,13 +71,16 @@ export default class Differencify {
6771
await this._closeChrome(testId, chromy);
6872
return result;
6973
}
74+
7075
async update(config) {
7176
return await this._run(config, configTypes.update, null);
7277
}
78+
7379
async test(config) {
7480
const testStep = { name: actions.test, value: this.configuration.testReportPath };
7581
return await this._run(config, configTypes.test, testStep);
7682
}
83+
7784
async cleanup() {
7885
await Promise.all(
7986
Object.values(this.chromeInstances).map(chromeInstance => chromeInstance.close()),

src/index.test.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import Differencify from './index';
44
import logger from './logger';
55

66
let chromyCloseCallsCounter = 0;
7-
jest.mock('chromy', () => jest.fn().mockImplementation(() =>
7+
jest.mock('chromy', () => jest.fn(() =>
88
({
99
goto: jest.fn(),
1010
close: jest.fn(() => { chromyCloseCallsCounter += 1; }),
@@ -78,7 +78,6 @@ describe('Differencify', () => {
7878
expect(loggerCalls[0]).toEqual('goto -> www.example.com');
7979
expect(loggerCalls[1]).toEqual('Capturing screenshot of whole DOM');
8080
expect(loggerCalls[2]).toEqual('screenshot saved in -> ./differencify_report/default.png');
81-
expect(loggerCalls[4]).toEqual('Saving the diff image to disk');
8281
expect(writeFileSyncCalls).toEqual(['./differencify_report/default.png', 'png file']);
8382
});
8483
it('cleanup fn', async () => {

src/logger.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
/* eslint-disable no-console */
2-
/* eslint-disable class-methods-use-this */
32
class Logger {
43
constructor() {
54
if (!Logger.instance) {

0 commit comments

Comments
 (0)