diff options
Diffstat (limited to 'docs/cli/test.md')
-rw-r--r-- | docs/cli/test.md | 47 |
1 files changed, 29 insertions, 18 deletions
diff --git a/docs/cli/test.md b/docs/cli/test.md index ecae45377..e76f37cdc 100644 --- a/docs/cli/test.md +++ b/docs/cli/test.md @@ -1,4 +1,10 @@ -Bun ships with a built-in test runner. +Bun ships with a fast built-in test runner. Tests are executed with the Bun runtime, and support the following features. + +- TypeScript and JSX +- Snapshot testing +- Lifecycle hooks +- Watch mode with `--watch` +- Script pre-loading with `--preload` ## Run tests @@ -29,34 +35,39 @@ You can filter the set of tests to run by passing additional positional argument $ bun test <filter> <filter> ... ``` -## Snapshot testing - -Snapshots are supported by `bun test`. First, write a test using the `.toMatchSnapshot()` matcher: - -```ts -import { test, expect } from "bun:test"; +The test runner runs all tests in a single process. It loads all `--preload` scripts (see [Lifecycle](/docs/test/lifecycle) for details), then runs all tests. If a test fails, the test runner will exit with a non-zero exit code. -test("snap", () => { - expect("foo").toMatchSnapshot(); -}); -``` +## Watch mode -Then generate snapshots with the following command: +Similar to `bun run`, you can pass the `--watch` flag to `bun test` to watch for changes and re-run tests. ```bash -bun test --update-snapshots +$ bun test --watch ``` -Snapshots will be stored in a `__snapshots__` directory next to the test file. +## Lifecycle hooks -## Watch mode +Bun supports the following lifecycle hooks: -Similar to `bun run`, you can pass the `--watch` flag to `bun test` to watch for changes and re-run tests. +| Hook | Description | +| ------------ | --------------------------- | +| `beforeAll` | Runs once before all tests. | +| `beforeEach` | Runs before each test. | +| `afterEach` | Runs after each test. | +| `afterAll` | Runs once after all tests. | -```bash -$ bun test --watch +These hooks can be define inside test files, or in a separate file that is preloaded with the `--preload` flag. + +```ts +$ bun test --preload ./setup.ts ``` +See [Test > Lifecycle](/docs/test/lifecycle) for complete documentation. + +## Snapshot testing + +Snapshots are supported by `bun test`. See [Test > Snapshots](/docs/test/snapshots) for complete documentation. + ## Performance Bun's test runner is fast. |