Skip to content
This repository was archived by the owner on Sep 21, 2022. It is now read-only.

Commit 3183c21

Browse files
committed
fix(html-reporter): don't store ref when it equal with current image
1 parent cc88f2b commit 3183c21

File tree

2 files changed

+120
-52
lines changed

2 files changed

+120
-52
lines changed

lib/reporters/html/index.js

Lines changed: 40 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
'use strict';
22

3-
var path = require('path'),
3+
const path = require('path');
4+
const chalk = require('chalk');
5+
const fs = require('fs-extra');
6+
const Promise = require('bluebird');
47

5-
Promise = require('bluebird'),
6-
fs = require('fs-extra'),
7-
8-
view = require('./view'),
9-
ViewModel = require('./view-model'),
10-
lib = require('./lib'),
11-
chalk = require('chalk'),
12-
logger = require('../../utils').logger,
13-
14-
Events = require('../../constants/events');
8+
const lib = require('./lib');
9+
const view = require('./view');
10+
const ViewModel = require('./view-model');
11+
const logger = require('../../utils').logger;
12+
const Events = require('../../constants/events');
1513

1614
/**
1715
* @param {String} srcPath
@@ -20,7 +18,7 @@ var path = require('path'),
2018
*/
2119
function copyImage(srcPath, destPath) {
2220
return makeDirFor(destPath)
23-
.then(fs.copyAsync.bind(fs, srcPath, destPath));
21+
.then(() => fs.copyAsync(srcPath, destPath));
2422
}
2523

2624
/**
@@ -30,7 +28,7 @@ function copyImage(srcPath, destPath) {
3028
*/
3129
function saveDiff(result, destPath) {
3230
return makeDirFor(destPath)
33-
.then(result.saveDiffTo.bind(result, destPath));
31+
.then(() => result.saveDiffTo(destPath));
3432
}
3533

