-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathclock.setup-file.ts
More file actions
37 lines (30 loc) · 949 Bytes
/
clock.setup-file.ts
File metadata and controls
37 lines (30 loc) · 949 Bytes
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
import { type MockInstance, afterEach, beforeEach, vi } from 'vitest';
const MOCK_DATE_NOW_MS = 1_000_000;
const MOCK_TIME_ORIGIN = 500_000;
const dateNow = MOCK_DATE_NOW_MS;
const performanceTimeOrigin = MOCK_TIME_ORIGIN;
/* eslint-disable functional/no-let */
let dateNowSpy: MockInstance<[], number> | undefined;
let performanceNowSpy: MockInstance<[], number> | undefined;
/* eslint-enable functional/no-let */
beforeEach(() => {
dateNowSpy = vi.spyOn(Date, 'now').mockReturnValue(dateNow);
performanceNowSpy = vi
.spyOn(performance, 'now')
.mockReturnValue(dateNow - performanceTimeOrigin);
vi.stubGlobal('performance', {
...performance,
timeOrigin: performanceTimeOrigin,
});
});
afterEach(() => {
vi.unstubAllGlobals();
if (dateNowSpy) {
dateNowSpy.mockRestore();
dateNowSpy = undefined;
}
if (performanceNowSpy) {
performanceNowSpy.mockRestore();
performanceNowSpy = undefined;
}
});