Skip to content

Commit bad4af3

Browse files
authored
[#46] Fixed issue with jest failure if error happens in steps (#47)
1 parent 39d4c8b commit bad4af3

File tree

7 files changed

+75
-9
lines changed

7 files changed

+75
-9
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
## [1.1.4] - 2017-11-03
2+
### Fixed
3+
- Fixed issue with jest failure if error happens in steps
4+
15
## [1.1.3] - 2017-10-30
2-
### Changed
6+
### Fixed
37
- Fixed issue with calling cleanup
48
- Improved readme file
59

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.1.3",
3+
"version": "1.1.4",
44
"description": "Perceptual diffing tool",
55
"main": "dist/index.js",
66
"scripts": {

src/helpers/proxyChain.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ const chainProxy = (target) => {
7575
const defaultParams = {
7676
resultFuncName: 'result',
7777
endFuncName: 'end',
78-
updateFunctionName: '_compareImage',
78+
updateFunctionName: '_evaluateResult',
7979
};
8080
const chainObject = new ChainObject(target, defaultParams);
8181
return new Proxy(

src/helpers/proxyChain.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class ForTest {
88
return this._value;
99
}
1010

11-
_compareImage() { return this._value; }
11+
_evaluateResult() { return this._value; }
1212
toMatchSnapshot() { this._value = 'Executed first'; }
1313

1414
async echo(text) {

src/page.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import puppeteer from 'puppeteer';
22
import logger from './utils/logger';
3-
import toMatchImageSnapshot from './utils/jestMatchImageSnapshot';
3+
import jestMatchers from './utils/jestMatchers';
44
import compareImage from './compareImage';
55
import functionToString from './helpers/functionToString';
66
import freezeImage from './freezeImage';
@@ -12,12 +12,12 @@ export default class Page {
1212
this.browser = browser;
1313
this.tab = null;
1414
this.prefixedLogger = logger.prefix(this.testConfig.testName);
15-
this.error = false;
15+
this.error = null;
1616
this.image = null;
1717
}
1818

1919
_logError(error) {
20-
this.error = true;
20+
this.error = error;
2121
this.prefixedLogger.error(error);
2222
}
2323

@@ -123,22 +123,31 @@ export default class Page {
123123
}
124124
}
125125

126-
async _compareImage() {
126+
async _evaluateResult() {
127127
if (!this.error) {
128128
let result;
129129
try {
130130
result = await compareImage(this.image, this.globalConfig, this.testConfig);
131131
} catch (error) {
132132
this._logError(error);
133133
}
134+
if (this.error) {
135+
return false;
136+
}
134137
if (this.testConfig.isJest === true) {
138+
const toMatchImageSnapshot = jestMatchers.toMatchImageSnapshot;
135139
expect.extend({ toMatchImageSnapshot });
136140
expect(result).toMatchImageSnapshot(this.testStats);
137141
}
138142
if (result.matched || result.updated || result.added) {
139143
return true;
140144
}
141145
}
146+
if (this.testConfig.isJest && this.error) {
147+
const toNotError = jestMatchers.toNotError;
148+
expect.extend({ toNotError });
149+
expect(this.error).toNotError(this.testStats);
150+
}
142151
return false;
143152
}
144153

src/page.test.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,18 @@ import { globalConfig, testConfig } from './config/defaultConfigs';
44
import functionToString from './helpers/functionToString';
55
import freezeImage from './freezeImage';
66
import { sanitiseGlobalConfiguration } from './sanitiser';
7+
import jestMatchers from './utils/jestMatchers';
8+
import compareImage from './compareImage';
9+
10+
const mockMatcher = jest.fn(() => ({
11+
message: 'message',
12+
pass: true,
13+
}));
14+
15+
jestMatchers.toNotError = mockMatcher;
16+
jestMatchers.toMatchImageSnapshot = mockMatcher;
17+
18+
jest.mock('./compareImage');
719

820
const tabMocks = {
921
goto: jest.fn(),
@@ -53,7 +65,10 @@ describe('Page', () => {
5365
afterEach(() => {
5466
puppeteer.launch.mockClear();
5567
mockLog.mockClear();
68+
mockErr.mockClear();
5669
functionToString.mockClear();
70+
mockMatcher.mockClear();
71+
compareImage.mockClear();
5772
page.error = false;
5873
});
5974
beforeEach(() => {
@@ -159,6 +174,27 @@ describe('Page', () => {
159174
expect(mockErr).toHaveBeenCalledTimes(0);
160175
});
161176
});
177+
describe('_evaluateResult', () => {
178+
it('it calls toNotError if error happens in any steps when in jest mode', async () => {
179+
page.error = new Error('Error happened');
180+
const result = await page._evaluateResult();
181+
expect(jestMatchers.toNotError).toHaveBeenCalled();
182+
expect(result).toEqual(false);
183+
});
184+
it('it wont calls toMatchImageSnapshot when in jest mode and compareImage throws', async () => {
185+
const result = await page._evaluateResult();
186+
expect(compareImage).toHaveBeenCalled();
187+
expect(jestMatchers.toMatchImageSnapshot).not.toHaveBeenCalled();
188+
expect(result).toEqual(false);
189+
});
190+
it('it calls toMatchImageSnapshot when in jest mode', async () => {
191+
compareImage.mockReturnValueOnce({ matched: true });
192+
const result = await page._evaluateResult();
193+
expect(compareImage).toHaveBeenCalled();
194+
expect(jestMatchers.toMatchImageSnapshot).toHaveBeenCalled();
195+
expect(result).toEqual(true);
196+
});
197+
});
162198
describe('FreezeImage', () => {
163199
beforeEach(() => {
164200
tabMocks.evaluate.mockClear();

src/utils/jestMatchImageSnapshot.js renamed to src/utils/jestMatchers.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,21 @@ const toMatchImageSnapshot = (result, testState) => {
2828
};
2929
};
3030

31-
export default toMatchImageSnapshot;
31+
const toNotError = (error, testState) => {
32+
const snapshotState = testState;
33+
expect.setState(snapshotState, {
34+
_counters: snapshotState.snapshotState._counters.set(
35+
snapshotState.currentTestName,
36+
(snapshotState.snapshotState._counters.get(snapshotState.currentTestName) || 0) + 1,
37+
),
38+
});
39+
40+
const message = () => 'Failed to run your test.\n'
41+
+ `${chalk.bold.red('Cause:')} ${chalk.red(error)}`;
42+
return {
43+
message,
44+
pass: false,
45+
};
46+
};
47+
48+
export default { toMatchImageSnapshot, toNotError };

0 commit comments

Comments
 (0)