Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
012acb0
feat: verbous error when entire test tree is canceled
MoLow Aug 2, 2022
d885ee2
feat: support programmatically running `--test`
MoLow Aug 15, 2022
27241c3
fix: fix `duration_ms` to be milliseconds
MoLow Sep 4, 2022
6755536
feat: support using `--inspect` with `--test`
MoLow Sep 10, 2022
c50f844
fix: include stack of uncaught exceptions
MoLow Sep 14, 2022
1950b38
test: fix test-runner-inspect
MoLow Sep 14, 2022
c5fd64c
feat: add --test-name-pattern CLI flag
cjihrig Sep 1, 2022
46dce07
feat: add extra fields in AssertionError YAML
bengl Oct 26, 2022
0bfdb77
fix: call {before,after}Each() on suites
cjihrig Oct 27, 2022
08269c5
fix: report tap subtest in order
MoLow Oct 28, 2022
f2815af
fix: fix afterEach not running on test failures
MrJithil Nov 7, 2022
cff397a
fix: avoid swallowing of asynchronously thrown errors
fossamagna Nov 7, 2022
2e499ee
feat: support function mocking
cjihrig Apr 4, 2022
5f8ce61
feat: add initial TAP parser
manekinekko Nov 21, 2022
5ba2500
fix: remove stdout and stderr from error
cjihrig Nov 25, 2022
b942f93
feat: add getter and setter to MockTracker
fossamagna Nov 18, 2022
b3b384e
fix: don't use a symbol for runHook()
cjihrig Dec 6, 2022
71b659e
feat: add t.after() hook
cjihrig Dec 8, 2022
c0854ac
test: fix invalid output TAP if there newline in test name
pulkit-30 Dec 11, 2022
9b49978
chore: refactor `tap_parser` to use more primordials
aduh95 Dec 11, 2022
4e778bc
chore: refactor `tap_lexer` to use more primordials
aduh95 Dec 12, 2022
d1343a7
feat: parse yaml
MoLow Dec 13, 2022
c80e426
fix: run t.after() if test body throws
cjihrig Dec 17, 2022
9fa496b
test: fix mock.method to support class instances
ErickWendel Dec 17, 2022
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
Prev Previous commit
Next Next commit
fix: fix afterEach not running on test failures
PR-URL: nodejs/node#45204
Fixes: nodejs/node#45192
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
(cherry picked from commit 3759935ee29d8042d917d3ceaa768521c14413ff)
  • Loading branch information
MrJithil authored and MoLow committed Feb 2, 2023
commit f2815afc2ff26902d027c0dba85eed773040c1a2
1 change: 1 addition & 0 deletions lib/internal/per_context/primordials.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ exports.StringPrototypeSlice = (str, ...args) => str.slice(...args)
exports.StringPrototypeSplit = (str, search, limit) => str.split(search, limit)
exports.Symbol = Symbol
exports.SymbolFor = repr => Symbol.for(repr)
exports.ReflectApply = (target, self, args) => Reflect.apply(target, self, args)
exports.RegExpPrototypeExec = (reg, str) => reg.exec(str)
exports.RegExpPrototypeSymbolReplace = (regexp, str, replacement) =>
regexp[Symbol.replace](str, replacement)
33 changes: 20 additions & 13 deletions lib/internal/test_runner/test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// https://github.com/nodejs/node/blob/3e57891ee2fde0971e18fc383c25acf8f90def05/lib/internal/test_runner/test.js
// https://github.com/nodejs/node/blob/3759935ee29d8042d917d3ceaa768521c14413ff/lib/internal/test_runner/test.js

'use strict'

