From 9da9bac30cc0e1fb09181c20a10d24eb6d4609f8 Mon Sep 17 00:00:00 2001 From: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> Date: Sun, 2 Jul 2023 23:18:58 -0700 Subject: Update time.md --- docs/test/time.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'docs/test') diff --git a/docs/test/time.md b/docs/test/time.md index 1cc841fb9..678312903 100644 --- a/docs/test/time.md +++ b/docs/test/time.md @@ -24,6 +24,20 @@ test("it is 2020", () => { }); ``` +To support existing tests that use Jest's `useFakeTimers` and `useRealTimers`, you can use `useFakeTimers` and `useRealTimers`: + +```ts +test("just like in jest", () => { + jest.useFakeTimers(); + jest.setSystemTime(new Date("2020-01-01T00:00:00.000Z")); + expect(new Date().getFullYear()).toBe(2020); + jest.useRealTimers(); + expect(new Date().getFullYear()).toBeGreaterThan(2020); +}); +``` + +Note that we have not implemented builtin support for mocking timers yet, but this is on the roadmap. + ### Reset the system time To reset the system time, pass no arguments to `setSystemTime`: @@ -32,9 +46,13 @@ To reset the system time, pass no arguments to `setSystemTime`: import { setSystemTime, beforeAll } from "bun:test"; test("it was 2020, for a moment.", () => { + // Set it to something! setSystemTime(new Date("2020-01-01T00:00:00.000Z")); expect(new Date().getFullYear()).toBe(2020); + + // reset it! setSystemTime(); + expect(new Date().getFullYear()).toBeGreaterThan(2020); }); ``` -- cgit v1.2.3