Added date faker

This commit is contained in:
Thom Werring 2024-03-23 15:03:53 +01:00
parent 40fa6f7ef3
commit 5874cb901b
3 changed files with 37 additions and 1 deletions

View File

@ -1,6 +1,9 @@
import { NestFactory } from '@nestjs/core'; import { NestFactory } from '@nestjs/core';
import { AppModule } from '@/app.module'; import { AppModule } from '@/app.module';
import { openApi } from '@/openApi'; import { openApi } from '@/utils/openApi';
import { setDate } from '@/utils/fake-date';
setDate('2023-02-14 14:00:00 UTC');
async function bootstrap() { async function bootstrap() {
const app = await NestFactory.create(AppModule); const app = await NestFactory.create(AppModule);
@ -9,4 +12,5 @@ async function bootstrap() {
await app.listen(3000); await app.listen(3000);
} }
bootstrap(); bootstrap();

32
src/utils/fake-date.ts Normal file
View File

@ -0,0 +1,32 @@
// Save an instance of the original Date object
const ODate = Date;
export function setDate(date: string) {
console.warn(`Time mocking has been set to ${date}`);
Date = new Proxy(Date, {
construct(
Target: DateConstructor,
argArray: Parameters<DateConstructor>,
): Date {
if (argArray.length === 0) {
return new Target(date);
}
return new Target(...argArray);
},
get(Target, prop) {
if (prop === 'now') {
return () => new Target(date).valueOf();
}
return Reflect.get(Target, prop);
},
});
}
export function resetDate() {
console.warn(`Time mocking is now disabled`);
Date = ODate;
}