Skip to content

Commit

Permalink
test_runner: catch errors thrown within describe
Browse files Browse the repository at this point in the history
PR-URL: #43729
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
  • Loading branch information
MoLow authored Jul 12, 2022
1 parent ed7631d commit 3aec7da
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 52 deletions.
14 changes: 12 additions & 2 deletions lib/internal/test_runner/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ class Test extends AsyncResource {
if (this.endTime < this.startTime) {
this.endTime = hrtime();
}
this.startTime ??= this.endTime;

// The test has run, so recursively cancel any outstanding subtests and
// mark this test as failed if any subtests failed.
Expand Down Expand Up @@ -457,7 +458,11 @@ class Suite extends Test {
constructor(options) {
super(options);

this.runInAsyncScope(this.fn);
try {
this.buildSuite = this.runInAsyncScope(this.fn);
} catch (err) {
this.fail(new ERR_TEST_FAILURE(err, kTestCodeFailure));
}
this.fn = () => {};
this.finished = true; // Forbid adding subtests to this suite
}
Expand All @@ -467,9 +472,14 @@ class Suite extends Test {
}

async run() {
try {
await this.buildSuite;
} catch (err) {
this.fail(new ERR_TEST_FAILURE(err, kTestCodeFailure));
}
this.parent.activeSubtests++;
this.startTime = hrtime();
const subtests = this.skipped ? [] : this.subtests;
const subtests = this.skipped || this.error ? [] : this.subtests;
await ArrayPrototypeReduce(subtests, async (prev, subtest) => {
await prev;
await subtest.run();
Expand Down
15 changes: 15 additions & 0 deletions test/message/test_runner_describe_it.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ it('async throw fail', async () => {
throw new Error('thrown from async throw fail');
});

it('async skip fail', async (t) => {
t.skip();
throw new Error('thrown from async throw fail');
});

it('async assertion fail', async () => {
// Make sure the assert module is handled.
assert.strictEqual(true, false);
Expand Down Expand Up @@ -301,3 +306,13 @@ describe('subtest sync throw fails', () => {
throw new Error('thrown from subtest sync throw fails at second');
});
});

describe('describe sync throw fails', () => {
it('should not run', () => {});
throw new Error('thrown from describe');
});

describe('describe async throw fails', async () => {
it('should not run', () => {});
throw new Error('thrown from describe');
});
Loading

0 comments on commit 3aec7da

Please sign in to comment.