diff --git a/lib/internal/test_runner/test.js b/lib/internal/test_runner/test.js
index e426438faba75f..a66efcf66a8d5d 100644
--- a/lib/internal/test_runner/test.js
+++ b/lib/internal/test_runner/test.js
@@ -1,5 +1,6 @@
'use strict';
const {
+ ArrayPrototypeJoin,
ArrayPrototypePush,
ArrayPrototypePushApply,
ArrayPrototypeShift,
@@ -1366,6 +1367,17 @@ class Test extends AsyncResource {
details.passed_on_attempt = this.passedAttempt;
}
+ // Generate classname from suite hierarchy for JUnit reporter
+ if (this.parent && this.parent !== this.root) {
+ const parts = [];
+ for (let t = this.parent; t !== t.root; t = t.parent) {
+ ArrayPrototypeUnshift(parts, t.name);
+ }
+ if (parts.length > 0) {
+ details.classname = ArrayPrototypeJoin(parts, '.');
+ }
+ }
+
return { __proto__: null, details, directive };
}
diff --git a/lib/internal/test_runner/tests_stream.js b/lib/internal/test_runner/tests_stream.js
index 7b64487696f53f..5f3ba2dbde232d 100644
--- a/lib/internal/test_runner/tests_stream.js
+++ b/lib/internal/test_runner/tests_stream.js
@@ -41,6 +41,7 @@ class TestsStream extends Readable {
nesting,
testNumber,
details,
+ ...(details.classname && { __proto__: null, classname: details.classname }),
...loc,
...directive,
});
@@ -53,6 +54,7 @@ class TestsStream extends Readable {
nesting,
testNumber,
details,
+ ...(details.classname && { __proto__: null, classname: details.classname }),
...loc,
...directive,
});
diff --git a/test/fixtures/test-runner/output/junit_classname_hierarchy.js b/test/fixtures/test-runner/output/junit_classname_hierarchy.js
new file mode 100644
index 00000000000000..8b634be39380e7
--- /dev/null
+++ b/test/fixtures/test-runner/output/junit_classname_hierarchy.js
@@ -0,0 +1,19 @@
+'use strict';
+require('../../../common');
+const { suite, test } = require('node:test');
+
+suite('Math', () => {
+ suite('Addition', () => {
+ test('adds positive numbers', () => {});
+ });
+
+ suite('Multiplication', () => {
+ test('multiplies positive numbers', () => {});
+ });
+});
+
+suite('String', () => {
+ test('concatenates strings', () => {});
+});
+
+test('standalone test', () => {});
diff --git a/test/fixtures/test-runner/output/junit_classname_hierarchy.snapshot b/test/fixtures/test-runner/output/junit_classname_hierarchy.snapshot
new file mode 100644
index 00000000000000..8b645b067d2dc5
--- /dev/null
+++ b/test/fixtures/test-runner/output/junit_classname_hierarchy.snapshot
@@ -0,0 +1,23 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/test/fixtures/test-runner/output/junit_reporter.snapshot b/test/fixtures/test-runner/output/junit_reporter.snapshot
index a24dc3646e4a30..ab6feb99a15d52 100644
--- a/test/fixtures/test-runner/output/junit_reporter.snapshot
+++ b/test/fixtures/test-runner/output/junit_reporter.snapshot
@@ -163,7 +163,7 @@ true !== false
-
+
Error [ERR_TEST_FAILURE]: thrown from subtest sync throw fail
*
@@ -192,15 +192,15 @@ Error [ERR_TEST_FAILURE]: thrown from subtest sync throw fail
-
-
-
-
+
+
+
+
-
+
-
+
@@ -317,9 +317,9 @@ Error [ERR_TEST_FAILURE]: thrown from callback async throw
-
-
-
+
+
+
@@ -339,7 +339,7 @@ Error [ERR_TEST_FAILURE]: thrown from callback async throw
-
+
Error [ERR_TEST_FAILURE]: thrown from subtest sync throw fails at first
*
@@ -360,7 +360,7 @@ Error [ERR_TEST_FAILURE]: thrown from subtest sync throw fails at first
}
-
+
Error [ERR_TEST_FAILURE]: thrown from subtest sync throw fails at second
* {
diff --git a/test/parallel/test-runner-reporters.js b/test/parallel/test-runner-reporters.js
index 50a47578a1da7e..63dac1346c90ef 100644
--- a/test/parallel/test-runner-reporters.js
+++ b/test/parallel/test-runner-reporters.js
@@ -201,7 +201,7 @@ describe('node:test reporters', { concurrency: true }, () => {
const fileContents = fs.readFileSync(file, 'utf8');
assert.match(fileContents, //);
assert.match(fileContents, /\s*/);
- assert.match(fileContents, //);
+ assert.match(fileContents, //);
assert.match(fileContents, //);
});
});
diff --git a/test/test-runner/test-output-junit-classname-hierarchy.mjs b/test/test-runner/test-output-junit-classname-hierarchy.mjs
new file mode 100644
index 00000000000000..737cefd89ecae2
--- /dev/null
+++ b/test/test-runner/test-output-junit-classname-hierarchy.mjs
@@ -0,0 +1,12 @@
+// Test that the output of test-runner/output/junit_classname_hierarchy.js matches
+// test-runner/output/junit_classname_hierarchy.snapshot
+import '../common/index.mjs';
+import * as fixtures from '../common/fixtures.mjs';
+import { spawnAndAssert, junitTransform, ensureCwdIsProjectRoot } from '../common/assertSnapshot.js';
+
+ensureCwdIsProjectRoot();
+await spawnAndAssert(
+ fixtures.path('test-runner/output/junit_classname_hierarchy.js'),
+ junitTransform,
+ { flags: ['--test-reporter=junit'] },
+);