Skip to content

Commit

Permalink
test_runner: add t.after() hook
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
cjihrig authored and targos committed Dec 12, 2022
1 parent cf7253d commit 95af851
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
27 changes: 27 additions & 0 deletions doc/api/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -1133,6 +1133,33 @@ test('top level test', async (t) => {
});
```

### `context.after([fn][, options])`

<!-- YAML
added: REPLACEME
-->

* `fn` {Function|AsyncFunction} The hook function. The first argument
to this function is a [`TestContext`][] object. If the hook uses callbacks,
the callback function is passed as the second argument. **Default:** A no-op
function.
* `options` {Object} Configuration options for the hook. The following
properties are supported:
* `signal` {AbortSignal} Allows aborting an in-progress hook.
* `timeout` {number} A number of milliseconds the hook will fail after.
If unspecified, subtests inherit this value from their parent.
**Default:** `Infinity`.

This function is used to create a hook that runs after the current test
finishes.

```js
test('top level test', async (t) => {
t.after((t) => t.diagnostic(`finished running ${t.name}`));
assert.ok('some relevant assertion here');
});
```

### `context.afterEach([fn][, options])`

<!-- YAML
Expand Down
5 changes: 5 additions & 0 deletions lib/internal/test_runner/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ class TestContext {
return subtest.start();
}

after(fn, options) {
this.#test.createHook('after', fn, options);
}

beforeEach(fn, options) {
this.#test.createHook('beforeEach', fn, options);
}
Expand Down Expand Up @@ -545,6 +549,7 @@ class Test extends AsyncResource {
return;
}

await this.runHook('after', { args, ctx });
await afterEach();
this.pass();
} catch (err) {
Expand Down
8 changes: 7 additions & 1 deletion test/message/test_runner_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ describe('afterEach throws and test fails', () => {

test('test hooks', async (t) => {
const testArr = [];

t.after(common.mustCall((t) => testArr.push('after ' + t.name)));
t.beforeEach((t) => testArr.push('beforeEach ' + t.name));
t.afterEach((t) => testArr.push('afterEach ' + t.name));
await t.test('1', () => testArr.push('1'));
Expand All @@ -113,26 +115,30 @@ test('test hooks', async (t) => {
});

test('t.beforeEach throws', async (t) => {
t.after(common.mustCall());
t.beforeEach(() => { throw new Error('beforeEach'); });
await t.test('1', () => {});
await t.test('2', () => {});
});

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


test('afterEach when test fails', async (t) => {
t.after(common.mustCall());
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'); });
t.after(common.mustCall());
t.afterEach(() => { throw new Error('afterEach'); });
await t.test('1', () => { throw new Error('test'); });
await t.test('2', () => {});
});

0 comments on commit 95af851

Please sign in to comment.