From 8ff48453a8b12d9cacf56b0c0c544c8256af64c7 Mon Sep 17 00:00:00 2001
From: Mark Wiemer <7833360+mark-wiemer@users.noreply.github.com>
Date: Tue, 29 Oct 2024 11:00:24 -0700
Subject: [PATCH] feat: highlight browser failures (#5222)
* Format html.js
* Remove newlines between JSDoc comments and their symbols
* (temp) update template.html to run tests
* Color the stat result icon and text based on root suite result
* (temp) add template fail and template pass
* On first test fail, add fail indicator to stats
* Remove extra newline
* Format mocha.css with Prettier
* Make pass icon color accessible
* Cleanup mocha.css var names
* Restore template to main
* Remove thirteen-year-old comment
https://github.com/mochajs/mocha/commit/440e38cec946826db9e2089c21f570ad3abae852
* Revert "Format html.js"
This reverts commit 8552a91de1508d59b44e2b80cf7809030ac8bef5.
* Revert "Restore template to main"
This reverts commit 854ab12182fb738903cb1383a416d06169a21304.
* Color passes and failures count as well
* Cleanup stats variables to consts
* Fix stats not updating on root suite end
* Revert "Remove extra newline"
This reverts commit 27a79585eee5a60a5dec67a8f27f9c62543921bb.
* Restore newlines :(
* Restore newlines for real
* Reapply "Remove extra newline"
This reverts commit c43bffeae50663f69b2b35e8a84bf3af2dfba9b0.
* Revert "Format mocha.css with Prettier"
This reverts commit 0898ed586c3a39e6ea73a902275c440b8e04461f.
* indent to 5, not 6 chars in mocha.css
* Re-add newline in mocha.css
* Reapply "Restore template to main"
This reverts commit 714ee1538b6089b4c00fe5daaf523c5985d87e25.
---
lib/reporters/html.js | 51 +++++++++++++++++++++++++++----------
mocha.css | 58 ++++++++++++++++++++++++++++++-------------
2 files changed, 79 insertions(+), 30 deletions(-)
diff --git a/lib/reporters/html.js b/lib/reporters/html.js
index ae4a4546f8..c970fc05e9 100644
--- a/lib/reporters/html.js
+++ b/lib/reporters/html.js
@@ -32,11 +32,12 @@ var Date = global.Date;
exports = module.exports = HTML;
/**
- * Stats template.
+ * Stats template: Result, progress, passes, failures, and duration.
*/
var statsTemplate =
'
' +
@@ -62,18 +63,35 @@ function HTML(runner, options) {
var stats = this.stats;
var stat = fragment(statsTemplate);
var items = stat.getElementsByTagName('li');
- var passes = items[1].getElementsByTagName('em')[0];
- var passesLink = items[1].getElementsByTagName('a')[0];
- var failures = items[2].getElementsByTagName('em')[0];
- var failuresLink = items[2].getElementsByTagName('a')[0];
- var duration = items[3].getElementsByTagName('em')[0];
+ const resultIndex = 0;
+ const progressIndex = 1;
+ const passesIndex = 2;
+ const failuresIndex = 3;
+ const durationIndex = 4;
+ /** Stat item containing the root suite pass or fail indicator (hasFailures ? '✖' : '✓') */
+ var resultIndicator = items[resultIndex];
+ /** Passes text and count */
+ const passesStat = items[passesIndex];
+ /** Stat item containing the pass count (not the word, just the number) */
+ const passesCount = passesStat.getElementsByTagName('em')[0];
+ /** Stat item linking to filter to show only passing tests */
+ const passesLink = passesStat.getElementsByTagName('a')[0];
+ /** Failures text and count */
+ const failuresStat = items[failuresIndex];
+ /** Stat item containing the failure count (not the word, just the number) */
+ const failuresCount = failuresStat.getElementsByTagName('em')[0];
+ /** Stat item linking to filter to show only failing tests */
+ const failuresLink = failuresStat.getElementsByTagName('a')[0];
+ /** Stat item linking to the duration time (not the word or unit, just the number) */
+ var duration = items[durationIndex].getElementsByTagName('em')[0];
var report = fragment('
');
var stack = [report];
- var progressText = items[0].getElementsByTagName('div')[0];
- var progressBar = items[0].getElementsByTagName('progress')[0];
+ var progressText = items[progressIndex].getElementsByTagName('div')[0];
+ var progressBar = items[progressIndex].getElementsByTagName('progress')[0];
var progressRing = [
- items[0].getElementsByClassName('ring-flatlight')[0],
- items[0].getElementsByClassName('ring-highlight')[0]];
+ items[progressIndex].getElementsByClassName('ring-flatlight')[0],
+ items[progressIndex].getElementsByClassName('ring-highlight')[0]
+ ];
var progressRingRadius = null; // computed CSS unavailable now, so set later
var root = document.getElementById('mocha');
@@ -127,6 +145,10 @@ function HTML(runner, options) {
runner.on(EVENT_SUITE_END, function (suite) {
if (suite.root) {
+ if (stats.failures === 0) {
+ text(resultIndicator, '✓');
+ stat.className += ' pass';
+ }
updateStats();
return;
}
@@ -147,6 +169,10 @@ function HTML(runner, options) {
});
runner.on(EVENT_TEST_FAIL, function (test) {
+ // Update stat items
+ text(resultIndicator, '✖');
+ stat.className += ' fail';
+
var el = fragment(
'