120
return new (OriginalDate as new (...a: unknown[]) => Date)(...args);
121
}
122
>
// Static-property tagging. Use a typed `Record` view of the function
virtualTimeApi.ts
123
>
// rather than `any`. We skip non-writable own properties (`length`,
124
>
// `name` on a function would throw) and then explicitly set the few
125
>
// statics callers reach for.
126
>
const dateStatics = VirtualDate as unknown as Record<string, unknown>;
127
>
const originalStatics = OriginalDate as unknown as Record<string, unknown>;
128
>
for (const key of Object.getOwnPropertyNames(OriginalDate)) {
129
>
const desc = Object.getOwnPropertyDescriptor(OriginalDate, key);
130
>
if (desc && (desc.writable || desc.set)) {
131
>
dateStatics[key] = originalStatics[key];
132
>
}
133
>
}
134
>
dateStatics.now = () => clock.now;
135
>
dateStatics.parse = OriginalDate.parse;
136
>
dateStatics.UTC = OriginalDate.UTC;
137
>
VirtualDate.prototype = OriginalDate.prototype;
138
>
139
>
const api: TimeApi = {
140
>
setTimeout: virtualSetTimeout as unknown as TimeApi['setTimeout'],
141
>
clearTimeout: virtualClearTimeout,
142
>
setInterval: virtualSetInterval as unknown as TimeApi['setInterval'],
143
>
clearInterval: virtualClearInterval,
144
>
Date: VirtualDate as unknown as DateConstructor,
145
>
};
146
>
147
>
// Expose the real setTimeout as `originalFn` on the virtual one. The
148
>
// component-explorer host's polling loop reads this to escape virtual
149
>
// time when waiting for renders to settle.
150
>
(api.setTimeout as unknown as { originalFn: TimeApi['setTimeout'] }).originalFn = realTimeApi.setTimeout;
151
>
152
>
if (options?.fakeRequestAnimationFrame) {
153
let rafIdCounter = 0;
154
const rafDisposables = new Map<number, IDisposable>();