-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathindex.unit.test.ts
More file actions
62 lines (49 loc) · 1.91 KB
/
index.unit.test.ts
File metadata and controls
62 lines (49 loc) · 1.91 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
import { Bench } from "tinybench";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { withCodSpeed } from ".";
const mockInstrumented = vi.hoisted(() => ({
setupCodspeedInstrumentedBench: vi.fn(),
}));
vi.mock("./instrumented", () => ({
...mockInstrumented,
}));
const mockWalltime = vi.hoisted(() => ({
setupCodspeedWalltimeBench: vi.fn(),
}));
vi.mock("./walltime", () => ({
...mockWalltime,
}));
describe("withCodSpeed behavior without different codspeed modes", () => {
beforeEach(() => {
vi.clearAllMocks();
// Ensure no CODSPEED env vars are set
delete process.env.CODSPEED_ENV;
delete process.env.CODSPEED_RUNNER_MODE;
});
it("should return the same bench instance unchanged when no CODSPEED_ENV", async () => {
const originalBench = new Bench({ iterations: 10, time: 10 });
const wrappedBench = withCodSpeed(originalBench);
const shouldBeCalled = vi.fn();
wrappedBench.add("test task", shouldBeCalled);
await wrappedBench.run();
// Should return the exact same instance
expect(wrappedBench).toBe(originalBench);
expect(shouldBeCalled.mock.calls.length).toBeGreaterThan(1000);
});
it("should run in instrumented mode when CODSPEED_RUNNER_MODE=instrumentation", async () => {
process.env.CODSPEED_ENV = "true";
process.env.CODSPEED_RUNNER_MODE = "instrumentation";
withCodSpeed(new Bench());
expect(mockInstrumented.setupCodspeedInstrumentedBench).toHaveBeenCalled();
expect(mockWalltime.setupCodspeedWalltimeBench).not.toHaveBeenCalled();
});
it("should run in walltime mode when CODSPEED_RUNNER_MODE=walltime", async () => {
process.env.CODSPEED_ENV = "true";
process.env.CODSPEED_RUNNER_MODE = "walltime";
withCodSpeed(new Bench());
expect(
mockInstrumented.setupCodspeedInstrumentedBench
).not.toHaveBeenCalled();
expect(mockWalltime.setupCodspeedWalltimeBench).toHaveBeenCalled();
});
});