From cac8f9bf21b84bdc8f652f39d3a6c437e21de4fe Mon Sep 17 00:00:00 2001 From: Nicklas Gummesson Date: Mon, 14 May 2018 15:33:42 -0700 Subject: [PATCH] Calculate overall time from actual run time Instead of summing up the individual test suite which can be significantly larger and not accurate --- .../__snapshots__/buildJsonResults.test.js.snap | 2 +- __tests__/buildJsonResults.test.js | 4 ++++ utils/buildJsonResults.js | 12 +++++++++--- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/__tests__/__snapshots__/buildJsonResults.test.js.snap b/__tests__/__snapshots__/buildJsonResults.test.js.snap index f5dcba4..5e8a628 100644 --- a/__tests__/__snapshots__/buildJsonResults.test.js.snap +++ b/__tests__/__snapshots__/buildJsonResults.test.js.snap @@ -8,7 +8,7 @@ Object { "failures": 0, "name": "jest tests", "tests": 2, - "time": 0.236, + "time": 1.234, }, }, Object { diff --git a/__tests__/buildJsonResults.test.js b/__tests__/buildJsonResults.test.js index 0c3985a..6cb99b7 100644 --- a/__tests__/buildJsonResults.test.js +++ b/__tests__/buildJsonResults.test.js @@ -102,6 +102,10 @@ describe('buildJsonResults', () => { it('should support displayName template var for jest multi-project', () => { const multiProjectNoFailingTestsReport = require('../__mocks__/multi-project-no-failing-tests.json'); + // Mock Date.now() to return a fixed later value + const startDate = new Date(multiProjectNoFailingTestsReport.startTime); + spyOn(Date, 'now').and.returnValue(startDate.getTime() + 1234); + const jsonResults = buildJsonResults(multiProjectNoFailingTestsReport, '', Object.assign({}, constants.DEFAULT_OPTIONS, { suiteNameTemplate: "{displayName}-foo", diff --git a/utils/buildJsonResults.js b/utils/buildJsonResults.js index 3b54169..414d2c1 100644 --- a/utils/buildJsonResults.js +++ b/utils/buildJsonResults.js @@ -13,6 +13,10 @@ const replaceVars = function (str, replacementMap) { return str; }; +const executionTime = function (startTime, endTime) { + return (endTime - startTime) / 1000; +} + module.exports = function (report, appDirectory, options) { // Generate a single XML file for all jest tests let jsonResults = { @@ -22,7 +26,10 @@ module.exports = function (report, appDirectory, options) { 'name': options.suiteName, 'tests': 0, 'failures': 0, - 'time': 0 + // Overall execution time: + // Since tests are typically executed in parallel this time can be significantly smaller + // than the sum of the individual test suites + 'time': executionTime(report.startTime, Date.now()) } } ] @@ -59,7 +66,7 @@ module.exports = function (report, appDirectory, options) { // Add properties const suiteNumTests = suite.numFailingTests + suite.numPassingTests + suite.numPendingTests; - const suiteExecutionTime = (suite.perfStats.end - suite.perfStats.start) / 1000; + const suiteExecutionTime = executionTime(suite.perfStats.start, suite.perfStats.end); let testSuite = { 'testsuite': [{ @@ -78,7 +85,6 @@ module.exports = function (report, appDirectory, options) { // Update top level testsuites properties jsonResults.testsuites[0]._attr.failures += suite.numFailingTests; jsonResults.testsuites[0]._attr.tests += suiteNumTests; - jsonResults.testsuites[0]._attr.time += suiteExecutionTime; // Iterate through test cases suite.testResults.forEach((tc) => {