aboutsummaryrefslogtreecommitdiff
path: root/docs/test/lifecycle.md
blob: ccb7ce85d94a466a4a99969dae9e491111a1c116 (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
The test runner supports the following lifecycle hooks. This is useful for loading test fixtures, mocking data, and configuring the test environment.

| Hook         | Description                 |
| ------------ | --------------------------- |
| `beforeAll`  | Runs once before all tests. |
| `beforeEach` | Runs before each test.      |
| `afterEach`  | Runs after each test.       |
| `afterAll`   | Runs once after all tests.  |

Perform per-test setup and teardown logic with `beforeEach` and `afterEach`.

```ts
import { expect, test } from "bun:test";

beforeEach(() => {
  console.log("running test.");
});

afterEach(() => {
  console.log("done with test.");
});

// tests...
```

Perform per-scope setup and teardown logic with `beforeAll` and `afterAll`. The _scope_ is determined by where the hook is defined.

To scope the hooks to a particular `describe` block:

```ts
import { describe, beforeAll } from "bun:test";

describe("test group", () => {
  beforeAll(() => {
    // setup
  });

  // tests...
});
```

To scope the hooks to a test file:

```ts
import { describe, beforeAll } from "bun:test";

describe("test group", () => {
  beforeAll(() => {
    // setup
  });

  // tests...
});
```

To scope the hooks to an entire multi-file test run, define the hooks in a separate file.

```ts#setup.ts
import { beforeAll, afterAll } from "bun:test";

beforeAll(() => {
  // global setup
});

afterAll(() => {
  // global teardown
});
```

Then use `--preload` to run the setup script before any test files.

```ts
bun test --preload ./setup.ts
```

To avoid typing `--preload` every time you run tests, it can be added to your `bunfig.toml`:

```toml
[test]
preload = ["./setup.ts"]
```
0'>2022-03-02[bun run] Set more environment variablesGravatar Jarred Sumner 2-0/+55 2022-03-02clean up error message when CLI flag is invalidGravatar Jarred Sumner 1-1/+4 2022-03-02add `bun pm cache` and `bun pm cache rm` commandsGravatar Jarred Sumner 1-0/+20 2022-03-01[bun.js] `ResolveError.prototype.toString()` `BuildError.prototype.toString()`Gravatar Jarred Sumner 1-2/+94 2022-03-01add `allowBunRuntime` and `autoImportJSX` flags to Bun.TranspilerGravatar Jarred Sumner 1-1/+32 2022-03-01cleanup code that checks if it should send an HTTP bodyGravatar Jarred Sumner 2-6/+18 2022-03-01[JS Parser] Fix bug with `super` from adding class static blocksGravatar Jarred Sumner 1-1/+12 2022-03-01Update bundler.zigGravatar Jarred Sumner 1-1/+5 2022-03-01Remove unused boolGravatar Jarred Sumner 1-3/+0 2022-03-01[bun.js] Allow disabling runtime imports so bun can build for nodeGravatar Jarred Sumner 3-5/+11 2022-03-01[JS Parser] Make auto importing JSX a flag so the API is easierGravatar Jarred Sumner 1-295/+297 2022-03-01cleanupGravatar Jarred Sumner 1-2/+2 2022-03-01Update javascript.zigGravatar Jarred Sumner 1-50/+0 2022-03-01[bun.js] shim async fsGravatar Jarred Sumner 2-36/+226 2022-03-01[bun.js] Implement `setTimeout`, `setInterval`, `clearTimeout`, `clearInterval`Gravatar Jarred Sumner 11-41/+295 2022-02-27Update transpiler.test.jsGravatar Jarred Sumner 1-0/+8 2022-02-27[TS] Make `export {type foo}` output consistent with TS parserGravatar Jarred Sumner 1-3/+45 2022-02-27WASMGravatar Jarred Sumner 83-690/+10789