Skip to content

Commit

Permalink
Implement afterEach
Browse files Browse the repository at this point in the history
  • Loading branch information
ivov committed Apr 19, 2024
1 parent d24f412 commit 8a0f64b
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 11 deletions.
2 changes: 1 addition & 1 deletion packages/cli/src/benchmark/lib/duplicate-hook.error.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ApplicationError } from 'n8n-workflow';

export class DuplicateHookError extends ApplicationError {
constructor(hookName: 'beforeEach', filePath: string) {
constructor(hookName: 'beforeEach' | 'afterEach', filePath: string) {
super(
`Duplicate \`${hookName}\` hook found in benchmarking suite \`${filePath}\`. Please define a single \`${hookName}\` hook for this suite.`,
{ level: 'warning' },
Expand Down
33 changes: 26 additions & 7 deletions packages/cli/src/benchmark/lib/suites.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,22 @@ export async function collectSuites() {
}

export function registerSuites(bench: Bench) {
for (const { tasks, hooks } of Object.values(suites)) {
for (const t of tasks) {
/**
* `beforeAll` in tinybench is called once before all iterations of a single operation.
* This is functionally equivalent to `beforeEach` in jest and vitest.
*/
const options = hooks.beforeEach ? { beforeAll: hooks.beforeEach } : {};
for (const { hooks, tasks } of Object.values(suites)) {
/**
* In tinybench, `beforeAll` and `afterAll` refer to all iterations of
* a single task, while `beforeEach` and `afterEach` refer to each iteration.
*
* In jest and vitest, `beforeAll` and `afterAll` refer to all tests in a suite,
* while `beforeEach` and `afterEach` refer to each individual test.
*
* The API renames tinybench's hooks to prevent confusion from this difference.
*/
const options: Record<string, Callback> = {};

if (hooks.beforeEach) options.beforeAll = hooks.beforeEach;
if (hooks.afterEach) options.afterAll = hooks.afterEach;

for (const t of tasks) {
bench.add(t.description, t.operation, options);
}
}
Expand Down Expand Up @@ -70,3 +78,14 @@ export function beforeEach(fn: Callback) {
suites[filePath] ||= { hooks: {}, tasks: [] };
suites[filePath].hooks.beforeEach = fn;
}

export function afterEach(fn: Callback) {
const filePath = suiteFilePath();

if (suites[filePath]?.hooks.afterEach) {
throw new DuplicateHookError('afterEach', filePath);
}

suites[filePath] ||= { hooks: {}, tasks: [] };
suites[filePath].hooks.afterEach = fn;
}
5 changes: 4 additions & 1 deletion packages/cli/src/benchmark/lib/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
export type Suites = {
[suiteFilepath: string]: {
hooks: Partial<{ beforeEach: Callback }>;
hooks: {
beforeEach?: Callback;
afterEach?: Callback;
};
tasks: Task[];
};
};
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/benchmark/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Bench from 'tinybench';
import { withCodSpeed } from '@codspeed/tinybench-plugin';
/* eslint-enable import/no-extraneous-dependencies */

export { beforeEach, task } from './lib/suites';
export { beforeEach, afterEach, task } from './lib/suites';

async function main() {
await collectSuites();
Expand Down
6 changes: 5 additions & 1 deletion packages/cli/src/benchmark/tasks/example.tasks.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { task, beforeEach } from '../main.js';
import { task, beforeEach, afterEach } from '../main.js';

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

afterEach(async () => {
console.log('[[[afterEach]]] for example.tasks.ts');
});

task('[example] Should do something', async () => {
console.log('Example task 1 executed');
});
Expand Down

0 comments on commit 8a0f64b

Please sign in to comment.