Skip to content

Commit

Permalink
Add additional reporter tests. Refs NimaSoroush#10
Browse files Browse the repository at this point in the history
  • Loading branch information
badsyntax committed Aug 17, 2017
1 parent 7001c47 commit b240507
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 17 deletions.
3 changes: 2 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ docs/
.vscode/
dist/
differencify_report/
screenshots/
screenshots/
coverage/
2 changes: 1 addition & 1 deletion src/Reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Reporter {
addResult(outcome, fileName, message, diff) {
this.results.push({
outcome,
fileName: path.basename(fileName),
fileName: fileName ? path.basename(fileName) : null,
message,
diff: diff ? path.basename(diff) : null,
});
Expand Down
1 change: 1 addition & 0 deletions src/chromyRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ const run = async (chromy, options, test, reporter) => {
const result = await compareImage(options, test.name, reporter);
prefixedLogger.log(result);
} catch (error) {
reporter.addResult(false, `${test.name}.png`, error.message || '');
prefixedLogger.error(error);
return false;
}
Expand Down
54 changes: 39 additions & 15 deletions src/chromyRunner.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { globalConfig, testConfig, configTypes } from './defaultConfig';
import actions from './actions';
import functionToString from './helpers/functionToString';
import freezeImage from './freezeImage';
import Reporter from './Reporter';

jest.mock('chromy', () => () =>
({
Expand All @@ -29,6 +30,10 @@ jest.mock('./compareImage', () => jest.fn(arg =>

jest.mock('./helpers/functionToString');

jest.mock('./Reporter', () => () => ({
addResult: jest.fn(),
}));

let loggerCalls = [];
logger.prefix = () => logger;
logger.log = (...args) => {
Expand All @@ -40,9 +45,15 @@ fs.writeFileSync = (...args) => {
writeFileSyncCalls.push(...args);
};

let mockReporter;

const chromy = new Chromy();

describe('ChromyRunner', () => {
beforeEach(() => {
mockReporter = new Reporter();
});

afterEach(() => {
loggerCalls = [];
writeFileSyncCalls = [];
Expand All @@ -55,7 +66,7 @@ describe('ChromyRunner', () => {
});
it('run update', async () => {
testConfig.type = configTypes.update;
const result = await run(chromy, globalConfig, testConfig);
const result = await run(chromy, globalConfig, testConfig, mockReporter);
expect(result).toEqual(true);
expect(chromy.goto).toHaveBeenCalledWith('www.example.com');
expect(chromy.screenshotDocument).toHaveBeenCalledTimes(1);
Expand All @@ -66,7 +77,7 @@ describe('ChromyRunner', () => {
});
it('run test', async () => {
testConfig.type = configTypes.test;
const result = await run(chromy, globalConfig, testConfig);
const result = await run(chromy, globalConfig, testConfig, mockReporter);
expect(result).toEqual(true);
expect(chromy.goto).toHaveBeenCalledWith('www.example.com');
expect(chromy.screenshotDocument).toHaveBeenCalledTimes(1);
Expand All @@ -75,11 +86,24 @@ describe('ChromyRunner', () => {
expect(loggerCalls[2]).toEqual('screenshot saved in -> ./differencify_report/default.png');
expect(writeFileSyncCalls).toEqual(['./differencify_report/default.png', 'png file']);
});
it('run test fail', async () => {
// eslint-disable-next-line prefer-object-spread/prefer-object-spread
const failTestConfig = Object.assign({}, testConfig);
failTestConfig.steps = [{
name: 'test',
}];
// eslint-disable-next-line prefer-object-spread/prefer-object-spread
const failGlobalConfig = Object.assign({}, testConfig);
failGlobalConfig.screenshots = null;
const result = await run(chromy, failGlobalConfig, failTestConfig, mockReporter);
expect(result).toEqual(false);
expect(mockReporter.addResult).toHaveBeenCalledWith(false, 'default.png', '');
});
describe('Chromy runner', () => {
it('Step runner: test action', async () => {
testConfig.type = configTypes.test;
testConfig.steps.push({ name: actions.test, value: globalConfig.testReportPath });
const result = await run(chromy, globalConfig, testConfig);
const result = await run(chromy, globalConfig, testConfig, mockReporter);
testConfig.steps.pop({ name: actions.test, value: globalConfig.testReportPath });
expect(result).toEqual(true);
expect(chromy.goto).toHaveBeenCalledWith('www.example.com');
Expand All @@ -91,7 +115,7 @@ describe('ChromyRunner', () => {
});
it('Step runner: update action', async () => {
testConfig.type = configTypes.update;
const result = await run(chromy, globalConfig, testConfig);
const result = await run(chromy, globalConfig, testConfig, mockReporter);
expect(result).toEqual(true);
expect(chromy.goto).toHaveBeenCalledWith('www.example.com');
expect(chromy.screenshotDocument).toHaveBeenCalledTimes(1);
Expand All @@ -115,7 +139,7 @@ describe('ChromyRunner', () => {
],
};
newConfig.type = configTypes.test;
const result = await run(chromy, globalConfig, newConfig);
const result = await run(chromy, globalConfig, newConfig, mockReporter);
expect(result).toEqual(true);
expect(chromy.goto).toHaveBeenCalledWith('www.example.com');
expect(chromy.screenshot).toHaveBeenCalledTimes(1);
Expand All @@ -137,7 +161,7 @@ describe('ChromyRunner', () => {
],
};
newConfig.type = configTypes.test;
const result = await run(chromy, globalConfig, newConfig);
const result = await run(chromy, globalConfig, newConfig, mockReporter);
expect(result).toEqual(true);
expect(chromy.goto).toHaveBeenCalledWith('www.example.com');
expect(chromy.screenshotDocument).toHaveBeenCalledTimes(1);
Expand All @@ -159,7 +183,7 @@ describe('ChromyRunner', () => {
],
};
newConfig.type = configTypes.test;
const result = await run(chromy, globalConfig, newConfig);
const result = await run(chromy, globalConfig, newConfig, mockReporter);
expect(result).toEqual(true);
expect(chromy.goto).toHaveBeenCalledWith('www.example.com');
expect(chromy.screenshotSelector).toHaveBeenCalledTimes(1);
Expand All @@ -183,7 +207,7 @@ describe('ChromyRunner', () => {
],
};
newConfig.type = configTypes.test;
const result = await run(chromy, globalConfig, newConfig);
const result = await run(chromy, globalConfig, newConfig, mockReporter);
expect(result).toEqual(true);
expect(chromy.goto).toHaveBeenCalledWith('www.example.com');
expect(chromy.wait).toHaveBeenCalledWith(10);
Expand All @@ -203,7 +227,7 @@ describe('ChromyRunner', () => {
],
};
newConfig.type = configTypes.test;
const result = await run(chromy, globalConfig, newConfig);
const result = await run(chromy, globalConfig, newConfig, mockReporter);
expect(result).toEqual(true);
expect(chromy.goto).toHaveBeenCalledWith('www.example.com');
expect(chromy.wait).toHaveBeenCalledWith('selector name');
Expand All @@ -223,7 +247,7 @@ describe('ChromyRunner', () => {
],
};
newConfig.type = configTypes.test;
const result = await run(chromy, globalConfig, newConfig);
const result = await run(chromy, globalConfig, newConfig, mockReporter);
expect(result).toEqual(true);
expect(chromy.goto).toHaveBeenCalledWith('www.example.com');
expect(chromy.wait).toHaveBeenCalledTimes(1);
Expand All @@ -243,7 +267,7 @@ describe('ChromyRunner', () => {
],
};
newConfig.type = configTypes.test;
const result = await run(chromy, globalConfig, newConfig);
const result = await run(chromy, globalConfig, newConfig, mockReporter);
expect(result).toEqual(false);
expect(chromy.goto).toHaveBeenCalledWith('www.example.com');
expect(chromy.wait).toHaveBeenCalledTimes(0);
Expand All @@ -263,7 +287,7 @@ describe('ChromyRunner', () => {
{ name: 'execute', value: () => {} },
],
};
const result = await run(chromy, globalConfig, newConfig);
const result = await run(chromy, globalConfig, newConfig, mockReporter);
expect(result).toEqual(true);
expect(chromy.evaluate).toHaveBeenCalledTimes(1);
expect(loggerCalls[0]).toEqual('waiting for to execute function in browser');
Expand All @@ -279,7 +303,7 @@ describe('ChromyRunner', () => {
{ name: 'execute', value: 123 },
],
};
const result = await run(chromy, globalConfig, newConfig);
const result = await run(chromy, globalConfig, newConfig, mockReporter);
expect(result).toEqual(false);
expect(chromy.evaluate).toHaveBeenCalledTimes(0);
expect(loggerCalls[0]).toEqual('failed to detect execute function');
Expand All @@ -299,7 +323,7 @@ describe('ChromyRunner', () => {
{ name: 'freezeImage', value: 'selector' },
],
};
const result = await run(chromy, globalConfig, newConfig);
const result = await run(chromy, globalConfig, newConfig, mockReporter);
expect(result).toEqual(true);
expect(chromy.evaluate).toHaveBeenCalledWith('return string function');
expect(functionToString).toHaveBeenCalledWith(freezeImage, 'selector');
Expand All @@ -318,7 +342,7 @@ describe('ChromyRunner', () => {
{ name: 'freezeImage', value: 'selector' },
],
};
const result = await run(chromy, globalConfig, newConfig);
const result = await run(chromy, globalConfig, newConfig, mockReporter);
expect(result).toEqual(false);
expect(chromy.evaluate).toHaveBeenCalledWith('return string function');
expect(functionToString).toHaveBeenCalledWith(freezeImage, 'selector');
Expand Down
45 changes: 45 additions & 0 deletions src/compareImage.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,49 @@ describe('Compare Image', () => {
expect(mockWrite).toHaveBeenCalledWith('./differencify_report/test_differencified.png');
}
});

it('adds a result to reporter if saveDifferencifiedImage is true', async () => {
expect.assertions(1);
Jimp.distance.mockReturnValue(0.02);
const mockWrite = jest.fn();
Jimp.diff.mockReturnValue({
percent: 0.02,
image: {
write: mockWrite,
},
});
try {
// eslint-disable-next-line prefer-object-spread/prefer-object-spread
await compareImage(Object.assign({}, mockConfig, { saveDifferencifiedImage: true }), 'test', mockReporter);
} catch (err) {
expect(mockReporter.addResult).toHaveBeenCalledWith(
false,
'./differencify_report/test.png',
expect.stringContaining('mismatch found'),
'./differencify_report/test_differencified.png',
);
}
});

it('adds a result to reporter if saveDifferencifiedImage is false', async () => {
expect.assertions(1);
Jimp.distance.mockReturnValue(0.02);
const mockWrite = jest.fn();
Jimp.diff.mockReturnValue({
percent: 0.02,
image: {
write: mockWrite,
},
});
try {
// eslint-disable-next-line prefer-object-spread/prefer-object-spread
await compareImage(Object.assign({}, mockConfig, { saveDifferencifiedImage: false }), 'test', mockReporter);
} catch (err) {
expect(mockReporter.addResult).toHaveBeenCalledWith(
false,
'./differencify_report/test.png',
expect.stringContaining('mismatch found'),
);
}
});
});

0 comments on commit b240507

Please sign in to comment.