1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
`bun:test` lets you change what time it is in your tests. This was introduced in Bun v0.6.13.
This works with any of the following:
- `Date.now`
- `new Date()`
- `new Intl.DateTimeFormat().format()`
Timers are not impacted yet, but may be in a future release of Bun.
## `setSystemTime`
To change the system time, use `setSystemTime`:
```ts
import { setSystemTime, beforeAll } from "bun:test";
beforeAll(() => {
setSystemTime(new Date("2020-01-01T00:00:00.000Z"));
});
test("it is 2020", () => {
expect(new Date().getFullYear()).toBe(2020);
});
```
### Reset the system time
To reset the system time, pass no arguments to `setSystemTime`:
```ts
import { setSystemTime, beforeAll } from "bun:test";
test("it was 2020, for a moment.", () => {
setSystemTime(new Date("2020-01-01T00:00:00.000Z"));
expect(new Date().getFullYear()).toBe(2020);
setSystemTime();
expect(new Date().getFullYear()).toBeGreaterThan(2020);
});
```
## Set the time zone
To change the time zone, either pass the `$TZ` environment variable to your test runner, or set `process.env.TZ` at runtime:
```ts
import { test, expect } from "bun:test";
test("Welcome to California!", () => {
process.env.TZ = "America/Los_Angeles";
expect(new Date().getTimezoneOffset()).toBe(420);
expect(new Intl.DateTimeFormat().resolvedOptions().timeZone).toBe(
"America/Los_Angeles",
);
});
test("Welcome to New York!", () => {
process.env.TZ = "America/New_York";
expect(new Date().getTimezoneOffset()).toBe(240);
expect(new Intl.DateTimeFormat().resolvedOptions().timeZone).toBe(
"America/New_York",
);
});
```
|