Skip to content

Commit c09cfd7

Browse files
MoLowaduh95
authored andcommitted
fix: catch errors thrown within describe
PR-URL: nodejs/node#43729 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> (cherry picked from commit 3aec7da5c6fcbedffdb034a9b7f6b60958b88d93)
1 parent 88d5c8f commit c09cfd7

File tree

3 files changed

+137
-50
lines changed

3 files changed

+137
-50
lines changed

lib/internal/test_runner/test.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ class Test extends AsyncResource {
365365
if (this.endTime < this.startTime) {
366366
this.endTime = hrtime()
367367
}
368+
if (this.startTime == null) this.startTime = this.endTime
368369

369370
// The test has run, so recursively cancel any outstanding subtests and
370371
// mark this test as failed if any subtests failed.
@@ -462,7 +463,11 @@ class Suite extends Test {
462463
constructor (options) {
463464
super(options)
464465

465-
this.runInAsyncScope(this.fn)
466+
try {
467+
this.buildSuite = this.runInAsyncScope(this.fn)
468+
} catch (err) {
469+
this.fail(new ERR_TEST_FAILURE(err, kTestCodeFailure))
470+
}
466471
this.fn = () => {}
467472
this.finished = true // Forbid adding subtests to this suite
468473
}
@@ -472,9 +477,14 @@ class Suite extends Test {
472477
}
473478

474479
async run () {
480+
try {
481+
await this.buildSuite
482+
} catch (err) {
483+
this.fail(new ERR_TEST_FAILURE(err, kTestCodeFailure))
484+
}
475485
this.parent.activeSubtests++
476486
this.startTime = hrtime()
477-
const subtests = this.skipped ? [] : this.subtests
487+
const subtests = this.skipped || this.error ? [] : this.subtests
478488
await ArrayPrototypeReduce(subtests, async (prev, subtest) => {
479489
await prev
480490
await subtest.run()

test/message/test_runner_desctibe_it.js

+15
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ it('async throw fail', async () => {
4545
throw new Error('thrown from async throw fail')
4646
})
4747

48+
it('async skip fail', async (t) => {
49+
t.skip()
50+
throw new Error('thrown from async throw fail')
51+
})
52+
4853
it('async assertion fail', async () => {
4954
// Make sure the assert module is handled.
5055
assert.strictEqual(true, false)
@@ -301,3 +306,13 @@ describe('subtest sync throw fails', () => {
301306
throw new Error('thrown from subtest sync throw fails at second')
302307
})
303308
})
309+
310+
describe('describe sync throw fails', () => {
311+
it('should not run', () => {})
312+
throw new Error('thrown from describe')
313+
})
314+
315+
describe('describe async throw fails', async () => {
316+
it('should not run', () => {})
317+
throw new Error('thrown from describe')
318+
})

0 commit comments

Comments
 (0)