Skip to content

Commit

Permalink
Prevent duplicate beforeEach`
Browse files Browse the repository at this point in the history
  • Loading branch information
ivov committed Apr 19, 2024
1 parent 8282963 commit d24f412
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
10 changes: 10 additions & 0 deletions packages/cli/src/benchmark/lib/duplicate-hook.error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { ApplicationError } from 'n8n-workflow';

export class DuplicateHookError extends ApplicationError {
constructor(hookName: 'beforeEach', filePath: string) {
super(
`Duplicate \`${hookName}\` hook found in benchmarking suite \`${filePath}\`. Please define a single \`${hookName}\` hook for this suite.`,
{ level: 'warning' },
);
}
}
25 changes: 17 additions & 8 deletions packages/cli/src/benchmark/lib/suites.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import type Bench from 'tinybench';
import { assert } from 'n8n-workflow';
import glob from 'fast-glob';
import callsites from 'callsites';
import type { Suites, Task } from './types';
import type { Suites, Task, Callback } from './types';
import { DuplicateHookError } from './duplicate-hook.error';

export const suites: Suites = {};

Expand All @@ -18,8 +19,8 @@ export async function collectSuites() {
absolute: true,
});

for (const file of files) {
await import(file);
for (const f of files) {
await import(f);
}
}

Expand Down Expand Up @@ -53,11 +54,19 @@ function suiteFilePath() {
*/

export function task(description: string, operation: Task['operation']) {
suites[suiteFilePath()] ||= { hooks: {}, tasks: [] };
suites[suiteFilePath()].tasks.push({ description, operation });
const filePath = suiteFilePath();

suites[filePath] ||= { hooks: {}, tasks: [] };
suites[filePath].tasks.push({ description, operation });
}

export function beforeEach(fn: () => Promise<void>) {
suites[suiteFilePath()] ||= { hooks: {}, tasks: [] };
suites[suiteFilePath()].hooks.beforeEach = fn;
export function beforeEach(fn: Callback) {
const filePath = suiteFilePath();

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

suites[filePath] ||= { hooks: {}, tasks: [] };
suites[filePath].hooks.beforeEach = fn;
}
4 changes: 2 additions & 2 deletions packages/cli/src/benchmark/lib/types.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
export type Suites = {
[suiteFilepath: string]: {
hooks: Partial<{ beforeEach: () => Promise<void> }>;
hooks: Partial<{ beforeEach: Callback }>;
tasks: Task[];
};
};

/** A benchmarking task, i.e. a single operation whose performance to measure. */
export type Task = {
description: string;
operation: () => Promise<void>;
operation: Callback;
};

export type Callback = () => void | Promise<void>;

0 comments on commit d24f412

Please sign in to comment.