Expand Down Expand Up @@ -43,7 +43,8 @@ const {
} = require('#internal/test_runner/utils')
const {
createDeferredPromise,
kEmptyObject
kEmptyObject,
once: runOnce
} = require('#internal/util')
const { isPromise } = require('#internal/util/types')
const {
Expand Down Expand Up @@ -486,8 +487,14 @@ class Test extends AsyncResource {
return
}

const { args, ctx } = this.getRunArgs()
const afterEach = runOnce(async () => {
if (this.parent?.hooks.afterEach.length > 0) {
await this.parent[kRunHook]('afterEach', { args, ctx })
}
})

try {
const { args, ctx } = this.getRunArgs()
if (this.parent?.hooks.beforeEach.length > 0) {
await this.parent[kRunHook]('beforeEach', { args, ctx })
}
Expand Down Expand Up @@ -522,12 +529,10 @@ class Test extends AsyncResource {
return
}

if (this.parent?.hooks.afterEach.length > 0) {
await this.parent[kRunHook]('afterEach', { args, ctx })
}

await afterEach()
this.pass()
} catch (err) {
try { await afterEach() } catch { /* test is already failing, let's the error */ }
if (isTestFailureError(err)) {
if (err.failureType === kTestTimeoutFailure) {
this.cancel(err)
Expand Down Expand Up @@ -735,6 +740,12 @@ class Suite extends Test {
}

async run () {
const hookArgs = this.getRunArgs()
const afterEach = runOnce(async () => {
if (this.parent?.hooks.afterEach.length > 0) {
await this.parent[kRunHook]('afterEach', hookArgs)
}
})
try {
this.parent.activeSubtests++
await this.buildSuite
Expand All @@ -746,8 +757,6 @@ class Suite extends Test {
return
}

const hookArgs = this.getRunArgs()

if (this.parent?.hooks.beforeEach.length > 0) {
await this.parent[kRunHook]('beforeEach', hookArgs)
}
Expand All @@ -760,13 +769,11 @@ class Suite extends Test {

await SafePromiseRace([promise, stopPromise])
await this[kRunHook]('after', hookArgs)

if (this.parent?.hooks.afterEach.length > 0) {
await this.parent[kRunHook]('afterEach', hookArgs)
}
await afterEach()

this.pass()
} catch (err) {
try { await afterEach() } catch { /* test is already failing, let's the error */ }
if (isTestFailureError(err)) {
this.fail(err)
} else {
Expand Down
17 changes: 14 additions & 3 deletions lib/internal/util.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// https://github.com/nodejs/node/blob/a9b1fd3987fae5ad5340859a6088b86179b576c5/lib/internal/util.js
// https://github.com/nodejs/node/blob/3759935ee29d8042d917d3ceaa768521c14413ff/lib/internal/util.js
'use strict'

const {
ObjectCreate,
ObjectFreeze
ObjectFreeze,
ReflectApply
} = require('#internal/per_context/primordials')
const {
types: { isNativeError }
Expand All @@ -27,10 +28,20 @@ function isError (e) {
return isNativeError(e) || e instanceof Error
}

function once (callback) {
let called = false
return function (...args) {
if (called) return
called = true
return ReflectApply(callback, this, args)
}
}

const kEmptyObject = ObjectFreeze(ObjectCreate(null))

module.exports = {
createDeferredPromise,
isError,
kEmptyObject
kEmptyObject,
once
}
2 changes: 0 additions & 2 deletions test/message/test_runner_desctibe_it.out
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ not ok 4 - sync fail todo with message # TODO this is a failing todo
*
*
*
*
*
...
# Subtest: sync skip pass
ok 5 - sync skip pass # SKIP
Expand Down
28 changes: 26 additions & 2 deletions test/message/test_runner_hooks.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// https://github.com/nodejs/node/blob/a69a30016cf3395b0bd775c1340ab6ecbac58296/test/message/test_runner_hooks.js
// https://github.com/nodejs/node/blob/3759935ee29d8042d917d3ceaa768521c14413ff/test/message/test_runner_hooks.js
// Flags: --no-warnings
'use strict'
require('../common')
const common = require('../common')
const assert = require('assert')
const { test, describe, it, before, after, beforeEach, afterEach } = require('#node:test')

Expand Down Expand Up @@ -77,6 +77,18 @@ describe('afterEach throws', () => {
it('2', () => {})
})

describe('afterEach when test fails', () => {
afterEach(common.mustCall(2))
it('1', () => { throw new Error('test') })
it('2', () => {})
})

describe('afterEach throws and test fails', () => {
afterEach(() => { throw new Error('afterEach') })
it('1', () => { throw new Error('test') })
it('2', () => {})
})

test('test hooks', async (t) => {
const testArr = []
t.beforeEach((t) => testArr.push('beforeEach ' + t.name))
Expand Down Expand Up @@ -112,3 +124,15 @@ test('t.afterEach throws', async (t) => {
await t.test('1', () => {})
await t.test('2', () => {})
})

test('afterEach when test fails', async (t) => {
t.afterEach(common.mustCall(2))
await t.test('1', () => { throw new Error('test') })
await t.test('2', () => {})
})

test('afterEach throws and test fails', async (t) => {
afterEach(() => { throw new Error('afterEach') })
await t.test('1', () => { throw new Error('test') })
await t.test('2', () => {})
})
172 changes: 166 additions & 6 deletions test/message/test_runner_hooks.out
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,86 @@ not ok 5 - afterEach throws
error: '2 subtests failed'
code: 'ERR_TEST_FAILURE'
...
# Subtest: afterEach when test fails
# Subtest: 1
not ok 1 - 1
---
duration_ms: *
failureType: 'testCodeFailure'
error: 'test'
code: 'ERR_TEST_FAILURE'
stack: |-
*
*
*
*
*
*
*
*
*
*
...
# Subtest: 2
ok 2 - 2
---
duration_ms: *
...
1..2
not ok 6 - afterEach when test fails
---
duration_ms: *
failureType: 'subtestsFailed'
error: '1 subtest failed'
code: 'ERR_TEST_FAILURE'
...
# Subtest: afterEach throws and test fails
# Subtest: 1
not ok 1 - 1
---
duration_ms: *
failureType: 'testCodeFailure'
error: 'test'
code: 'ERR_TEST_FAILURE'
stack: |-
*
*
*
*
*
*
*
*
*
*
...
# Subtest: 2
not ok 2 - 2
---
duration_ms: *
failureType: 'hookFailed'
error: 'failed running afterEach hook'
code: 'ERR_TEST_FAILURE'
stack: |-
*
*
*
*
*
*
*
*
*
*
...
1..2
not ok 7 - afterEach throws and test fails
---
duration_ms: *
failureType: 'subtestsFailed'
error: '2 subtests failed'
code: 'ERR_TEST_FAILURE'
...
# Subtest: test hooks
# Subtest: 1
ok 1 - 1
Expand Down Expand Up @@ -217,7 +297,7 @@ not ok 5 - afterEach throws
duration_ms: *
...
1..3
ok 6 - test hooks
ok 8 - test hooks
---
duration_ms: *
...
Expand Down Expand Up @@ -261,7 +341,7 @@ ok 6 - test hooks
*
...
1..2
not ok 7 - t.beforeEach throws
not ok 9 - t.beforeEach throws
---
duration_ms: *
failureType: 'subtestsFailed'
Expand Down Expand Up @@ -308,17 +388,97 @@ not ok 7 - t.beforeEach throws
*
...
1..2
not ok 8 - t.afterEach throws
not ok 10 - t.afterEach throws
---
duration_ms: *
failureType: 'subtestsFailed'
error: '2 subtests failed'
code: 'ERR_TEST_FAILURE'
...
# Subtest: afterEach when test fails
# Subtest: 1
not ok 1 - 1
---
duration_ms: *
failureType: 'testCodeFailure'
error: 'test'
code: 'ERR_TEST_FAILURE'
stack: |-
*
*
*
*
*
*
*
*
*
*
...
# Subtest: 2
ok 2 - 2
---
duration_ms: *
...
1..2
not ok 11 - afterEach when test fails
---
duration_ms: *
failureType: 'subtestsFailed'
error: '1 subtest failed'
code: 'ERR_TEST_FAILURE'
...
# Subtest: afterEach throws and test fails
# Subtest: 1
not ok 1 - 1
---
duration_ms: *
failureType: 'testCodeFailure'
error: 'test'
code: 'ERR_TEST_FAILURE'
stack: |-
*
*
*
*
*
*
*
*
*
*
...
# Subtest: 2
not ok 2 - 2
---
duration_ms: *
failureType: 'hookFailed'
error: 'failed running afterEach hook'
code: 'ERR_TEST_FAILURE'
stack: |-
*
*
*
*
*
*
*
*
*
*
...
1..2
not ok 12 - afterEach throws and test fails
---
duration_ms: *
failureType: 'subtestsFailed'
error: '2 subtests failed'
code: 'ERR_TEST_FAILURE'
...
1..8
# tests 8
1..12
# tests 12
# pass 2
# fail 6
# fail 10
# cancelled 0
# skipped 0
# todo 0
Expand Down
2 changes: 0 additions & 2 deletions test/message/test_runner_output.out
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ not ok 4 - sync fail todo with message # TODO this is a failing todo
*
*
*
*
*
...
# Subtest: sync skip pass
ok 5 - sync skip pass # SKIP
Expand Down