-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcommon.ts
More file actions
42 lines (34 loc) · 1.31 KB
/
common.ts
File metadata and controls
42 lines (34 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { getGitDir } from "@codspeed/core";
import path from "path";
import { Benchmark, type RunnerTask, type RunnerTestSuite } from "vitest";
import { getHooks } from "./compat";
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type SuiteHooks = Record<string, any[]>;
function getSuiteHooks(suite: RunnerTestSuite, name: keyof SuiteHooks) {
return getHooks(suite)?.[name] ?? [];
}
export async function callSuiteHook<T extends keyof SuiteHooks>(
suite: RunnerTestSuite,
currentTask: RunnerTask,
name: T
): Promise<void> {
if (name === "beforeEach" && suite?.suite) {
await callSuiteHook(suite.suite, currentTask, name);
}
const hooks = getSuiteHooks(suite, name);
// @ts-expect-error TODO: add support for hooks parameters
await Promise.all(hooks.map((fn) => fn()));
if (name === "afterEach" && suite?.suite) {
await callSuiteHook(suite.suite, currentTask, name);
}
}
export function patchRootSuiteWithFullFilePath(suite: RunnerTestSuite) {
const gitDir = getGitDir(suite.file.filepath);
if (gitDir === undefined) {
throw new Error("Could not find a git repository");
}
suite.name = path.relative(gitDir, suite.file.filepath);
}
export function isVitestTaskBenchmark(task: RunnerTask): task is Benchmark {
return task.type === "test" && task.meta.benchmark === true;
}