mockPerformance()
mockPerformance(
overrides): MockPerformanceResult
Creates a mock performance object on globalThis for Node.js tests.
remarque
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