Skip to content

Commit

Permalink
1.2.0: added the ability to attach diff image (jest-image-snapshot)
Browse files Browse the repository at this point in the history
  • Loading branch information
nikita.kryazhin committed Jun 20, 2019
1 parent 29be7a8 commit ece2ee2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
{
"name": "jest-puppeteer-allure",
"version": "1.1.1",
"version": "1.2.0",
"description": "This package allows you to generate an allure report.",
"main": "src/index.js",
"keywords": [
"report",
"allure",
"jest",
"puppeteer"
"puppeteer",
"jest-image-snapshot"
],
"author": "Nikita Kryazhin <kryzhin94@gmail.com>",
"license": "MIT",
Expand Down
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,5 @@ it('Test', async () => {
reporter.addAttachment('Screenshot', screenshot, 'image/jpg');
})
```

If you use [jest-image-snapshot](https://github.com/americanexpress/jest-image-snapshot) then diff image attach to test report.
24 changes: 16 additions & 8 deletions src/registerAllureReporter.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const Allure = require('allure-js-commons');
const stripAnsi = require('strip-ansi');
const Reporter = require('./reporter');
const fs = require('fs');

function registerAllureReporter() {
const allure = new Allure();
Expand All @@ -22,38 +23,45 @@ function registerAllureReporter() {
}
};

const addStatus = async (spec, screen, failure) => {
const addStatus = async (spec, failure) => {
let error;
if (spec.status === 'pending') {
error = { message: spec.pendingReason };
return error;
}
if (spec.status === 'disabled') {
error = { message: 'This test was disabled' };
return error;
}
if (failure) {
error = {
message: stripAnsi(failure.message),
stack: stripAnsi(failure.stack),
};
allure.addAttachment('screenshot', screen, 'image/png');
if (logError.length || logPageError.length) {
const errorText = `${logError.join('\n')}\n${logPageError.join('\n')}`;
allure.addAttachment('console error', errorText, 'text/plain');
}
const rx = /See diff for details: (.*)/g;
const arrMessage = rx.exec(error.message);
if (arrMessage) {
const diffImage = fs.readFileSync(arrMessage[1]);
allure.addAttachment('diff', diffImage, 'image/png');
return error;
}
const screen = await page.screenshot();
allure.addAttachment('screenshot', screen, 'image/png');
}
allure.endCase(spec.status, error);
return error;
};

const asyncSpecDone = async spec => {
const failure =
spec.failedExpectations && spec.failedExpectations.length
? spec.failedExpectations[0]
: undefined;
let screen;
if (failure) {
screen = await page.screenshot();
}
await addStatus(spec, screen, failure);
const error = await addStatus(spec, failure);
allure.endCase(spec.status, error);
};

beforeEach(() => wait());
Expand Down

0 comments on commit ece2ee2

Please sign in to comment.