Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test_runner: catch errors thrown within describe #43729

Merged
merged 3 commits into from
Jul 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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