3634
/**
@@ -46,20 +44,16 @@ function prepareViewData(runner) {
4644

4745
runner.on(Events.SKIP_STATE, model.addSkipped.bind(model));
4846

49-
runner.on(Events.TEST_RESULT, function(r) {
50-
if (r.equal) {
51-
model.addSuccess(r);
52-
} else {
53-
model.addFail(r);
54-
}
47+
runner.on(Events.TEST_RESULT, (result) => {
48+
result.equal ? model.addSuccess(result) : model.addFail(result);
5549
});
5650

5751
runner.on(Events.RETRY, model.addRetry.bind(model));
5852

5953
runner.on(Events.ERROR, model.addError.bind(model));
6054
runner.on(Events.WARNING, model.addWarning.bind(model));
6155

62-
runner.on(Events.END, function() {
56+
runner.on(Events.END, () => {
6357
resolve(model.getResult());
6458
});
6559
});
@@ -77,11 +71,18 @@ function logPathToHtmlReport() {
7771

7872
function prepareImages(runner) {
7973
function handleTestResultEvent_(testResult) {
80-
return Promise.all([
81-
copyImage(testResult.currentPath, lib.currentAbsolutePath(testResult)),
82-
copyImage(testResult.referencePath, lib.referenceAbsolutePath(testResult)),
83-
testResult.equal || saveDiff(testResult, lib.diffAbsolutePath(testResult))
84-
]);
74+
let actions = [
75+
copyImage(testResult.currentPath, lib.currentAbsolutePath(testResult))
76+
];
77+
78+
if (!testResult.equal) {
79+
actions.push(
80+
copyImage(testResult.referencePath, lib.referenceAbsolutePath(testResult)),
81+
saveDiff(testResult, lib.diffAbsolutePath(testResult))
82+
);
83+
}
84+
85+
return Promise.all(actions);
8586
}
8687

8788
function handleErrorEvent_(testResult) {
@@ -93,47 +94,40 @@ function prepareImages(runner) {
9394
return new Promise((resolve, reject) => {
9495
let queue = Promise.resolve(true);
9596

96-
runner.on(Events.WARNING, function(testResult) {
97-
queue = queue.then(function() {
97+
runner.on(Events.WARNING, (testResult) => {
98+
queue = queue.then(() => {
9899
return copyImage(testResult.currentPath, lib.currentAbsolutePath(testResult));
99100
});
100101
});
101102

102-
runner.on(Events.ERROR, function(testResult) {
103-
queue = queue.then(function() {
104-
return handleErrorEvent_(testResult);
105-
});
103+
runner.on(Events.ERROR, (testResult) => {
104+
queue = queue.then(() => handleErrorEvent_(testResult));
106105
});
107106

108-
runner.on(Events.RETRY, function(testResult) {
109-
queue = queue.then(function() {
107+
runner.on(Events.RETRY, (testResult) => {
108+
queue = queue.then(() => {
110109
return testResult.hasOwnProperty('equal')
111110
? handleTestResultEvent_(testResult)
112111
: handleErrorEvent_(testResult);
113112
});
114113
});
115114

116115
runner.on(Events.TEST_RESULT, function(testResult) {
117-
queue = queue.then(function() {
118-
return handleTestResultEvent_(testResult);
119-
});
116+
queue = queue.then(() => handleTestResultEvent_(testResult));
120117
});
121118

122-
runner.on(Events.END, function() {
123-
logPathToHtmlReport();
124-
119+
runner.on(Events.END, () => {
125120
queue.then(resolve, reject);
126121
});
127122
});
128123
}
129124

130-
module.exports = function htmlReporter(tester) {
131-
Promise.all([
132-
prepareViewData(tester),
133-
prepareImages(tester)
134-
])
125+
module.exports = function htmlReporter(runner) {
126+
const generateReportPromise = Promise.all([prepareViewData(runner), prepareImages(runner)])
135127
.spread(view.createHtml)
136128
.then(view.save)
137-
.catch(logError)
138-
.done();
129+
.then(logPathToHtmlReport)
130+
.catch(logError);
131+
132+
runner.on(Events.END_RUNNER, () => generateReportPromise.thenReturn());
139133
};
Lines changed: 80 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,43 @@
11
'use strict';
22

3-
const EventEmitter = require('events').EventEmitter;
3+
const _ = require('lodash');
4+
const fs = require('fs-extra');
5+
const Promise = require('bluebird');
6+
const QEmitter = require('qemitter');
47
const HtmlReporter = require('lib/reporters/html/index.js');
58
const Events = require('lib/constants/events');
69
const Handlebars = require('handlebars');
710
const logger = require('lib/utils').logger;
811
const chalk = require('chalk');
912
const path = require('path');
13+
const lib = require('lib/reporters/html/lib');
1014
const view = require('lib/reporters/html/view');
1115

1216
describe('HTML Reporter', () => {
1317
const sandbox = sinon.sandbox.create();
1418
let emitter;
1519

20+
function mkStubResult_(options) {
21+
return _.defaultsDeep(options, {
22+
state: {name: 'name-default'},
23+
browserId: 'browserId-default',
24+
suite: {
25+
path: ['suite/path-default'],
26+
metaInfo: {sessionId: 'sessionId-default'}
27+
},
28+
currentPath: 'current/path-default',
29+
referencePath: 'reference/path-default',
30+
equal: false
31+
});
32+
}
33+
1634
beforeEach(() => {
1735
sandbox.stub(view, 'save');
36+
sandbox.stub(logger, 'log');
37+
sandbox.stub(fs, 'copyAsync').returns(Promise.resolve());
38+
sandbox.stub(fs, 'mkdirsAsync').returns(Promise.resolve());
1839

19-
emitter = new EventEmitter();
40+
emitter = new QEmitter();
2041

2142
// calling constructor for its side effect
2243
new HtmlReporter(emitter); // eslint-disable-line no-new
@@ -25,12 +46,12 @@ describe('HTML Reporter', () => {
2546
afterEach(() => sandbox.restore());
2647

2748
it('should log correct path to html report', () => {
28-
sandbox.stub(logger, 'log');
29-
3049
emitter.emit(Events.END);
3150

32-
const reportPath = `file://${path.resolve('gemini-report/index.html')}`;
33-
assert.calledWith(logger.log, `Your HTML report is here: ${chalk.yellow(reportPath)}`);
51+
return emitter.emitAndWait(Events.END_RUNNER).then(() => {
52+
const reportPath = `file://${path.resolve('gemini-report/index.html')}`;
53+
assert.calledWith(logger.log, `Your HTML report is here: ${chalk.yellow(reportPath)}`);
54+
});
3455
});
3556

3657
it('should escape special chars in urls', () => {
@@ -42,4 +63,57 @@ describe('HTML Reporter', () => {
4263

4364
assert.equal(render(data), '<img data-src="images/fake/long%2Bpath/fakeName/fakeId~current.png">');
4465
});
66+
67+
it('should save only current when screenshots are equal', () => {
68+
sandbox.stub(lib, 'currentAbsolutePath').returns('absolute/current/path');
69+
70+
emitter.emit(Events.TEST_RESULT, mkStubResult_({
71+
currentPath: 'current/path',
72+
equal: true
73+
}));
74+
75+
emitter.emit(Events.END);
76+
77+
return emitter.emitAndWait(Events.END_RUNNER).then(() => {
78+
assert.calledOnce(fs.copyAsync);
79+
assert.calledWith(fs.copyAsync, 'current/path', 'absolute/current/path');
80+
});
81+
});
82+
83+
describe('when screenshots are not equal', () => {
84+
function emitResult_(options) {
85+
emitter.emit(Events.TEST_RESULT, mkStubResult_(options));
86+
emitter.emit(Events.END);
87+
return emitter.emitAndWait(Events.END_RUNNER);
88+
}
89+
90+
it('should save current image', () => {
91+
sandbox.stub(lib, 'currentAbsolutePath').returns('/absolute/report/current/path');
92+
93+
return emitResult_({currentPath: 'current/path'})
94+
.then(() => {
95+
assert.calledWith(fs.copyAsync, 'current/path', '/absolute/report/current/path');
96+
});
97+
});
98+
99+
it('should save reference image', () => {
100+
sandbox.stub(lib, 'referenceAbsolutePath').returns('/absolute/report/reference/path');
101+
102+
return emitResult_({referencePath: 'reference/path'})
103+
.then(() => {
104+
assert.calledWith(fs.copyAsync, 'reference/path', '/absolute/report/reference/path');
105+
});
106+
});
107+
108+
it('should save diff image', () => {
109+
const saveDiffTo = sandbox.stub();
110+
111+
sandbox.stub(lib, 'diffAbsolutePath').returns('/absolute/report/diff/path');
112+
113+
return emitResult_({saveDiffTo})
114+
.then(() => {
115+
assert.calledWith(saveDiffTo, '/absolute/report/diff/path');
116+
});
117+
});
118+
});
45119
});

0 commit comments

Comments
 (0)