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