blob: d3876b6a13cbfcdae3ea8b693fde1353339df59f (
plain) (
blame)
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
|
---
name: Set the system time in Bun's test runner
---
Bun's test runner supports setting the system time programmatically with the `setSystemTime` function.
```ts
import { test, expect, setSystemTime } from "bun:test";
test("party like it's 1999", () => {
const date = new Date("1999-01-01T00:00:00.000Z");
setSystemTime(date); // it's now January 1, 1999
const now = new Date();
expect(now.getFullYear()).toBe(1999);
expect(now.getMonth()).toBe(0);
expect(now.getDate()).toBe(1);
});
```
---
The `setSystemTime` function is commonly used on conjunction with [Lifecycle Hooks](/docs/test/lifecycle) to configure a testing environment with a determinstic "fake clock".
```ts
import { test, expect, beforeAll, setSystemTime } from "bun:test";
beforeAll(() => {
const date = new Date("1999-01-01T00:00:00.000Z");
setSystemTime(date); // it's now January 1, 1999
});
// tests...
```
---
To reset the system clock to the actual time, call `setSystemTime` with no arguments.
```ts
import { test, expect, beforeAll, setSystemTime } from "bun:test";
setSystemTime(); // reset to actual time
```
---
See [Docs > Test Runner > Date and time](/docs/test/time) for complete documentation on mocking with the Bun test runner.
|