mirror of
https://github.com/vale981/apollo-server
synced 2025-03-06 10:11:40 -05:00
33 lines
548 B
TypeScript
33 lines
548 B
TypeScript
![]() |
const RealDate = global.Date;
|
||
|
|
||
|
export function mockDate() {
|
||
|
global.Date = new Proxy(RealDate, handler);
|
||
|
}
|
||
|
|
||
|
export function unmockDate() {
|
||
|
global.Date = RealDate;
|
||
|
}
|
||
|
|
||
|
let now = Date.now();
|
||
|
|
||
|
export function advanceTimeBy(ms: number) {
|
||
|
now += ms;
|
||
|
}
|
||
|
|
||
|
const handler = {
|
||
|
construct(target, args) {
|
||
|
if (args.length === 0) {
|
||
|
return new Date(now);
|
||
|
} else {
|
||
|
return new target(...args);
|
||
|
}
|
||
|
},
|
||
|
get(target, propKey) {
|
||
|
if (propKey === 'now') {
|
||
|
return () => now;
|
||
|
} else {
|
||
|
return target[propKey];
|
||
|
}
|
||
|
},
|
||
|
};
|