From 4419ce625ad8dfdb8dde3c3c25ffbcd80b9834c0 Mon Sep 17 00:00:00 2001 From: Timo Tijhof Date: Wed, 10 Jul 2024 21:32:47 +0100 Subject: [PATCH] HTML Reporter: Fix formatting of 951-999ms as plural "1 seconds" We only checked for 1 equality in the branch for >1000, but toPrecision also returns "1" for numbers close but under it. Ref https://github.com/qunitjs/qunit/pull/1760. --- src/reporters/HtmlReporter.js | 21 +++++++++------------ test/main/HtmlReporter.js | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 12 deletions(-) diff --git a/src/reporters/HtmlReporter.js b/src/reporters/HtmlReporter.js index db9e6ce54..729b50cfe 100644 --- a/src/reporters/HtmlReporter.js +++ b/src/reporters/HtmlReporter.js @@ -723,12 +723,12 @@ export default class HtmlReporter { onRunEnd (runEnd) { function msToSec (milliseconds) { - if (milliseconds < 1000) { - // Will return e.g. "0.2", "0.03" or "0.004" - return (milliseconds / 1000).toPrecision(1) + ' seconds'; - } - const sec = Math.ceil(milliseconds / 1000); - return sec + (sec === 1 ? ' second' : ' seconds'); + // Will return a whole number of seconds, + // or e.g. "0.2", "0.03" or "0.004". + const sec = (milliseconds > 1000) + ? ('' + Math.round(milliseconds / 1000)) + : (milliseconds / 1000).toPrecision(1); + return sec + (sec === '1' ? ' second' : ' seconds'); } const abortButton = this.element.querySelector('#qunit-abort-tests-button'); @@ -742,20 +742,17 @@ export default class HtmlReporter { 'and ', runEnd.testCounts.todo, ' todo.', this.getRerunFailedHtml(this.stats.failedTests) ].join(''); - let test; - let assertLi; - let assertList; // Update remaining tests to aborted if (abortButton && abortButton.disabled) { html = 'Tests aborted after ' + msToSec(runEnd.runtime) + '.'; for (let i = 0; i < this.elementTests.children.length; i++) { - test = this.elementTests.children[i]; + const test = this.elementTests.children[i]; if (test.className === '' || test.className === 'running') { test.className = 'aborted'; - assertList = test.getElementsByTagName('ol')[0]; - assertLi = document.createElement('li'); + const assertList = test.getElementsByTagName('ol')[0]; + const assertLi = document.createElement('li'); assertLi.className = 'fail'; assertLi.textContent = 'Test aborted.'; assertList.appendChild(assertLi); diff --git a/test/main/HtmlReporter.js b/test/main/HtmlReporter.js index 29f57beb7..d6c8eae41 100644 --- a/test/main/HtmlReporter.js +++ b/test/main/HtmlReporter.js @@ -139,6 +139,40 @@ QUnit.test('testresult-display [testStart]', function (assert) { ); }); +QUnit.test.each('testresult-display [runEnd]', { + '3ms': [3, '0.003 seconds'], + '98ms': [98, '0.1 seconds'], + '149ms': [149, '0.1 seconds'], + '151ms': [151, '0.2 seconds'], + '850ms': [850, '0.8 seconds'], + '940ms': [940, '0.9 seconds'], + '960ms': [960, '1 second'], + '1010ms': [1010, '1 second'], + '1090ms': [1090, '1 second'], + '1590ms': [1590, '2 seconds'] +}, function (assert, data) { + var element = document.createElement('div'); + new QUnit.reporters.html(this.MockQUnit, { + element: element, + config: { + urlConfig: [] + } + }); + this.MockQUnit._do_mixed_run_half(); + this.MockQUnit.emit('runEnd', { + testCounts: { total: 5, passed: 4, failed: 0, skipped: 1, todo: 0 }, + status: 'passed', + runtime: data[0] + }); + + var display = element.querySelector('#qunit-testresult-display'); + assert.equal( + display.textContent, + '5 tests completed in ' + data[1] + '.4 passed, 1 skipped, 0 failed, and 0 todo.', + 'display text' + ); +}); + QUnit.test('test-output trace', function (assert) { var element = document.createElement('div'); new QUnit.reporters.html(this.MockQUnit, {