If you want to mimic the behavior of new Date
in Deno, one way to do it is by using @sinonjs/fake-timers
import FakeTimers from "npm:@sinonjs/fake-timers";
import { assertEquals, assertNotEquals } from "https://deno.land/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f2818696b2c2dcc3cbc2dcc2">[email protected]</a>/testing/asserts.ts";
Deno.test("New Year", () => {
const clock = FakeTimers.install();
clock.setSystemTime(new Date('2020-01-01'));
// Celebrating New Year
assertEquals(new Date().toDateString(), 'Wed Jan 01 2020');
clock.uninstall();
});
Deno.test("now", () => {
// Not yet New Year
assertNotEquals(new Date().toDateString(), 'Wed Jan 01 2020');
});
For those already using jest, you can opt for jest.useFakeTimers()
along with .setSystemTime(now?: number | Date)
jest
.useFakeTimers()
.setSystemTime(new Date('2020-01-01'));
Remember to switch back to real timers when finished by calling jest.useRealTimers()