Skip to content

Commit 71b659e

Browse files
cjihrigMoLow
authored andcommitted
feat: add t.after() hook
This commit adds an after() hook to the TestContext class. This hook can be used to clean up after a test finishes. PR-URL: nodejs/node#45792 Reviewed-By: Moshe Atlow <[email protected]> Reviewed-By: Matteo Collina <[email protected]> (cherry picked from commit 215c5317d4837287fddb2e3b97872babd53183ac)
1 parent b3b384e commit 71b659e

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,6 +1035,33 @@ test('top level test', async (t) => {
10351035
});
10361036
```
10371037

1038+
### `context.after([fn][, options])`
1039+
1040+
<!-- YAML
1041+
added: REPLACEME
1042+
-->
1043+
1044+
* `fn` {Function|AsyncFunction} The hook function. The first argument
1045+
to this function is a [`TestContext`][] object. If the hook uses callbacks,
1046+
the callback function is passed as the second argument. **Default:** A no-op
1047+
function.
1048+
* `options` {Object} Configuration options for the hook. The following
1049+
properties are supported:
1050+
* `signal` {AbortSignal} Allows aborting an in-progress hook.
1051+
* `timeout` {number} A number of milliseconds the hook will fail after.
1052+
If unspecified, subtests inherit this value from their parent.
1053+
**Default:** `Infinity`.
1054+
1055+
This function is used to create a hook that runs after the current test
1056+
finishes.
1057+
1058+
```js
1059+
test('top level test', async (t) => {
1060+
t.after((t) => t.diagnostic(`finished running ${t.name}`));
1061+
assert.ok('some relevant assertion here');
1062+
});
1063+
```
1064+
10381065
### `context.afterEach([, fn][, options])`
10391066

10401067
* `fn` {Function|AsyncFunction} The hook function. The first argument

lib/internal/test_runner/test.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// https://github.com/nodejs/node/blob/8302b0add01758713246117d3d0533cd212f160d/lib/internal/test_runner/test.js
1+
// https://github.com/nodejs/node/blob/215c5317d4837287fddb2e3b97872babd53183ac/lib/internal/test_runner/test.js
22

33
'use strict'
44

@@ -140,6 +140,10 @@ class TestContext {
140140
return subtest.start()
141141
}
142142

143+
after (fn, options) {
144+
this.#test.createHook('after', fn, options)
145+
}
146+
143147
beforeEach (fn, options) {
144148
this.#test.createHook('beforeEach', fn, options)
145149
}
@@ -533,6 +537,7 @@ class Test extends AsyncResource {
533537
return
534538
}
535539

540+
await this.runHook('after', { args, ctx })
536541
await afterEach()
537542
this.pass()
538543
} catch (err) {

test/message/test_runner_hooks.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// https://github.com/nodejs/node/blob/3759935ee29d8042d917d3ceaa768521c14413ff/test/message/test_runner_hooks.js
1+
// https://github.com/nodejs/node/blob/215c5317d4837287fddb2e3b97872babd53183ac/test/message/test_runner_hooks.js
22
// Flags: --no-warnings
33
'use strict'
44
const common = require('../common')
@@ -91,6 +91,8 @@ describe('afterEach throws and test fails', () => {
9191

9292
test('test hooks', async (t) => {
9393
const testArr = []
94+
95+
t.after(common.mustCall((t) => testArr.push('after ' + t.name)))
9496
t.beforeEach((t) => testArr.push('beforeEach ' + t.name))
9597
t.afterEach((t) => testArr.push('afterEach ' + t.name))
9698
await t.test('1', () => testArr.push('1'))
@@ -114,25 +116,29 @@ test('test hooks', async (t) => {
114116
})
115117

116118
test('t.beforeEach throws', async (t) => {
119+
t.after(common.mustCall())
117120
t.beforeEach(() => { throw new Error('beforeEach') })
118121
await t.test('1', () => {})
119122
await t.test('2', () => {})
120123
})
121124

122125
test('t.afterEach throws', async (t) => {
126+
t.after(common.mustCall())
123127
t.afterEach(() => { throw new Error('afterEach') })
124128
await t.test('1', () => {})
125129
await t.test('2', () => {})
126130
})
127131

128132
test('afterEach when test fails', async (t) => {
133+
t.after(common.mustCall())
129134
t.afterEach(common.mustCall(2))
130135
await t.test('1', () => { throw new Error('test') })
131136
await t.test('2', () => {})
132137
})
133138

134139
test('afterEach throws and test fails', async (t) => {
135-
afterEach(() => { throw new Error('afterEach') })
140+
t.after(common.mustCall())
141+
t.afterEach(() => { throw new Error('afterEach') })
136142
await t.test('1', () => { throw new Error('test') })
137143
await t.test('2', () => {})
138144
})

0 commit comments

Comments
 (0)