Skip to content

Commit 8a0f64b

Browse files
committed
Implement afterEach
1 parent d24f412 commit 8a0f64b

File tree

5 files changed

+37
-11
lines changed

5 files changed

+37
-11
lines changed

packages/cli/src/benchmark/lib/duplicate-hook.error.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { ApplicationError } from 'n8n-workflow';
22

33
export class DuplicateHookError extends ApplicationError {
4-
constructor(hookName: 'beforeEach', filePath: string) {
4+
constructor(hookName: 'beforeEach' | 'afterEach', filePath: string) {
55
super(
66
`Duplicate \`${hookName}\` hook found in benchmarking suite \`${filePath}\`. Please define a single \`${hookName}\` hook for this suite.`,
77
{ level: 'warning' },

packages/cli/src/benchmark/lib/suites.ts

+26-7
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,22 @@ export async function collectSuites() {
2525
}
2626

2727
export function registerSuites(bench: Bench) {
28-
for (const { tasks, hooks } of Object.values(suites)) {
29-
for (const t of tasks) {
30-
/**
31-
* `beforeAll` in tinybench is called once before all iterations of a single operation.
32-
* This is functionally equivalent to `beforeEach` in jest and vitest.
33-
*/
34-
const options = hooks.beforeEach ? { beforeAll: hooks.beforeEach } : {};
28+
for (const { hooks, tasks } of Object.values(suites)) {
29+
/**
30+
* In tinybench, `beforeAll` and `afterAll` refer to all iterations of
31+
* a single task, while `beforeEach` and `afterEach` refer to each iteration.
32+
*
33+
* In jest and vitest, `beforeAll` and `afterAll` refer to all tests in a suite,
34+
* while `beforeEach` and `afterEach` refer to each individual test.
35+
*
36+
* The API renames tinybench's hooks to prevent confusion from this difference.
37+
*/
38+
const options: Record<string, Callback> = {};
39+
40+
if (hooks.beforeEach) options.beforeAll = hooks.beforeEach;
41+
if (hooks.afterEach) options.afterAll = hooks.afterEach;
3542

43+
for (const t of tasks) {
3644
bench.add(t.description, t.operation, options);
3745
}
3846
}
@@ -70,3 +78,14 @@ export function beforeEach(fn: Callback) {
7078
suites[filePath] ||= { hooks: {}, tasks: [] };
7179
suites[filePath].hooks.beforeEach = fn;
7280
}
81+
82+
export function afterEach(fn: Callback) {
83+
const filePath = suiteFilePath();
84+
85+
if (suites[filePath]?.hooks.afterEach) {
86+
throw new DuplicateHookError('afterEach', filePath);
87+
}
88+
89+
suites[filePath] ||= { hooks: {}, tasks: [] };
90+
suites[filePath].hooks.afterEach = fn;
91+
}

packages/cli/src/benchmark/lib/types.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
export type Suites = {
22
[suiteFilepath: string]: {
3-
hooks: Partial<{ beforeEach: Callback }>;
3+
hooks: {
4+
beforeEach?: Callback;
5+
afterEach?: Callback;
6+
};
47
tasks: Task[];
58
};
69
};

packages/cli/src/benchmark/main.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import Bench from 'tinybench';
77
import { withCodSpeed } from '@codspeed/tinybench-plugin';
88
/* eslint-enable import/no-extraneous-dependencies */
99

10-
export { beforeEach, task } from './lib/suites';
10+
export { beforeEach, afterEach, task } from './lib/suites';
1111

1212
async function main() {
1313
await collectSuites();

packages/cli/src/benchmark/tasks/example.tasks.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
import { task, beforeEach } from '../main.js';
1+
import { task, beforeEach, afterEach } from '../main.js';
22

33
beforeEach(async () => {
44
console.log('[[[beforeEach]]] for example.tasks.ts');
55
});
66

7+
afterEach(async () => {
8+
console.log('[[[afterEach]]] for example.tasks.ts');
9+
});
10+
711
task('[example] Should do something', async () => {
812
console.log('Example task 1 executed');
913
});

0 commit comments

Comments
 (0)