Skip to content

Commit

Permalink
test_runner: fix async callback in describe not awaited
Browse files Browse the repository at this point in the history
PR-URL: nodejs#48856
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
  • Loading branch information
rluvaton authored and pluris committed Aug 6, 2023
1 parent 364b948 commit 3a68c4b
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 12 deletions.
21 changes: 14 additions & 7 deletions lib/internal/test_runner/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const {
ObjectSeal,
PromisePrototypeThen,
PromiseResolve,
SafePromisePrototypeFinally,
ReflectApply,
RegExpPrototypeExec,
SafeMap,
Expand Down Expand Up @@ -789,17 +790,23 @@ class Suite extends Test {
const { ctx, args } = this.getRunArgs();
const runArgs = [this.fn, ctx];
ArrayPrototypePushApply(runArgs, args);
this.buildSuite = PromisePrototypeThen(
PromiseResolve(ReflectApply(this.runInAsyncScope, this, runArgs)),
undefined,
(err) => {
this.fail(new ERR_TEST_FAILURE(err, kTestCodeFailure));
});
this.buildSuite = SafePromisePrototypeFinally(
PromisePrototypeThen(
PromiseResolve(ReflectApply(this.runInAsyncScope, this, runArgs)),
undefined,
(err) => {
this.fail(new ERR_TEST_FAILURE(err, kTestCodeFailure));
}),
() => {
this.buildPhaseFinished = true;
},
);
} catch (err) {
this.fail(new ERR_TEST_FAILURE(err, kTestCodeFailure));

this.buildPhaseFinished = true;
}
this.fn = () => {};
this.buildPhaseFinished = true;
}

getRunArgs() {
Expand Down
19 changes: 19 additions & 0 deletions test/fixtures/test-runner/output/describe_it.js
Original file line number Diff line number Diff line change
Expand Up @@ -375,3 +375,22 @@ describe('rejected thenable', () => {
},
};
});

describe("async describe function", async () => {
await null;

await it("it inside describe 1", async () => {
await null
});
await it("it inside describe 2", async () => {
await null;
});

describe("inner describe", async () => {
await null;

it("it inside inner describe", async () => {
await null;
});
});
});
39 changes: 34 additions & 5 deletions test/fixtures/test-runner/output/describe_it.snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -635,8 +635,37 @@ not ok 59 - rejected thenable
stack: |-
*
...
# Subtest: async describe function
# Subtest: it inside describe 1
ok 1 - it inside describe 1
---
duration_ms: *
...
# Subtest: it inside describe 2
ok 2 - it inside describe 2
---
duration_ms: *
...
# Subtest: inner describe
# Subtest: it inside inner describe
ok 1 - it inside inner describe
---
duration_ms: *
...
1..1
ok 3 - inner describe
---
duration_ms: *
type: 'suite'
...
1..3
ok 60 - async describe function
---
duration_ms: *
type: 'suite'
...
# Subtest: invalid subtest fail
not ok 60 - invalid subtest fail
not ok 61 - invalid subtest fail
---
duration_ms: *
failureType: 'parentAlreadyFinished'
Expand All @@ -645,16 +674,16 @@ not ok 60 - invalid subtest fail
stack: |-
*
...
1..60
1..61
# 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.
# 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.
# 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.
# 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.
# 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.
# 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.
# tests 67
# suites 9
# pass 29
# tests 70
# suites 11
# pass 32
# fail 19
# cancelled 4
# skipped 10
Expand Down

0 comments on commit 3a68c4b

Please sign in to comment.