Skip to content

Commit 4493266

Browse files
cjihrigdanielleadams
authored andcommitted
test_runner: 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: #45792 Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent 370ad45 commit 4493266

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

doc/api/test.md

+27
Original file line numberDiff line numberDiff line change
@@ -1115,6 +1115,33 @@ test('top level test', async (t) => {
11151115
});
11161116
```
11171117

1118+
### `context.after([fn][, options])`
1119+
1120+
<!-- YAML
1121+
added: REPLACEME
1122+
-->
1123+
1124+
* `fn` {Function|AsyncFunction} The hook function. The first argument
1125+
to this function is a [`TestContext`][] object. If the hook uses callbacks,
1126+
the callback function is passed as the second argument. **Default:** A no-op
1127+
function.
1128+
* `options` {Object} Configuration options for the hook. The following
1129+
properties are supported:
1130+
* `signal` {AbortSignal} Allows aborting an in-progress hook.
1131+
* `timeout` {number} A number of milliseconds the hook will fail after.
1132+
If unspecified, subtests inherit this value from their parent.
1133+
**Default:** `Infinity`.
1134+
1135+
This function is used to create a hook that runs after the current test
1136+
finishes.
1137+
1138+
```js
1139+
test('top level test', async (t) => {
1140+
t.after((t) => t.diagnostic(`finished running ${t.name}`));
1141+
assert.ok('some relevant assertion here');
1142+
});
1143+
```
1144+
11181145
### `context.afterEach([fn][, options])`
11191146

11201147
<!-- YAML

lib/internal/test_runner/test.js

+5
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@ class TestContext {
136136
return subtest.start();
137137
}
138138

139+
after(fn, options) {
140+
this.#test.createHook('after', fn, options);
141+
}
142+
139143
beforeEach(fn, options) {
140144
this.#test.createHook('beforeEach', fn, options);
141145
}
@@ -545,6 +549,7 @@ class Test extends AsyncResource {
545549
return;
546550
}
547551

552+
await this.runHook('after', { args, ctx });
548553
await afterEach();
549554
this.pass();
550555
} catch (err) {

test/message/test_runner_hooks.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ describe('afterEach throws and test fails', () => {
9090

9191
test('test hooks', async (t) => {
9292
const testArr = [];
93+
94+
t.after(common.mustCall((t) => testArr.push('after ' + t.name)));
9395
t.beforeEach((t) => testArr.push('beforeEach ' + t.name));
9496
t.afterEach((t) => testArr.push('afterEach ' + t.name));
9597
await t.test('1', () => testArr.push('1'));
@@ -113,26 +115,30 @@ test('test hooks', async (t) => {
113115
});
114116

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

121124
test('t.afterEach throws', async (t) => {
125+
t.after(common.mustCall());
122126
t.afterEach(() => { throw new Error('afterEach'); });
123127
await t.test('1', () => {});
124128
await t.test('2', () => {});
125129
});
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)