From 7385f398d39b42bf8c93ae8d09b6e73cbf394e16 Mon Sep 17 00:00:00 2001 From: Moshe Atlow Date: Sat, 30 Jul 2022 23:49:20 +0300 Subject: [PATCH] test_runner: verbous error when entire test tree is canceled --- lib/internal/test_runner/harness.js | 6 ++++-- lib/internal/test_runner/test.js | 9 +++++---- test/message/test_runner_no_refs.out | 4 ++-- test/message/test_runner_unresolved_promise.js | 2 +- test/message/test_runner_unresolved_promise.out | 4 ++-- 5 files changed, 14 insertions(+), 11 deletions(-) diff --git a/lib/internal/test_runner/harness.js b/lib/internal/test_runner/harness.js index e9f0907c4d490d..5286ef678422d1 100644 --- a/lib/internal/test_runner/harness.js +++ b/lib/internal/test_runner/harness.js @@ -14,7 +14,7 @@ const { }, } = require('internal/errors'); const { getOptionValue } = require('internal/options'); -const { Test, ItTest, Suite } = require('internal/test_runner/test'); +const { kCancelledByParent, Test, ItTest, Suite } = require('internal/test_runner/test'); const isTestRunner = getOptionValue('--test'); const testResources = new SafeMap(); @@ -77,7 +77,9 @@ function setup(root) { createProcessEventHandler('unhandledRejection', root); const exitHandler = () => { - root.postRun(); + const msg = 'There is no more asynchronous activity queued in the event loop, ' + + 'thus the test runner has completed and canceled all outstanding pending tests' + root.postRun(new ERR_TEST_FAILURE(msg, kCancelledByParent)); let passCount = 0; let failCount = 0; diff --git a/lib/internal/test_runner/test.js b/lib/internal/test_runner/test.js index 3172d1ec2b40a5..e2d068e87bb051 100644 --- a/lib/internal/test_runner/test.js +++ b/lib/internal/test_runner/test.js @@ -511,7 +511,7 @@ class Test extends AsyncResource { this.postRun(); } - postRun() { + postRun(pendingSubtestsError) { let failedSubtests = 0; // If the test was failed before it even started, then the end time will @@ -523,12 +523,13 @@ class Test extends AsyncResource { // The test has run, so recursively cancel any outstanding subtests and // mark this test as failed if any subtests failed. + this.pendingSubtests = []; for (let i = 0; i < this.subtests.length; i++) { const subtest = this.subtests[i]; if (!subtest.finished) { - subtest.cancel(); - subtest.postRun(); + subtest.cancel(pendingSubtestsError); + subtest.postRun(pendingSubtestsError); } if (!subtest.passed) { @@ -690,4 +691,4 @@ class Suite extends Test { } } -module.exports = { kDefaultIndent, kSubtestsFailed, kTestCodeFailure, Test, Suite, ItTest }; +module.exports = { kCancelledByParent, kDefaultIndent, kSubtestsFailed, kTestCodeFailure, Test, Suite, ItTest }; diff --git a/test/message/test_runner_no_refs.out b/test/message/test_runner_no_refs.out index 63b79cd57d777f..ce698a49e59ca8 100644 --- a/test/message/test_runner_no_refs.out +++ b/test/message/test_runner_no_refs.out @@ -5,7 +5,7 @@ TAP version 13 --- duration_ms: * failureType: 'cancelledByParent' - error: 'test did not finish before its parent and was cancelled' + error: 'There is no more asynchronous activity queued in the event loop, thus the test runner has completed and canceled all outstanding pending tests' code: 'ERR_TEST_FAILURE' stack: |- * @@ -15,7 +15,7 @@ not ok 1 - does not keep event loop alive --- duration_ms: * failureType: 'cancelledByParent' - error: 'test did not finish before its parent and was cancelled' + error: 'There is no more asynchronous activity queued in the event loop, thus the test runner has completed and canceled all outstanding pending tests' code: 'ERR_TEST_FAILURE' stack: |- * diff --git a/test/message/test_runner_unresolved_promise.js b/test/message/test_runner_unresolved_promise.js index 00d231be34b6df..a36652669fc0a9 100644 --- a/test/message/test_runner_unresolved_promise.js +++ b/test/message/test_runner_unresolved_promise.js @@ -5,4 +5,4 @@ const test = require('node:test'); test('pass'); test('never resolving promise', () => new Promise(() => {})); -test('fail'); +test('fail', () => console.log('this should not appear')); diff --git a/test/message/test_runner_unresolved_promise.out b/test/message/test_runner_unresolved_promise.out index d4e868cc9ef3c3..354ffc5bf81168 100644 --- a/test/message/test_runner_unresolved_promise.out +++ b/test/message/test_runner_unresolved_promise.out @@ -9,7 +9,7 @@ not ok 2 - never resolving promise --- duration_ms: * failureType: 'cancelledByParent' - error: 'test did not finish before its parent and was cancelled' + error: 'There is no more asynchronous activity queued in the event loop, thus the test runner has completed and canceled all outstanding pending tests' code: 'ERR_TEST_FAILURE' stack: |- * @@ -19,7 +19,7 @@ not ok 3 - fail --- duration_ms: * failureType: 'cancelledByParent' - error: 'test did not finish before its parent and was cancelled' + error: 'There is no more asynchronous activity queued in the event loop, thus the test runner has completed and canceled all outstanding pending tests' code: 'ERR_TEST_FAILURE' stack: |- *