From b0cbd1359a434a561667f54b2a9d4cdaf1724941 Mon Sep 17 00:00:00 2001 From: Johan Hargne Date: Tue, 5 Nov 2019 13:43:16 +0100 Subject: [PATCH] Added manual catch of console logs, Changed default run type to Custom Reporter --- jest.config.json | 12 +++++++++- jesthtmlreporter.config.json | 8 ------- package-lock.json | 14 +++++------ package.json | 2 +- src/index.js | 13 +++++++++++ src/reportGenerator.js | 45 +++++++++++++++++++++++++----------- 6 files changed, 64 insertions(+), 30 deletions(-) delete mode 100644 jesthtmlreporter.config.json diff --git a/jest.config.json b/jest.config.json index 5186054..6b5abe0 100644 --- a/jest.config.json +++ b/jest.config.json @@ -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" + }] + ] } diff --git a/jesthtmlreporter.config.json b/jesthtmlreporter.config.json deleted file mode 100644 index 81bef2f..0000000 --- a/jesthtmlreporter.config.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "pageTitle": "Test Suite Reporter", - "outputPath": "testResultsProcessorResult.html", - "includeFailureMsg": true, - "includeConsoleLog": true, - "useCssFile" : true, - "sort": "titleAsc" -} diff --git a/package-lock.json b/package-lock.json index 1daf100..d9577b0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "jest-html-reporter", - "version": "2.6.0", + "version": "2.7.0", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -677,9 +677,9 @@ }, "dependencies": { "handlebars": { - "version": "4.3.1", - "resolved": "https://registry.npmjs.org/handlebars/-/handlebars-4.3.1.tgz", - "integrity": "sha512-c0HoNHzDiHpBt4Kqe99N8tdLPKAnGCQ73gYMPWtAYM4PwGnf7xl8PBUHJqh9ijlzt2uQKaSRxbXRt+rZ7M2/kA==", + "version": "4.5.1", + "resolved": "https://registry.npmjs.org/handlebars/-/handlebars-4.5.1.tgz", + "integrity": "sha512-C29UoFzHe9yM61lOsIlCE5/mQVGrnIOrOq7maQl76L7tYPCgC1og0Ajt6uWnX4ZTxBPnjw+CUvawphwCfJgUnA==", "dev": true, "requires": { "neo-async": "^2.6.0", @@ -3792,9 +3792,9 @@ "dev": true }, "handlebars": { - "version": "4.3.1", - "resolved": "https://registry.npmjs.org/handlebars/-/handlebars-4.3.1.tgz", - "integrity": "sha512-c0HoNHzDiHpBt4Kqe99N8tdLPKAnGCQ73gYMPWtAYM4PwGnf7xl8PBUHJqh9ijlzt2uQKaSRxbXRt+rZ7M2/kA==", + "version": "4.5.1", + "resolved": "https://registry.npmjs.org/handlebars/-/handlebars-4.5.1.tgz", + "integrity": "sha512-C29UoFzHe9yM61lOsIlCE5/mQVGrnIOrOq7maQl76L7tYPCgC1og0Ajt6uWnX4ZTxBPnjw+CUvawphwCfJgUnA==", "dev": true, "requires": { "neo-async": "^2.6.0", diff --git a/package.json b/package.json index e5eb17d..360f084 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/index.js b/src/index.js index 016fd1a..16f9d9c 100644 --- a/src/index.js +++ b/src/index.js @@ -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 }); }; diff --git a/src/reportGenerator.js b/src/reportGenerator.js index 79d1cf8..033e578 100644 --- a/src/reportGenerator.js +++ b/src/reportGenerator.js @@ -8,6 +8,7 @@ const sorting = require('./sorting'); class ReportGenerator { constructor(config) { this.config = config; + this.consoleLogs = null; } /** @@ -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')); } @@ -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)); + }); + } } });