mockPerformance()
mockPerformance(
overrides?): MockPerformanceResult
Creates a mock performance object on globalThis for Node.js tests.
note
Always call restore() for cleanup. Auto-restored after each test if forgotten.
Parametersβ
overrides?: Partial<Performance> = {}β
Properties to override on the mock performance.
Returns: MockPerformanceResultβ
Object containing the mock performance and restore function.
Sinceβ
2.0.0
Exampleβ
const { performance, restore } = mockPerformance({
now: vi.fn(() => 12345),
});
expect(performance.now()).toBe(12345);
restore();
Use Casesβ
Control timing πβ
Mock performance.now() to return deterministic timestamps.
Critical for testing animations, timeouts, or performance markers.
const { restore } = mockPerformance({
now: () => 1000
});
const time = performance.now(); // Always 1000
restore();
Test duration calculationsβ
Verify elapsed time logic with predictable values.
let time = 0;
const { restore } = mockPerformance({
now: () => time
});
const start = performance.now();
time = 500;
const elapsed = performance.now() - start;
expect(elapsed).toBe(500);
restore();
MockPerformanceResultβ
Result of mockPerformance().
Sinceβ
2.0.0
Propertiesβ
performance: Performanceβ
restore(): () => voidβ
Returnsβ
void