-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathinstrumented.test.ts
More file actions
136 lines (120 loc) · 4.18 KB
/
instrumented.test.ts
File metadata and controls
136 lines (120 loc) · 4.18 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import { fromPartial } from "@total-typescript/shoehorn";
import { describe, expect, it, vi, type RunnerTestSuite } from "vitest";
import { getBenchFn } from "../compat";
import { AnalysisRunner as CodSpeedRunner } from "../analysis";
const coreMocks = vi.hoisted(() => {
return {
InstrumentHooks: {
startBenchmark: vi.fn(),
stopBenchmark: vi.fn(),
setExecutedBenchmark: vi.fn(),
},
setupCore: vi.fn(),
teardownCore: vi.fn(),
mongoMeasurement: {
start: vi.fn(),
stop: vi.fn(),
},
};
});
global.eval = vi.fn();
vi.mock("@codspeed/core", async (importOriginal) => {
const mod = await importOriginal<typeof import("@codspeed/core")>();
return { ...mod, ...coreMocks };
});
console.log = vi.fn();
vi.mock("../compat", async (importOriginal) => {
const actual = await importOriginal<typeof import("../compat")>();
return {
...actual,
getBenchFn: vi.fn(),
};
});
const mockedGetBenchFn = vi.mocked(getBenchFn);
describe("CodSpeedRunner", () => {
it("should run the bench function", async () => {
const benchFn = vi.fn();
mockedGetBenchFn.mockReturnValue(benchFn);
const runner = new CodSpeedRunner(fromPartial({}));
const suite = fromPartial<RunnerTestSuite>({
file: { filepath: __filename },
name: "test suite",
tasks: [
{
type: "test",
mode: "run",
meta: { benchmark: true },
name: "test bench",
},
],
});
await runner.runSuite(suite);
// setup
expect(coreMocks.setupCore).toHaveBeenCalledTimes(1);
expect(console.log).toHaveBeenCalledWith(
"[CodSpeed] running suite packages/vitest-plugin/src/__tests__/instrumented.test.ts"
);
// run
expect(coreMocks.mongoMeasurement.start).toHaveBeenCalledWith(
"packages/vitest-plugin/src/__tests__/instrumented.test.ts::test bench"
);
expect(coreMocks.InstrumentHooks.startBenchmark).toHaveBeenCalledTimes(1);
expect(benchFn).toHaveBeenCalledTimes(8);
expect(coreMocks.InstrumentHooks.stopBenchmark).toHaveBeenCalledTimes(1);
expect(coreMocks.mongoMeasurement.stop).toHaveBeenCalledTimes(1);
expect(console.log).toHaveBeenCalledWith(
"[CodSpeed] packages/vitest-plugin/src/__tests__/instrumented.test.ts::test bench done"
);
// teardown
expect(console.log).toHaveBeenCalledWith(
"[CodSpeed] running suite packages/vitest-plugin/src/__tests__/instrumented.test.ts done"
);
expect(coreMocks.teardownCore).toHaveBeenCalledTimes(1);
});
it("should run nested suites", async () => {
const benchFn = vi.fn();
mockedGetBenchFn.mockReturnValue(benchFn);
const runner = new CodSpeedRunner(fromPartial({}));
const rootSuite = fromPartial<RunnerTestSuite>({
file: { filepath: __filename },
name: "test suite",
tasks: [
{
type: "suite",
name: "nested suite",
mode: "run",
tasks: [
{
type: "test",
mode: "run",
meta: { benchmark: true },
name: "test bench",
},
],
},
],
});
await runner.runSuite(rootSuite);
// setup
expect(coreMocks.setupCore).toHaveBeenCalledTimes(1);
expect(console.log).toHaveBeenCalledWith(
"[CodSpeed] running suite packages/vitest-plugin/src/__tests__/instrumented.test.ts"
);
// run
expect(coreMocks.mongoMeasurement.start).toHaveBeenCalledWith(
"packages/vitest-plugin/src/__tests__/instrumented.test.ts::nested suite::test bench"
);
expect(coreMocks.InstrumentHooks.startBenchmark).toHaveBeenCalledTimes(1);
expect(benchFn).toHaveBeenCalledTimes(8);
expect(coreMocks.InstrumentHooks.stopBenchmark).toHaveBeenCalledTimes(1);
expect(coreMocks.mongoMeasurement.stop).toHaveBeenCalledTimes(1);
expect(console.log).toHaveBeenCalledWith(
"[CodSpeed] packages/vitest-plugin/src/__tests__/instrumented.test.ts::nested suite::test bench done"
);
// teardown
expect(console.log).toHaveBeenCalledWith(
"[CodSpeed] running suite packages/vitest-plugin/src/__tests__/instrumented.test.ts done"
);
expect(coreMocks.teardownCore).toHaveBeenCalledTimes(1);
});
});