diff options
author | 2023-07-02 23:46:08 -0700 | |
---|---|---|
committer | 2023-07-02 23:46:08 -0700 | |
commit | 1206352b4ac8b12fffb497eab82f5e3db8ed0871 (patch) | |
tree | 7af6908c34cbeade5e3778c7994905034e028776 | |
parent | 9da9bac30cc0e1fb09181c20a10d24eb6d4609f8 (diff) | |
download | bun-1206352b4ac8b12fffb497eab82f5e3db8ed0871.tar.gz bun-1206352b4ac8b12fffb497eab82f5e3db8ed0871.tar.zst bun-1206352b4ac8b12fffb497eab82f5e3db8ed0871.zip |
Update time.md
-rw-r--r-- | docs/test/time.md | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/docs/test/time.md b/docs/test/time.md index 678312903..dd23f788f 100644 --- a/docs/test/time.md +++ b/docs/test/time.md @@ -34,6 +34,21 @@ test("just like in jest", () => { jest.useRealTimers(); expect(new Date().getFullYear()).toBeGreaterThan(2020); }); + +test("unlike in jest", () => { + const OriginalDate = Date; + jest.useFakeTimers(); + if (typeof Bun === "undefined") { + // In Jest, the Date constructor changes + // That can cause all sorts of bugs because suddenly Date !== Date before the test. + expect(Date).not.toBe(OriginalDate); + expect(Date.now).not.toBe(OriginalDate.now); + } else { + // In bun:test, Date constructor does not change when you useFakeTimers + expect(Date).toBe(OriginalDate); + expect(Date.now).toBe(OriginalDate.now); + } +}); ``` Note that we have not implemented builtin support for mocking timers yet, but this is on the roadmap. @@ -73,6 +88,7 @@ test("Welcome to California!", () => { }); test("Welcome to New York!", () => { + // Unlike in jest, you can set the timezone multiple times at runtime and it will work. process.env.TZ = "America/New_York"; expect(new Date().getTimezoneOffset()).toBe(240); expect(new Intl.DateTimeFormat().resolvedOptions().timeZone).toBe( |