-
Notifications
You must be signed in to change notification settings - Fork 4
/
html-report-creator.js
106 lines (95 loc) · 3.75 KB
/
html-report-creator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
* Copyright (c) 2017 American Express Travel Related Services Company, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
// based on https://github.com/Hargne/jest-html-reporter but with prettier styling and jest-image-snapshot specific logic
const path = require('path');
const fs = require('fs');
const xmlbuilder = require('xmlbuilder');
const mkdirp = require('mkdirp');
const stripAnsi = require('strip-ansi');
const style = require('./html-report-style');
const writeFile = (filePath, content) => {
mkdirp.sync(path.dirname(filePath));
fs.writeFileSync(filePath, content);
};
const createHtml = () => xmlbuilder.create({
html: {
head: {
meta: { '@charset': 'utf-8' },
title: { '#text': 'Jest test results' },
style: { '@type': 'text/css', '#text': style },
},
},
});
module.exports = (result) => {
const htmlOutput = createHtml();
const summary = htmlOutput.ele('div', { class: 'summary' });
summary.ele('div', `
Test Suites: ${result.numPassedTestSuites} passed,
${result.numFailedTestSuites} failed,
${result.numPendingTestSuites} skipped,
${result.numTotalTestSuites} total
`);
summary.ele('div', `
Tests: ${result.numPassedTests} passed,
${result.numFailedTests} failed,
${result.numPendingTests} skipped,
${result.numTotalTests} total
`);
result.testResults.forEach((suite) => {
htmlOutput.ele('div', { class: 'suite-info' }, `
${suite.testFilePath}
(${(suite.perfStats.end - suite.perfStats.start) / 1000}s)
`);
const suiteTable = htmlOutput.ele('table', { class: 'suite-table' });
if (suite.testExecError) {
const testTr = suiteTable.ele('tr', { class: 'failed' });
const testExecFailureCell = testTr.ele('td');
testExecFailureCell.ele('div', 'Test suite failed to run:');
testExecFailureCell.ele('pre', `${suite.testExecError.message}\n${suite.testExecError.stack}`);
} else {
suite.testResults.forEach((test) => {
if (test.status !== 'pending') {
const testTr = suiteTable.ele('tr', { class: test.status });
const statusIcon = test.status === 'passed' ? '✔' : '✘';
const testTd = testTr.ele('td', `${statusIcon} ${test.fullName}`);
testTd.ele('span', { class: 'result' }, `${test.status} in ${test.duration / 1000}s`);
if (test.failureMessages.length > 0) {
const failureMsgDiv = testTd.ele('div');
test.failureMessages.forEach((failureMsg) => {
const plainFailureMessage = stripAnsi(failureMsg);
if (plainFailureMessage) {
// TODO: need to find better way to when it is an image snapshot
if (plainFailureMessage.includes('__image_snapshots__/')) {
const imageDiffUrl = `${plainFailureMessage.match(':(.*).png')[1]}.png`;
failureMsgDiv.ele('a', { href: imageDiffUrl });
}
failureMsgDiv.ele('pre', plainFailureMessage);
}
});
}
}
});
}
});
writeFile(
process.env.JEST_TEST_REPORT_PATH || path.join(process.cwd(), 'test-results/test-report.html'),
htmlOutput.end({
pretty: true,
indent: ' ',
newline: '\n',
allowEmpty: false,
})
);
return result;
};