Skip to content

Commit ecee4c4

Browse files
rluvatonCeres6
authored andcommitted
test_runner: fix async callback in describe not awaited
PR-URL: nodejs#48856 Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
1 parent 593e084 commit ecee4c4

File tree

3 files changed

+67
-12
lines changed

3 files changed

+67
-12
lines changed

lib/internal/test_runner/test.js

+14-7
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const {
1313
ObjectSeal,
1414
PromisePrototypeThen,
1515
PromiseResolve,
16+
SafePromisePrototypeFinally,
1617
ReflectApply,
1718
RegExpPrototypeExec,
1819
SafeMap,
@@ -789,17 +790,23 @@ class Suite extends Test {
789790
const { ctx, args } = this.getRunArgs();
790791
const runArgs = [this.fn, ctx];
791792
ArrayPrototypePushApply(runArgs, args);
792-
this.buildSuite = PromisePrototypeThen(
793-
PromiseResolve(ReflectApply(this.runInAsyncScope, this, runArgs)),
794-
undefined,
795-
(err) => {
796-
this.fail(new ERR_TEST_FAILURE(err, kTestCodeFailure));
797-
});
793+
this.buildSuite = SafePromisePrototypeFinally(
794+
PromisePrototypeThen(
795+
PromiseResolve(ReflectApply(this.runInAsyncScope, this, runArgs)),
796+
undefined,
797+
(err) => {
798+
this.fail(new ERR_TEST_FAILURE(err, kTestCodeFailure));
799+
}),
800+
() => {
801+
this.buildPhaseFinished = true;
802+
},
803+
);
798804
} catch (err) {
799805
this.fail(new ERR_TEST_FAILURE(err, kTestCodeFailure));
806+
807+
this.buildPhaseFinished = true;
800808
}
801809
this.fn = () => {};
802-
this.buildPhaseFinished = true;
803810
}
804811

805812
getRunArgs() {

test/fixtures/test-runner/output/describe_it.js

+19
Original file line numberDiff line numberDiff line change
@@ -375,3 +375,22 @@ describe('rejected thenable', () => {
375375
},
376376
};
377377
});
378+
379+
describe("async describe function", async () => {
380+
await null;
381+
382+
await it("it inside describe 1", async () => {
383+
await null
384+
});
385+
await it("it inside describe 2", async () => {
386+
await null;
387+
});
388+
389+
describe("inner describe", async () => {
390+
await null;
391+
392+
it("it inside inner describe", async () => {
393+
await null;
394+
});
395+
});
396+
});

test/fixtures/test-runner/output/describe_it.snapshot

+34-5
Original file line numberDiff line numberDiff line change
@@ -635,8 +635,37 @@ not ok 59 - rejected thenable
635635
stack: |-
636636
*
637637
...
638+
# Subtest: async describe function
639+
# Subtest: it inside describe 1
640+
ok 1 - it inside describe 1
641+
---
642+
duration_ms: *
643+
...
644+
# Subtest: it inside describe 2
645+
ok 2 - it inside describe 2
646+
---
647+
duration_ms: *
648+
...
649+
# Subtest: inner describe
650+
# Subtest: it inside inner describe
651+
ok 1 - it inside inner describe
652+
---
653+
duration_ms: *
654+
...
655+
1..1
656+
ok 3 - inner describe
657+
---
658+
duration_ms: *
659+
type: 'suite'
660+
...
661+
1..3
662+
ok 60 - async describe function
663+
---
664+
duration_ms: *
665+
type: 'suite'
666+
...
638667
# Subtest: invalid subtest fail
639-
not ok 60 - invalid subtest fail
668+
not ok 61 - invalid subtest fail
640669
---
641670
duration_ms: *
642671
failureType: 'parentAlreadyFinished'
@@ -645,16 +674,16 @@ not ok 60 - invalid subtest fail
645674
stack: |-
646675
*
647676
...
648-
1..60
677+
1..61
649678
# 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.
650679
# 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.
651680
# 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.
652681
# 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.
653682
# 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.
654683
# 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.
655-
# tests 67
656-
# suites 9
657-
# pass 29
684+
# tests 70
685+
# suites 11
686+
# pass 32
658687
# fail 19
659688
# cancelled 4
660689
# skipped 10

0 commit comments

Comments
 (0)