Skip to content

Commit d445f84

Browse files
committed
test_runner: include stack of uncaught exceptions
PR-URL: nodejs#44614 Backport-PR-URL: nodejs#44873 Fixes: nodejs#44611 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
1 parent c5db7f0 commit d445f84

File tree

5 files changed

+49
-7
lines changed

5 files changed

+49
-7
lines changed

lib/internal/test_runner/tap_stream.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ function jsToYaml(indent, name, value) {
173173
}
174174

175175
if (isErrorObj) {
176-
const { kTestCodeFailure, kHookFailure } = lazyLoadTest();
176+
const { kTestCodeFailure, kUnwrapErrors } = lazyLoadTest();
177177
const {
178178
cause,
179179
code,
@@ -187,7 +187,7 @@ function jsToYaml(indent, name, value) {
187187

188188
// If the ERR_TEST_FAILURE came from an error provided by user code,
189189
// then try to unwrap the original error message and stack.
190-
if (code === 'ERR_TEST_FAILURE' && (failureType === kTestCodeFailure || failureType === kHookFailure)) {
190+
if (code === 'ERR_TEST_FAILURE' && kUnwrapErrors.has(failureType)) {
191191
errStack = cause?.stack ?? errStack;
192192
errCode = cause?.code ?? errCode;
193193
if (failureType === kTestCodeFailure) {

lib/internal/test_runner/test.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const {
1313
PromiseResolve,
1414
ReflectApply,
1515
SafeMap,
16+
SafeSet,
1617
SafePromiseAll,
1718
SafePromiseRace,
1819
Symbol,
@@ -60,6 +61,9 @@ const testOnlyFlag = !isTestRunner && getOptionValue('--test-only');
6061
const kShouldAbort = Symbol('kShouldAbort');
6162
const kRunHook = Symbol('kRunHook');
6263
const kHookNames = ObjectSeal(['before', 'after', 'beforeEach', 'afterEach']);
64+
const kUnwrapErrors = new SafeSet()
65+
.add(kTestCodeFailure).add(kHookFailure)
66+
.add('uncaughtException').add('unhandledRejection');
6367

6468

6569
function stopTest(timeout, signal) {
@@ -726,9 +730,9 @@ module.exports = {
726730
ItTest,
727731
kCancelledByParent,
728732
kDefaultIndent,
729-
kHookFailure,
730733
kSubtestsFailed,
731734
kTestCodeFailure,
735+
kUnwrapErrors,
732736
Suite,
733737
Test,
734738
};

test/message/test_runner_describe_it.out

+1
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,7 @@ not ok 49 - callback async throw
418418
code: 'ERR_TEST_FAILURE'
419419
stack: |-
420420
*
421+
*
421422
...
422423
# Subtest: callback async throw after done
423424
ok 50 - callback async throw after done

test/message/test_runner_output.js

+12
Original file line numberDiff line numberDiff line change
@@ -371,3 +371,15 @@ test('rejected thenable', () => {
371371
},
372372
};
373373
});
374+
375+
test('unfinished test with uncaughtException', async () => {
376+
await new Promise(() => {
377+
setTimeout(() => { throw new Error('foo'); });
378+
});
379+
});
380+
381+
test('unfinished test with unhandledRejection', async () => {
382+
await new Promise(() => {
383+
setTimeout(() => Promise.reject(new Error('bar')));
384+
});
385+
});

test/message/test_runner_output.out

+29-4
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,7 @@ not ok 51 - callback async throw
463463
code: 'ERR_TEST_FAILURE'
464464
stack: |-
465465
*
466+
*
466467
...
467468
# Subtest: callback async throw after done
468469
ok 52 - callback async throw after done
@@ -601,8 +602,32 @@ not ok 62 - rejected thenable
601602
error: 'custom error'
602603
code: 'ERR_TEST_FAILURE'
603604
...
605+
# Subtest: unfinished test with uncaughtException
606+
not ok 63 - unfinished test with uncaughtException
607+
---
608+
duration_ms: *
609+
failureType: 'uncaughtException'
610+
error: 'foo'
611+
code: 'ERR_TEST_FAILURE'
612+
stack: |-
613+
*
614+
*
615+
*
616+
...
617+
# Subtest: unfinished test with unhandledRejection
618+
not ok 64 - unfinished test with unhandledRejection
619+
---
620+
duration_ms: *
621+
failureType: 'unhandledRejection'
622+
error: 'bar'
623+
code: 'ERR_TEST_FAILURE'
624+
stack: |-
625+
*
626+
*
627+
*
628+
...
604629
# Subtest: invalid subtest fail
605-
not ok 63 - invalid subtest fail
630+
not ok 65 - invalid subtest fail
606631
---
607632
duration_ms: *
608633
failureType: 'parentAlreadyFinished'
@@ -611,16 +636,16 @@ not ok 63 - invalid subtest fail
611636
stack: |-
612637
*
613638
...
614-
1..63
639+
1..65
615640
# 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.
616641
# 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.
617642
# 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.
618643
# 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.
619644
# 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.
620645
# Warning: Test "callback async throw after done" generated asynchronous activity after the test ended. This activity created the error "Error: thrown from callback async throw after done" and would have caused the test to fail, but instead triggered an uncaughtException event.
621-
# tests 63
646+
# tests 65
622647
# pass 27
623-
# fail 19
648+
# fail 21
624649
# cancelled 2
625650
# skipped 10
626651
# todo 5

0 commit comments

Comments
 (0)