Skip to content

Commit

Permalink
Merge pull request #78 from Hargne/release/2.7.0
Browse files Browse the repository at this point in the history
Release 2.7.0 - Proper console.log handling
  • Loading branch information
Hargne authored Nov 5, 2019
2 parents 7f5c01c + b0cbd13 commit 61e47ca
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 30 deletions.
12 changes: 11 additions & 1 deletion jest.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,15 @@
"statements": 50
}
},
"testResultsProcessor": "./src"
"reporters": [
"default",
["./src", {
"pageTitle": "Test Suite Reporter",
"outputPath": "testResultsProcessorResult.html",
"includeFailureMsg": true,
"includeConsoleLog": true,
"useCssFile" : true,
"sort": "titleAsc"
}]
]
}
8 changes: 0 additions & 8 deletions jesthtmlreporter.config.json

This file was deleted.

14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jest-html-reporter",
"version": "2.6.2",
"version": "2.7.0",
"description": "Jest test results processor for generating a summary in HTML",
"main": "dist/main",
"unpkg": "dist/main.min.js",
Expand Down
13 changes: 13 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,25 @@ function JestHtmlReporter(globalConfig, options) {
*/
this.jestConfig = globalConfig;
this.jestOptions = options;
this.consoleLogs = [];

this.onTestResult = (data, result) => {
// Catch console logs per test
if (result.console) {
this.consoleLogs.push({
testFilePath: result.testFilePath,
logs: result.console,
});
}
};

this.onRunComplete = (contexts, testResult) => {
// Apply the configuration within jest.config.json to the current config
config.setConfigData(this.jestOptions);
// Apply the updated config
reportGenerator.config = config;
// Add the console logs that we've caught
reportGenerator.consoleLogs = this.consoleLogs;
// Generate Report
return reportGenerator.generate({ data: testResult });
};
Expand Down
45 changes: 32 additions & 13 deletions src/reportGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const sorting = require('./sorting');
class ReportGenerator {
constructor(config) {
this.config = config;
this.consoleLogs = null;
}

/**
Expand Down Expand Up @@ -84,7 +85,11 @@ class ReportGenerator {
* @param {String} stylesheetPath Optional path to an external stylesheet
* @return {xmlbuilder}
*/
renderHtmlReport({ data, stylesheet, stylesheetPath }) {
renderHtmlReport({
data,
stylesheet,
stylesheetPath,
}) {
return new Promise((resolve, reject) => {
// Make sure that test data was provided
if (!data) { return reject(new Error('Test data missing or malformed')); }
Expand Down Expand Up @@ -211,18 +216,32 @@ class ReportGenerator {
testTr.ele('td', { class: 'result' }, (test.status === 'passed') ? `${test.status} in ${test.duration / 1000}s` : test.status);
});

// Test Suite console.logs
if (suite.console && suite.console.length > 0 && (this.config.shouldIncludeConsoleLog())) {
// Console Log Container
const consoleLogContainer = reportBody.ele('div', { class: 'suite-consolelog' });
// Console Log Header
consoleLogContainer.ele('div', { class: 'suite-consolelog-header' }, 'Console Log');
// Logs
suite.console.forEach((log) => {
const logElement = consoleLogContainer.ele('div', { class: 'suite-consolelog-item' });
logElement.ele('pre', { class: 'suite-consolelog-item-origin' }, stripAnsi(log.origin));
logElement.ele('pre', { class: 'suite-consolelog-item-message' }, stripAnsi(log.message));
});
// All console.logs caught during the test run
if (this.consoleLogs && this.consoleLogs.length > 0 && (this.config.shouldIncludeConsoleLog())) {
// Filter out the logs for this test file path
const filteredConsoleLogs = this.consoleLogs.find(logs => logs.testFilePath === suite.testFilePath);
if (filteredConsoleLogs && filteredConsoleLogs.logs.length > 0) {
// Console Log Container
const consoleLogContainer = reportBody.ele('div', { class: 'suite-consolelog' });
// Console Log Header
consoleLogContainer.ele('div', { class: 'suite-consolelog-header' }, 'Console Log');
// Sort the order by the path
const sortedConsoleLogs = filteredConsoleLogs.logs.sort((a, b) => {
if (a.origin < b.origin) {
return -1;
}
if (a.origin > b.origin) {
return 1;
}
return 0;
});
// Apply the logs to the body
sortedConsoleLogs.forEach((log) => {
const logElement = consoleLogContainer.ele('div', { class: 'suite-consolelog-item' });
logElement.ele('pre', { class: 'suite-consolelog-item-origin' }, stripAnsi(log.origin));
logElement.ele('pre', { class: 'suite-consolelog-item-message' }, stripAnsi(log.message));
});
}
}
});

Expand Down

0 comments on commit 61e47ca

Please sign in to comment.