diff options
author | 2023-07-02 23:18:58 -0700 | |
---|---|---|
committer | 2023-07-02 23:18:58 -0700 | |
commit | 9da9bac30cc0e1fb09181c20a10d24eb6d4609f8 (patch) | |
tree | db0b2b7bc3f4ee837946f5a118371508839fc39f | |
parent | cd243f40eece9d70d901672086e5938188c27e83 (diff) | |
download | bun-9da9bac30cc0e1fb09181c20a10d24eb6d4609f8.tar.gz bun-9da9bac30cc0e1fb09181c20a10d24eb6d4609f8.tar.zst bun-9da9bac30cc0e1fb09181c20a10d24eb6d4609f8.zip |
Update time.md
-rw-r--r-- | docs/test/time.md | 18 |
1 files changed, 18 insertions, 0 deletions
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); }); ``` |