Skip to content

Commit 16db3ad

Browse files
cjihrigdanielleadams
authored andcommitted
test_runner: handle errors not bound to tests
This commit addresses a previously untested branch of the code. It is possible when using the test runner that an error occurs outside of a test. In this case, the test runner would simply rethrow the error. This commit updates the logic to handle the error in the same fashion as other uncaughtExceptions. PR-URL: #46962 Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
1 parent d60eef2 commit 16db3ad

File tree

5 files changed

+24
-11
lines changed

5 files changed

+24
-11
lines changed

lib/internal/test_runner/harness.js

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,21 @@ function createProcessEventHandler(eventName, rootTest) {
3939
// Check if this error is coming from a test. If it is, fail the test.
4040
const test = testResources.get(executionAsyncId());
4141

42-
if (!test) {
43-
throw err;
44-
}
45-
46-
if (test.finished) {
47-
// If the test is already finished, report this as a top level
48-
// diagnostic since this is a malformed test.
49-
const msg = `Warning: Test "${test.name}" generated asynchronous ` +
50-
'activity after the test ended. This activity created the error ' +
51-
`"${err}" and would have caused the test to fail, but instead ` +
52-
`triggered an ${eventName} event.`;
42+
if (!test || test.finished) {
43+
// If the test is already finished or the resource that created the error
44+
// is not mapped to a Test, report this as a top level diagnostic.
45+
let msg;
46+
47+
if (test) {
48+
msg = `Warning: Test "${test.name}" generated asynchronous ` +
49+
'activity after the test ended. This activity created the error ' +
50+
`"${err}" and would have caused the test to fail, but instead ` +
51+
`triggered an ${eventName} event.`;
52+
} else {
53+
msg = 'Warning: A resource generated asynchronous activity after ' +
54+
`the test ended. This activity created the error "${err}" which ` +
55+
`triggered an ${eventName} event, caught by the test runner.`;
56+
}
5357

5458
rootTest.diagnostic(msg);
5559
process.exitCode = 1;

test/message/test_runner_output.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,3 +383,9 @@ test('unfinished test with unhandledRejection', async () => {
383383
setTimeout(() => Promise.reject(new Error('bar')));
384384
});
385385
});
386+
387+
// Verify that uncaught exceptions outside of any tests are handled after the
388+
// test harness has finished bootstrapping itself.
389+
setImmediate(() => {
390+
throw new Error('uncaught from outside of a test');
391+
});

test/message/test_runner_output.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,7 @@ not ok 65 - invalid subtest fail
637637
1..65
638638
# Warning: Test "unhandled rejection - passes but warns" generated asynchronous activity after the test ended. This activity created the error "Error: rejected from unhandled rejection fail" and would have caused the test to fail, but instead triggered an unhandledRejection event.
639639
# Warning: Test "async unhandled rejection - passes but warns" generated asynchronous activity after the test ended. This activity created the error "Error: rejected from async unhandled rejection fail" and would have caused the test to fail, but instead triggered an unhandledRejection event.
640+
# Warning: A resource generated asynchronous activity after the test ended. This activity created the error "Error: uncaught from outside of a test" which triggered an uncaughtException event, caught by the test runner.
640641
# Warning: Test "immediate throw - passes but warns" generated asynchronous activity after the test ended. This activity created the error "Error: thrown from immediate throw fail" and would have caused the test to fail, but instead triggered an uncaughtException event.
641642
# Warning: Test "immediate reject - passes but warns" generated asynchronous activity after the test ended. This activity created the error "Error: rejected from immediate reject fail" and would have caused the test to fail, but instead triggered an unhandledRejection event.
642643
# Warning: Test "callback called twice in different ticks" generated asynchronous activity after the test ended. This activity created the error "Error [ERR_TEST_FAILURE]: callback invoked multiple times" and would have caused the test to fail, but instead triggered an uncaughtException event.

test/message/test_runner_output_cli.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,7 @@ not ok 65 - invalid subtest fail
636636
...
637637
# Warning: Test "unhandled rejection - passes but warns" generated asynchronous activity after the test ended. This activity created the error "Error: rejected from unhandled rejection fail" and would have caused the test to fail, but instead triggered an unhandledRejection event.
638638
# Warning: Test "async unhandled rejection - passes but warns" generated asynchronous activity after the test ended. This activity created the error "Error: rejected from async unhandled rejection fail" and would have caused the test to fail, but instead triggered an unhandledRejection event.
639+
# Warning: A resource generated asynchronous activity after the test ended. This activity created the error "Error: uncaught from outside of a test" which triggered an uncaughtException event, caught by the test runner.
639640
# Warning: Test "immediate throw - passes but warns" generated asynchronous activity after the test ended. This activity created the error "Error: thrown from immediate throw fail" and would have caused the test to fail, but instead triggered an uncaughtException event.
640641
# Warning: Test "immediate reject - passes but warns" generated asynchronous activity after the test ended. This activity created the error "Error: rejected from immediate reject fail" and would have caused the test to fail, but instead triggered an unhandledRejection event.
641642
# Warning: Test "callback called twice in different ticks" generated asynchronous activity after the test ended. This activity created the error "Error [ERR_TEST_FAILURE]: callback invoked multiple times" and would have caused the test to fail, but instead triggered an uncaughtException event.

test/message/test_runner_output_spec_reporter.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@
270270

271271
Warning: Test "unhandled rejection - passes but warns" generated asynchronous activity after the test ended. This activity created the error "Error: rejected from unhandled rejection fail" and would have caused the test to fail, but instead triggered an unhandledRejection event.
272272
Warning: Test "async unhandled rejection - passes but warns" generated asynchronous activity after the test ended. This activity created the error "Error: rejected from async unhandled rejection fail" and would have caused the test to fail, but instead triggered an unhandledRejection event.
273+
Warning: A resource generated asynchronous activity after the test ended. This activity created the error "Error: uncaught from outside of a test" which triggered an uncaughtException event, caught by the test runner.
273274
Warning: Test "immediate throw - passes but warns" generated asynchronous activity after the test ended. This activity created the error "Error: thrown from immediate throw fail" and would have caused the test to fail, but instead triggered an uncaughtException event.
274275
Warning: Test "immediate reject - passes but warns" generated asynchronous activity after the test ended. This activity created the error "Error: rejected from immediate reject fail" and would have caused the test to fail, but instead triggered an unhandledRejection event.
275276
Warning: Test "callback called twice in different ticks" generated asynchronous activity after the test ended. This activity created the error "Error [ERR_TEST_FAILURE]: callback invoked multiple times" and would have caused the test to fail, but instead triggered an uncaughtException event.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy