Skip to content

Commit

Permalink
Calculate overall time from actual run time
Browse files Browse the repository at this point in the history
Instead of summing up the individual test suite which can be significantly larger and not accurate
  • Loading branch information
Nicklas Gummesson committed May 14, 2018
1 parent 3cf1caa commit cac8f9b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
2 changes: 1 addition & 1 deletion __tests__/__snapshots__/buildJsonResults.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Object {
"failures": 0,
"name": "jest tests",
"tests": 2,
"time": 0.236,
"time": 1.234,
},
},
Object {
Expand Down
4 changes: 4 additions & 0 deletions __tests__/buildJsonResults.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
12 changes: 9 additions & 3 deletions utils/buildJsonResults.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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())
}
}
]
Expand Down Expand Up @@ -59,7 +66,7 @@ module.exports = function (report, appDirectory, options) {

// Add <testsuite /> 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': [{
Expand All @@ -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) => {
Expand Down

0 comments on commit cac8f9b

Please sign in to comment.