diff --git a/lib/internal/test_runner/test.js b/lib/internal/test_runner/test.js index c7cf9e36129d96..75a3a1558924b1 100644 --- a/lib/internal/test_runner/test.js +++ b/lib/internal/test_runner/test.js @@ -142,6 +142,7 @@ class TestContext { class Test extends AsyncResource { #abortController; #outerSignal; + #reportedSubtest; constructor(options) { super('Test'); @@ -308,7 +309,7 @@ class Test extends AsyncResource { } if (i === 1 && this.parent !== null) { - this.reporter.subtest(this.indent, this.name); + this.reportSubtest(); } // Report the subtest's results and remove it from the ready map. @@ -631,12 +632,6 @@ class Test extends AsyncResource { this.processReadySubtestRange(true); // Output this test's results and update the parent's waiting counter. - if (this.subtests.length > 0) { - this.reporter.plan(this.subtests[0].indent, this.subtests.length); - } else { - this.reporter.subtest(this.indent, this.name); - } - this.report(); this.parent.waitingOn++; this.finished = true; @@ -648,6 +643,11 @@ class Test extends AsyncResource { } report() { + if (this.subtests.length > 0) { + this.reporter.plan(this.subtests[0].indent, this.subtests.length); + } else { + this.reportSubtest(); + } let directive; if (this.skipped) { @@ -666,6 +666,15 @@ class Test extends AsyncResource { this.reporter.diagnostic(this.indent, this.diagnostics[i]); } } + + reportSubtest() { + if (this.#reportedSubtest || this.parent === null) { + return; + } + this.#reportedSubtest = true; + this.parent.reportSubtest(); + this.reporter.subtest(this.indent, this.name); + } } class TestHook extends Test { diff --git a/test/message/test_runner_describe_nested.js b/test/message/test_runner_describe_nested.js new file mode 100644 index 00000000000000..64819d62f7ebd0 --- /dev/null +++ b/test/message/test_runner_describe_nested.js @@ -0,0 +1,10 @@ +// Flags: --no-warnings +'use strict'; +require('../common'); +const { describe, it } = require('node:test'); + +describe('nested - no tests', () => { + describe('nested', () => { + it('nested', () => {}); + }); +}); diff --git a/test/message/test_runner_describe_nested.out b/test/message/test_runner_describe_nested.out new file mode 100644 index 00000000000000..1d3fe31b75c37a --- /dev/null +++ b/test/message/test_runner_describe_nested.out @@ -0,0 +1,26 @@ +TAP version 13 +# Subtest: nested - no tests + # Subtest: nested + # Subtest: nested + ok 1 - nested + --- + duration_ms: * + ... + 1..1 + ok 1 - nested + --- + duration_ms: * + ... + 1..1 +ok 1 - nested - no tests + --- + duration_ms: * + ... +1..1 +# tests 1 +# pass 1 +# fail 0 +# cancelled 0 +# skipped 0 +# todo 0 +# duration_ms *