Skip to content

Commit

Permalink
fix: catch errors thrown within describe
Browse files Browse the repository at this point in the history
PR-URL: nodejs/node#43729
Reviewed-By: Antoine du Hamel <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
(cherry picked from commit 3aec7da5c6fcbedffdb034a9b7f6b60958b88d93)
  • Loading branch information
MoLow authored and aduh95 committed Jul 16, 2022
1 parent 88d5c8f commit c09cfd7
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 50 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 @@ -365,6 +365,7 @@ class Test extends AsyncResource {
if (this.endTime < this.startTime) {
this.endTime = hrtime()
}
if (this.startTime == null) 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 @@ -462,7 +463,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 @@ -472,9 +477,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_desctibe_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 c09cfd7

Please sign in to comment.