bun-types 1.3.2-canary.20251106T140813 → 1.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bun.d.ts +102 -6
- package/docs/bundler/bytecode.mdx +465 -0
- package/docs/bundler/css.mdx +1024 -0
- package/docs/bundler/esbuild.mdx +253 -0
- package/docs/bundler/executables.mdx +535 -0
- package/docs/bundler/fullstack.mdx +1064 -0
- package/docs/bundler/hot-reloading.mdx +229 -0
- package/docs/bundler/html-static.mdx +386 -0
- package/docs/bundler/index.mdx +1499 -0
- package/docs/bundler/loaders.mdx +356 -0
- package/docs/bundler/macros.mdx +328 -0
- package/docs/bundler/minifier.mdx +1306 -0
- package/docs/bundler/plugins.mdx +411 -0
- package/docs/feedback.mdx +85 -0
- package/docs/guides/binary/arraybuffer-to-array.mdx +29 -0
- package/docs/guides/binary/arraybuffer-to-blob.mdx +26 -0
- package/docs/guides/binary/arraybuffer-to-buffer.mdx +27 -0
- package/docs/guides/binary/arraybuffer-to-string.mdx +17 -0
- package/docs/guides/binary/arraybuffer-to-typedarray.mdx +41 -0
- package/docs/guides/binary/blob-to-arraybuffer.mdx +16 -0
- package/docs/guides/binary/blob-to-dataview.mdx +16 -0
- package/docs/guides/binary/blob-to-stream.mdx +16 -0
- package/docs/guides/binary/blob-to-string.mdx +17 -0
- package/docs/guides/binary/blob-to-typedarray.mdx +16 -0
- package/docs/guides/binary/buffer-to-arraybuffer.mdx +16 -0
- package/docs/guides/binary/buffer-to-blob.mdx +16 -0
- package/docs/guides/binary/buffer-to-readablestream.mdx +43 -0
- package/docs/guides/binary/buffer-to-string.mdx +27 -0
- package/docs/guides/binary/buffer-to-typedarray.mdx +16 -0
- package/docs/guides/binary/dataview-to-string.mdx +17 -0
- package/docs/guides/binary/typedarray-to-arraybuffer.mdx +27 -0
- package/docs/guides/binary/typedarray-to-blob.mdx +18 -0
- package/docs/guides/binary/typedarray-to-buffer.mdx +16 -0
- package/docs/guides/binary/typedarray-to-dataview.mdx +16 -0
- package/docs/guides/binary/typedarray-to-readablestream.mdx +43 -0
- package/docs/guides/binary/typedarray-to-string.mdx +18 -0
- package/docs/guides/deployment/aws-lambda.mdx +204 -0
- package/docs/guides/deployment/digital-ocean.mdx +161 -0
- package/docs/guides/deployment/google-cloud-run.mdx +197 -0
- package/docs/guides/deployment/railway.mdx +145 -0
- package/docs/guides/deployment/render.mdx +82 -0
- package/docs/guides/deployment/vercel.mdx +99 -0
- package/docs/guides/ecosystem/astro.mdx +82 -0
- package/docs/guides/ecosystem/discordjs.mdx +80 -0
- package/docs/guides/ecosystem/docker.mdx +151 -0
- package/docs/guides/ecosystem/drizzle.mdx +195 -0
- package/docs/guides/ecosystem/edgedb.mdx +257 -0
- package/docs/guides/ecosystem/elysia.mdx +31 -0
- package/docs/guides/ecosystem/express.mdx +43 -0
- package/docs/guides/ecosystem/hono.mdx +47 -0
- package/docs/guides/ecosystem/mongoose.mdx +92 -0
- package/docs/guides/ecosystem/neon-drizzle.mdx +234 -0
- package/docs/guides/ecosystem/neon-serverless-postgres.mdx +60 -0
- package/docs/guides/ecosystem/nextjs.mdx +57 -0
- package/docs/guides/ecosystem/nuxt.mdx +90 -0
- package/docs/guides/ecosystem/pm2.mdx +55 -0
- package/docs/guides/ecosystem/prisma-postgres.mdx +169 -0
- package/docs/guides/ecosystem/prisma.mdx +164 -0
- package/docs/guides/ecosystem/qwik.mdx +114 -0
- package/docs/guides/ecosystem/react.mdx +52 -0
- package/docs/guides/ecosystem/remix.mdx +97 -0
- package/docs/guides/ecosystem/sentry.mdx +54 -0
- package/docs/guides/ecosystem/solidstart.mdx +66 -0
- package/docs/guides/ecosystem/ssr-react.mdx +49 -0
- package/docs/guides/ecosystem/stric.mdx +54 -0
- package/docs/guides/ecosystem/sveltekit.mdx +138 -0
- package/docs/guides/ecosystem/systemd.mdx +114 -0
- package/docs/guides/ecosystem/upstash.mdx +87 -0
- package/docs/guides/ecosystem/vite.mdx +77 -0
- package/docs/guides/html-rewriter/extract-links.mdx +72 -0
- package/docs/guides/html-rewriter/extract-social-meta.mdx +97 -0
- package/docs/guides/http/cluster.mdx +69 -0
- package/docs/guides/http/fetch-unix.mdx +35 -0
- package/docs/guides/http/fetch.mdx +26 -0
- package/docs/guides/http/file-uploads.mdx +97 -0
- package/docs/guides/http/hot.mdx +28 -0
- package/docs/guides/http/proxy.mdx +26 -0
- package/docs/guides/http/server.mdx +48 -0
- package/docs/guides/http/simple.mdx +20 -0
- package/docs/guides/http/stream-file.mdx +50 -0
- package/docs/guides/http/stream-iterator.mdx +49 -0
- package/docs/guides/http/stream-node-streams-in-bun.mdx +22 -0
- package/docs/guides/http/tls.mdx +32 -0
- package/docs/guides/index.mdx +10 -0
- package/docs/guides/install/add-dev.mdx +28 -0
- package/docs/guides/install/add-git.mdx +38 -0
- package/docs/guides/install/add-optional.mdx +27 -0
- package/docs/guides/install/add-peer.mdx +45 -0
- package/docs/guides/install/add-tarball.mdx +35 -0
- package/docs/guides/install/add.mdx +44 -0
- package/docs/guides/install/azure-artifacts.mdx +76 -0
- package/docs/guides/install/cicd.mdx +43 -0
- package/docs/guides/install/custom-registry.mdx +32 -0
- package/docs/guides/install/from-npm-install-to-bun-install.mdx +230 -0
- package/docs/guides/install/git-diff-bun-lockfile.mdx +47 -0
- package/docs/guides/install/jfrog-artifactory.mdx +28 -0
- package/docs/guides/install/npm-alias.mdx +25 -0
- package/docs/guides/install/registry-scope.mdx +40 -0
- package/docs/guides/install/trusted.mdx +50 -0
- package/docs/guides/install/workspaces.mdx +70 -0
- package/docs/guides/install/yarnlock.mdx +50 -0
- package/docs/guides/process/argv.mdx +66 -0
- package/docs/guides/process/ctrl-c.mdx +18 -0
- package/docs/guides/process/ipc.mdx +69 -0
- package/docs/guides/process/nanoseconds.mdx +15 -0
- package/docs/guides/process/os-signals.mdx +41 -0
- package/docs/guides/process/spawn-stderr.mdx +34 -0
- package/docs/guides/process/spawn-stdout.mdx +28 -0
- package/docs/guides/process/spawn.mdx +43 -0
- package/docs/guides/process/stdin.mdx +62 -0
- package/docs/guides/read-file/arraybuffer.mdx +30 -0
- package/docs/guides/read-file/buffer.mdx +21 -0
- package/docs/guides/read-file/exists.mdx +18 -0
- package/docs/guides/read-file/json.mdx +19 -0
- package/docs/guides/read-file/mime.mdx +22 -0
- package/docs/guides/read-file/stream.mdx +28 -0
- package/docs/guides/read-file/string.mdx +24 -0
- package/docs/guides/read-file/uint8array.mdx +23 -0
- package/docs/guides/read-file/watch.mdx +66 -0
- package/docs/guides/runtime/build-time-constants.mdx +295 -0
- package/docs/guides/runtime/cicd.mdx +45 -0
- package/docs/guides/runtime/codesign-macos-executable.mdx +61 -0
- package/docs/guides/runtime/define-constant.mdx +149 -0
- package/docs/guides/runtime/delete-directory.mdx +39 -0
- package/docs/guides/runtime/delete-file.mdx +21 -0
- package/docs/guides/runtime/heap-snapshot.mdx +28 -0
- package/docs/guides/runtime/import-html.mdx +17 -0
- package/docs/guides/runtime/import-json.mdx +46 -0
- package/docs/guides/runtime/import-toml.mdx +32 -0
- package/docs/guides/runtime/import-yaml.mdx +104 -0
- package/docs/guides/runtime/read-env.mdx +37 -0
- package/docs/guides/runtime/set-env.mdx +51 -0
- package/docs/guides/runtime/shell.mdx +42 -0
- package/docs/guides/runtime/timezone.mdx +38 -0
- package/docs/guides/runtime/tsconfig-paths.mdx +31 -0
- package/docs/guides/runtime/typescript.mdx +51 -0
- package/docs/guides/runtime/vscode-debugger.mdx +48 -0
- package/docs/guides/runtime/web-debugger.mdx +103 -0
- package/docs/guides/streams/node-readable-to-arraybuffer.mdx +13 -0
- package/docs/guides/streams/node-readable-to-blob.mdx +13 -0
- package/docs/guides/streams/node-readable-to-json.mdx +14 -0
- package/docs/guides/streams/node-readable-to-string.mdx +14 -0
- package/docs/guides/streams/node-readable-to-uint8array.mdx +13 -0
- package/docs/guides/streams/to-array.mdx +16 -0
- package/docs/guides/streams/to-arraybuffer.mdx +16 -0
- package/docs/guides/streams/to-blob.mdx +16 -0
- package/docs/guides/streams/to-buffer.mdx +17 -0
- package/docs/guides/streams/to-json.mdx +16 -0
- package/docs/guides/streams/to-string.mdx +16 -0
- package/docs/guides/streams/to-typedarray.mdx +24 -0
- package/docs/guides/test/bail.mdx +24 -0
- package/docs/guides/test/coverage-threshold.mdx +67 -0
- package/docs/guides/test/coverage.mdx +49 -0
- package/docs/guides/test/happy-dom.mdx +73 -0
- package/docs/guides/test/migrate-from-jest.mdx +125 -0
- package/docs/guides/test/mock-clock.mdx +50 -0
- package/docs/guides/test/mock-functions.mdx +70 -0
- package/docs/guides/test/rerun-each.mdx +16 -0
- package/docs/guides/test/run-tests.mdx +116 -0
- package/docs/guides/test/skip-tests.mdx +43 -0
- package/docs/guides/test/snapshot.mdx +102 -0
- package/docs/guides/test/spy-on.mdx +49 -0
- package/docs/guides/test/svelte-test.mdx +113 -0
- package/docs/guides/test/testing-library.mdx +93 -0
- package/docs/guides/test/timeout.mdx +17 -0
- package/docs/guides/test/todo-tests.mdx +74 -0
- package/docs/guides/test/update-snapshots.mdx +49 -0
- package/docs/guides/test/watch-mode.mdx +24 -0
- package/docs/guides/util/base64.mdx +17 -0
- package/docs/guides/util/deep-equals.mdx +41 -0
- package/docs/guides/util/deflate.mdx +20 -0
- package/docs/guides/util/detect-bun.mdx +25 -0
- package/docs/guides/util/entrypoint.mdx +19 -0
- package/docs/guides/util/escape-html.mdx +24 -0
- package/docs/guides/util/file-url-to-path.mdx +16 -0
- package/docs/guides/util/gzip.mdx +20 -0
- package/docs/guides/util/hash-a-password.mdx +56 -0
- package/docs/guides/util/import-meta-dir.mdx +15 -0
- package/docs/guides/util/import-meta-file.mdx +15 -0
- package/docs/guides/util/import-meta-path.mdx +15 -0
- package/docs/guides/util/javascript-uuid.mdx +25 -0
- package/docs/guides/util/main.mdx +43 -0
- package/docs/guides/util/path-to-file-url.mdx +16 -0
- package/docs/guides/util/sleep.mdx +24 -0
- package/docs/guides/util/version.mdx +23 -0
- package/docs/guides/util/which-path-to-executable-bin.mdx +17 -0
- package/docs/guides/websocket/compression.mdx +33 -0
- package/docs/guides/websocket/context.mdx +74 -0
- package/docs/guides/websocket/pubsub.mdx +40 -0
- package/docs/guides/websocket/simple.mdx +35 -0
- package/docs/guides/write-file/append.mdx +54 -0
- package/docs/guides/write-file/basic.mdx +46 -0
- package/docs/guides/write-file/blob.mdx +30 -0
- package/docs/guides/write-file/cat.mdx +19 -0
- package/docs/guides/write-file/file-cp.mdx +18 -0
- package/docs/guides/write-file/filesink.mdx +54 -0
- package/docs/guides/write-file/response.mdx +19 -0
- package/docs/guides/write-file/stdout.mdx +23 -0
- package/docs/guides/write-file/stream.mdx +19 -0
- package/docs/guides/write-file/unlink.mdx +18 -0
- package/docs/index.mdx +133 -0
- package/docs/installation.mdx +365 -0
- package/docs/pm/bunx.mdx +83 -0
- package/docs/pm/catalogs.mdx +292 -0
- package/docs/pm/cli/add.mdx +179 -0
- package/docs/pm/cli/audit.mdx +60 -0
- package/docs/pm/cli/install.mdx +471 -0
- package/docs/pm/cli/link.mdx +48 -0
- package/docs/pm/cli/outdated.mdx +197 -0
- package/docs/pm/cli/patch.mdx +69 -0
- package/docs/pm/cli/pm.mdx +319 -0
- package/docs/pm/cli/publish.mdx +123 -0
- package/docs/pm/cli/remove.mdx +16 -0
- package/docs/pm/cli/update.mdx +140 -0
- package/docs/pm/cli/why.mdx +84 -0
- package/docs/pm/filter.mdx +102 -0
- package/docs/pm/global-cache.mdx +72 -0
- package/docs/pm/isolated-installs.mdx +205 -0
- package/docs/pm/lifecycle.mdx +57 -0
- package/docs/pm/lockfile.mdx +64 -0
- package/docs/pm/npmrc.mdx +111 -0
- package/docs/pm/overrides.mdx +83 -0
- package/docs/pm/scopes-registries.mdx +35 -0
- package/docs/pm/security-scanner-api.mdx +95 -0
- package/docs/pm/workspaces.mdx +109 -0
- package/docs/project/benchmarking.mdx +218 -0
- package/docs/project/bindgen.mdx +223 -0
- package/docs/project/building-windows.mdx +133 -0
- package/docs/project/contributing.mdx +343 -0
- package/docs/project/feedback.mdx +20 -0
- package/docs/project/license.mdx +78 -0
- package/docs/project/roadmap.mdx +8 -0
- package/docs/quickstart.mdx +240 -0
- package/docs/runtime/auto-install.mdx +97 -0
- package/docs/runtime/binary-data.mdx +846 -0
- package/docs/runtime/bun-apis.mdx +59 -0
- package/docs/runtime/bunfig.mdx +642 -0
- package/docs/runtime/c-compiler.mdx +204 -0
- package/docs/runtime/child-process.mdx +532 -0
- package/docs/runtime/color.mdx +267 -0
- package/docs/runtime/console.mdx +67 -0
- package/docs/runtime/cookies.mdx +454 -0
- package/docs/runtime/debugger.mdx +335 -0
- package/docs/runtime/environment-variables.mdx +214 -0
- package/docs/runtime/ffi.mdx +565 -0
- package/docs/runtime/file-io.mdx +306 -0
- package/docs/runtime/file-system-router.mdx +118 -0
- package/docs/runtime/file-types.mdx +354 -0
- package/docs/runtime/glob.mdx +181 -0
- package/docs/runtime/globals.mdx +72 -0
- package/docs/runtime/hashing.mdx +315 -0
- package/docs/runtime/html-rewriter.mdx +340 -0
- package/docs/runtime/http/cookies.mdx +79 -0
- package/docs/runtime/http/error-handling.mdx +40 -0
- package/docs/runtime/http/metrics.mdx +36 -0
- package/docs/runtime/http/routing.mdx +289 -0
- package/docs/runtime/http/server.mdx +647 -0
- package/docs/runtime/http/tls.mdx +101 -0
- package/docs/runtime/http/websockets.mdx +404 -0
- package/docs/runtime/index.mdx +223 -0
- package/docs/runtime/jsx.mdx +115 -0
- package/docs/runtime/module-resolution.mdx +342 -0
- package/docs/runtime/networking/dns.mdx +111 -0
- package/docs/runtime/networking/fetch.mdx +468 -0
- package/docs/runtime/networking/tcp.mdx +239 -0
- package/docs/runtime/networking/udp.mdx +129 -0
- package/docs/runtime/node-api.mdx +19 -0
- package/docs/runtime/nodejs-compat.mdx +468 -0
- package/docs/runtime/plugins.mdx +405 -0
- package/docs/runtime/redis.mdx +582 -0
- package/docs/runtime/s3.mdx +863 -0
- package/docs/runtime/secrets.mdx +336 -0
- package/docs/runtime/semver.mdx +57 -0
- package/docs/runtime/shell.mdx +637 -0
- package/docs/runtime/sql.mdx +1404 -0
- package/docs/runtime/sqlite.mdx +699 -0
- package/docs/runtime/streams.mdx +232 -0
- package/docs/runtime/templating/create.mdx +269 -0
- package/docs/runtime/templating/init.mdx +58 -0
- package/docs/runtime/transpiler.mdx +288 -0
- package/docs/runtime/typescript.mdx +58 -0
- package/docs/runtime/utils.mdx +922 -0
- package/docs/runtime/watch-mode.mdx +161 -0
- package/docs/runtime/web-apis.mdx +29 -0
- package/docs/runtime/workers.mdx +328 -0
- package/docs/runtime/yaml.mdx +469 -0
- package/docs/snippets/cli/add.mdx +166 -0
- package/docs/snippets/cli/build.mdx +196 -0
- package/docs/snippets/cli/feedback.mdx +17 -0
- package/docs/snippets/cli/init.mdx +84 -0
- package/docs/snippets/cli/install.mdx +173 -0
- package/docs/snippets/cli/link.mdx +163 -0
- package/docs/snippets/cli/outdated.mdx +140 -0
- package/docs/snippets/cli/patch.mdx +171 -0
- package/docs/snippets/cli/publish.mdx +198 -0
- package/docs/snippets/cli/remove.mdx +146 -0
- package/docs/snippets/cli/run.mdx +293 -0
- package/docs/snippets/cli/test.mdx +100 -0
- package/docs/snippets/cli/update.mdx +144 -0
- package/docs/snippets/product-card.mdx +32 -0
- package/docs/snippets/product-tiles.mdx +94 -0
- package/docs/test/code-coverage.mdx +409 -0
- package/docs/test/configuration.mdx +467 -0
- package/docs/test/dates-times.mdx +129 -0
- package/docs/test/discovery.mdx +90 -0
- package/docs/test/dom.mdx +226 -0
- package/docs/test/index.mdx +380 -0
- package/docs/test/lifecycle.mdx +348 -0
- package/docs/test/mocks.mdx +637 -0
- package/docs/test/reporters.mdx +117 -0
- package/docs/test/runtime-behavior.mdx +342 -0
- package/docs/test/snapshots.mdx +434 -0
- package/docs/test/writing-tests.mdx +635 -0
- package/docs/typescript.mdx +54 -0
- package/package.json +8 -6
- package/test.d.ts +2 -2
|
@@ -0,0 +1,635 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Writing tests"
|
|
3
|
+
description: "Learn how to write tests using Bun's Jest-compatible API with support for async tests, timeouts, and various test modifiers"
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
Define tests with a Jest-like API imported from the built-in `bun:test` module. Long term, Bun aims for complete Jest compatibility; at the moment, a limited set of expect matchers are supported.
|
|
7
|
+
|
|
8
|
+
## Basic Usage
|
|
9
|
+
|
|
10
|
+
To define a simple test:
|
|
11
|
+
|
|
12
|
+
```ts title="math.test.ts" icon="/icons/typescript.svg"
|
|
13
|
+
import { expect, test } from "bun:test";
|
|
14
|
+
|
|
15
|
+
test("2 + 2", () => {
|
|
16
|
+
expect(2 + 2).toBe(4);
|
|
17
|
+
});
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
### Grouping Tests
|
|
21
|
+
|
|
22
|
+
Tests can be grouped into suites with `describe`.
|
|
23
|
+
|
|
24
|
+
```ts title="math.test.ts" icon="/icons/typescript.svg"
|
|
25
|
+
import { expect, test, describe } from "bun:test";
|
|
26
|
+
|
|
27
|
+
describe("arithmetic", () => {
|
|
28
|
+
test("2 + 2", () => {
|
|
29
|
+
expect(2 + 2).toBe(4);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
test("2 * 2", () => {
|
|
33
|
+
expect(2 * 2).toBe(4);
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### Async Tests
|
|
39
|
+
|
|
40
|
+
Tests can be async.
|
|
41
|
+
|
|
42
|
+
```ts title="math.test.ts" icon="/icons/typescript.svg"
|
|
43
|
+
import { expect, test } from "bun:test";
|
|
44
|
+
|
|
45
|
+
test("2 * 2", async () => {
|
|
46
|
+
const result = await Promise.resolve(2 * 2);
|
|
47
|
+
expect(result).toEqual(4);
|
|
48
|
+
});
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Alternatively, use the `done` callback to signal completion. If you include the `done` callback as a parameter in your test definition, you must call it or the test will hang.
|
|
52
|
+
|
|
53
|
+
```ts title="math.test.ts" icon="/icons/typescript.svg"
|
|
54
|
+
import { expect, test } from "bun:test";
|
|
55
|
+
|
|
56
|
+
test("2 * 2", done => {
|
|
57
|
+
Promise.resolve(2 * 2).then(result => {
|
|
58
|
+
expect(result).toEqual(4);
|
|
59
|
+
done();
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## Timeouts
|
|
65
|
+
|
|
66
|
+
Optionally specify a per-test timeout in milliseconds by passing a number as the third argument to `test`.
|
|
67
|
+
|
|
68
|
+
```ts title="math.test.ts" icon="/icons/typescript.svg"
|
|
69
|
+
import { test } from "bun:test";
|
|
70
|
+
|
|
71
|
+
test("wat", async () => {
|
|
72
|
+
const data = await slowOperation();
|
|
73
|
+
expect(data).toBe(42);
|
|
74
|
+
}, 500); // test must run in <500ms
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
In `bun:test`, test timeouts throw an uncatchable exception to force the test to stop running and fail. We also kill any child processes that were spawned in the test to avoid leaving behind zombie processes lurking in the background.
|
|
78
|
+
|
|
79
|
+
The default timeout for each test is 5000ms (5 seconds) if not overridden by this timeout option or `jest.setDefaultTimeout()`.
|
|
80
|
+
|
|
81
|
+
### 🧟 Zombie Process Killer
|
|
82
|
+
|
|
83
|
+
When a test times out and processes spawned in the test via `Bun.spawn`, `Bun.spawnSync`, or `node:child_process` are not killed, they will be automatically killed and a message will be logged to the console. This prevents zombie processes from lingering in the background after timed-out tests.
|
|
84
|
+
|
|
85
|
+
## Test Modifiers
|
|
86
|
+
|
|
87
|
+
### test.skip
|
|
88
|
+
|
|
89
|
+
Skip individual tests with `test.skip`. These tests will not be run.
|
|
90
|
+
|
|
91
|
+
```ts title="math.test.ts" icon="/icons/typescript.svg"
|
|
92
|
+
import { expect, test } from "bun:test";
|
|
93
|
+
|
|
94
|
+
test.skip("wat", () => {
|
|
95
|
+
// TODO: fix this
|
|
96
|
+
expect(0.1 + 0.2).toEqual(0.3);
|
|
97
|
+
});
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
### test.todo
|
|
101
|
+
|
|
102
|
+
Mark a test as a todo with `test.todo`. These tests will not be run.
|
|
103
|
+
|
|
104
|
+
```ts title="math.test.ts" icon="/icons/typescript.svg"
|
|
105
|
+
import { expect, test } from "bun:test";
|
|
106
|
+
|
|
107
|
+
test.todo("fix this", () => {
|
|
108
|
+
myTestFunction();
|
|
109
|
+
});
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
To run todo tests and find any which are passing, use `bun test --todo`.
|
|
113
|
+
|
|
114
|
+
```bash terminal icon="terminal"
|
|
115
|
+
bun test --todo
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
```
|
|
119
|
+
my.test.ts:
|
|
120
|
+
✗ unimplemented feature
|
|
121
|
+
^ this test is marked as todo but passes. Remove `.todo` or check that test is correct.
|
|
122
|
+
|
|
123
|
+
0 pass
|
|
124
|
+
1 fail
|
|
125
|
+
1 expect() calls
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
With this flag, failing todo tests will not cause an error, but todo tests which pass will be marked as failing so you can remove the todo mark or fix the test.
|
|
129
|
+
|
|
130
|
+
### test.only
|
|
131
|
+
|
|
132
|
+
To run a particular test or suite of tests use `test.only()` or `describe.only()`.
|
|
133
|
+
|
|
134
|
+
```ts title="example.test.ts" icon="/icons/typescript.svg"
|
|
135
|
+
import { test, describe } from "bun:test";
|
|
136
|
+
|
|
137
|
+
test("test #1", () => {
|
|
138
|
+
// does not run
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
test.only("test #2", () => {
|
|
142
|
+
// runs
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
describe.only("only", () => {
|
|
146
|
+
test("test #3", () => {
|
|
147
|
+
// runs
|
|
148
|
+
});
|
|
149
|
+
});
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
The following command will only execute tests #2 and #3.
|
|
153
|
+
|
|
154
|
+
```bash terminal icon="terminal"
|
|
155
|
+
bun test --only
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
The following command will only execute tests #1, #2 and #3.
|
|
159
|
+
|
|
160
|
+
```bash terminal icon="terminal"
|
|
161
|
+
bun test
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
### test.if
|
|
165
|
+
|
|
166
|
+
To run a test conditionally, use `test.if()`. The test will run if the condition is truthy. This is particularly useful for tests that should only run on specific architectures or operating systems.
|
|
167
|
+
|
|
168
|
+
```ts title="example.test.ts" icon="/icons/typescript.svg"
|
|
169
|
+
test.if(Math.random() > 0.5)("runs half the time", () => {
|
|
170
|
+
// ...
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
const macOS = process.platform === "darwin";
|
|
174
|
+
test.if(macOS)("runs on macOS", () => {
|
|
175
|
+
// runs if macOS
|
|
176
|
+
});
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
### test.skipIf
|
|
180
|
+
|
|
181
|
+
To instead skip a test based on some condition, use `test.skipIf()` or `describe.skipIf()`.
|
|
182
|
+
|
|
183
|
+
```ts title="example.test.ts" icon="/icons/typescript.svg"
|
|
184
|
+
const macOS = process.platform === "darwin";
|
|
185
|
+
|
|
186
|
+
test.skipIf(macOS)("runs on non-macOS", () => {
|
|
187
|
+
// runs if *not* macOS
|
|
188
|
+
});
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
### test.todoIf
|
|
192
|
+
|
|
193
|
+
If instead you want to mark the test as TODO, use `test.todoIf()` or `describe.todoIf()`. Carefully choosing `skipIf` or `todoIf` can show a difference between, for example, intent of "invalid for this target" and "planned but not implemented yet."
|
|
194
|
+
|
|
195
|
+
```ts title="example.test.ts" icon="/icons/typescript.svg"
|
|
196
|
+
const macOS = process.platform === "darwin";
|
|
197
|
+
|
|
198
|
+
// TODO: we've only implemented this for Linux so far.
|
|
199
|
+
test.todoIf(macOS)("runs on posix", () => {
|
|
200
|
+
// runs if *not* macOS
|
|
201
|
+
});
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
### test.failing
|
|
205
|
+
|
|
206
|
+
Use `test.failing()` when you know a test is currently failing but you want to track it and be notified when it starts passing. This inverts the test result:
|
|
207
|
+
|
|
208
|
+
- A failing test marked with `.failing()` will pass
|
|
209
|
+
- A passing test marked with `.failing()` will fail (with a message indicating it's now passing and should be fixed)
|
|
210
|
+
|
|
211
|
+
```ts math.test.ts icon="/icons/typescript.svg"
|
|
212
|
+
// This will pass because the test is failing as expected
|
|
213
|
+
test.failing("math is broken", () => {
|
|
214
|
+
expect(0.1 + 0.2).toBe(0.3); // fails due to floating point precision
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
// This will fail with a message that the test is now passing
|
|
218
|
+
test.failing("fixed bug", () => {
|
|
219
|
+
expect(1 + 1).toBe(2); // passes, but we expected it to fail
|
|
220
|
+
});
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
This is useful for tracking known bugs that you plan to fix later, or for implementing test-driven development.
|
|
224
|
+
|
|
225
|
+
## Conditional Tests for Describe Blocks
|
|
226
|
+
|
|
227
|
+
The conditional modifiers `.if()`, `.skipIf()`, and `.todoIf()` can also be applied to describe blocks, affecting all tests within the suite:
|
|
228
|
+
|
|
229
|
+
```ts title="example.test.ts" icon="/icons/typescript.svg"
|
|
230
|
+
const isMacOS = process.platform === "darwin";
|
|
231
|
+
|
|
232
|
+
// Only runs the entire suite on macOS
|
|
233
|
+
describe.if(isMacOS)("macOS-specific features", () => {
|
|
234
|
+
test("feature A", () => {
|
|
235
|
+
// only runs on macOS
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
test("feature B", () => {
|
|
239
|
+
// only runs on macOS
|
|
240
|
+
});
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
// Skips the entire suite on Windows
|
|
244
|
+
describe.skipIf(process.platform === "win32")("Unix features", () => {
|
|
245
|
+
test("feature C", () => {
|
|
246
|
+
// skipped on Windows
|
|
247
|
+
});
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
// Marks the entire suite as TODO on Linux
|
|
251
|
+
describe.todoIf(process.platform === "linux")("Upcoming Linux support", () => {
|
|
252
|
+
test("feature D", () => {
|
|
253
|
+
// marked as TODO on Linux
|
|
254
|
+
});
|
|
255
|
+
});
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
## Parametrized Tests
|
|
259
|
+
|
|
260
|
+
### `test.each` and `describe.each`
|
|
261
|
+
|
|
262
|
+
To run the same test with multiple sets of data, use `test.each`. This creates a parametrized test that runs once for each test case provided.
|
|
263
|
+
|
|
264
|
+
```ts title="math.test.ts" icon="/icons/typescript.svg"
|
|
265
|
+
const cases = [
|
|
266
|
+
[1, 2, 3],
|
|
267
|
+
[3, 4, 7],
|
|
268
|
+
];
|
|
269
|
+
|
|
270
|
+
test.each(cases)("%p + %p should be %p", (a, b, expected) => {
|
|
271
|
+
expect(a + b).toBe(expected);
|
|
272
|
+
});
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
You can also use `describe.each` to create a parametrized suite that runs once for each test case:
|
|
276
|
+
|
|
277
|
+
```ts title="sum.test.ts" icon="/icons/typescript.svg"
|
|
278
|
+
describe.each([
|
|
279
|
+
[1, 2, 3],
|
|
280
|
+
[3, 4, 7],
|
|
281
|
+
])("add(%i, %i)", (a, b, expected) => {
|
|
282
|
+
test(`returns ${expected}`, () => {
|
|
283
|
+
expect(a + b).toBe(expected);
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
test(`sum is greater than each value`, () => {
|
|
287
|
+
expect(a + b).toBeGreaterThan(a);
|
|
288
|
+
expect(a + b).toBeGreaterThan(b);
|
|
289
|
+
});
|
|
290
|
+
});
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
### Argument Passing
|
|
294
|
+
|
|
295
|
+
How arguments are passed to your test function depends on the structure of your test cases:
|
|
296
|
+
|
|
297
|
+
- If a table row is an array (like `[1, 2, 3]`), each element is passed as an individual argument
|
|
298
|
+
- If a row is not an array (like an object), it's passed as a single argument
|
|
299
|
+
|
|
300
|
+
```ts title="example.test.ts" icon="/icons/typescript.svg"
|
|
301
|
+
// Array items passed as individual arguments
|
|
302
|
+
test.each([
|
|
303
|
+
[1, 2, 3],
|
|
304
|
+
[4, 5, 9],
|
|
305
|
+
])("add(%i, %i) = %i", (a, b, expected) => {
|
|
306
|
+
expect(a + b).toBe(expected);
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
// Object items passed as a single argument
|
|
310
|
+
test.each([
|
|
311
|
+
{ a: 1, b: 2, expected: 3 },
|
|
312
|
+
{ a: 4, b: 5, expected: 9 },
|
|
313
|
+
])("add($a, $b) = $expected", data => {
|
|
314
|
+
expect(data.a + data.b).toBe(data.expected);
|
|
315
|
+
});
|
|
316
|
+
```
|
|
317
|
+
|
|
318
|
+
### Format Specifiers
|
|
319
|
+
|
|
320
|
+
There are a number of options available for formatting the test title:
|
|
321
|
+
|
|
322
|
+
| Specifier | Description |
|
|
323
|
+
| --------- | ----------------------- |
|
|
324
|
+
| `%p` | pretty-format |
|
|
325
|
+
| `%s` | String |
|
|
326
|
+
| `%d` | Number |
|
|
327
|
+
| `%i` | Integer |
|
|
328
|
+
| `%f` | Floating point |
|
|
329
|
+
| `%j` | JSON |
|
|
330
|
+
| `%o` | Object |
|
|
331
|
+
| `%#` | Index of the test case |
|
|
332
|
+
| `%%` | Single percent sign (%) |
|
|
333
|
+
|
|
334
|
+
#### Examples
|
|
335
|
+
|
|
336
|
+
```ts title="example.test.ts" icon="/icons/typescript.svg"
|
|
337
|
+
// Basic specifiers
|
|
338
|
+
test.each([
|
|
339
|
+
["hello", 123],
|
|
340
|
+
["world", 456],
|
|
341
|
+
])("string: %s, number: %i", (str, num) => {
|
|
342
|
+
// "string: hello, number: 123"
|
|
343
|
+
// "string: world, number: 456"
|
|
344
|
+
});
|
|
345
|
+
|
|
346
|
+
// %p for pretty-format output
|
|
347
|
+
test.each([
|
|
348
|
+
[{ name: "Alice" }, { a: 1, b: 2 }],
|
|
349
|
+
[{ name: "Bob" }, { x: 5, y: 10 }],
|
|
350
|
+
])("user %p with data %p", (user, data) => {
|
|
351
|
+
// "user { name: 'Alice' } with data { a: 1, b: 2 }"
|
|
352
|
+
// "user { name: 'Bob' } with data { x: 5, y: 10 }"
|
|
353
|
+
});
|
|
354
|
+
|
|
355
|
+
// %# for index
|
|
356
|
+
test.each(["apple", "banana"])("fruit #%# is %s", fruit => {
|
|
357
|
+
// "fruit #0 is apple"
|
|
358
|
+
// "fruit #1 is banana"
|
|
359
|
+
});
|
|
360
|
+
```
|
|
361
|
+
|
|
362
|
+
## Assertion Counting
|
|
363
|
+
|
|
364
|
+
Bun supports verifying that a specific number of assertions were called during a test:
|
|
365
|
+
|
|
366
|
+
### expect.hasAssertions()
|
|
367
|
+
|
|
368
|
+
Use `expect.hasAssertions()` to verify that at least one assertion is called during a test:
|
|
369
|
+
|
|
370
|
+
```ts title="example.test.ts" icon="/icons/typescript.svg"
|
|
371
|
+
test("async work calls assertions", async () => {
|
|
372
|
+
expect.hasAssertions(); // Will fail if no assertions are called
|
|
373
|
+
|
|
374
|
+
const data = await fetchData();
|
|
375
|
+
expect(data).toBeDefined();
|
|
376
|
+
});
|
|
377
|
+
```
|
|
378
|
+
|
|
379
|
+
This is especially useful for async tests to ensure your assertions actually run.
|
|
380
|
+
|
|
381
|
+
### expect.assertions(count)
|
|
382
|
+
|
|
383
|
+
Use `expect.assertions(count)` to verify that a specific number of assertions are called during a test:
|
|
384
|
+
|
|
385
|
+
```ts title="example.test.ts" icon="/icons/typescript.svg"
|
|
386
|
+
test("exactly two assertions", () => {
|
|
387
|
+
expect.assertions(2); // Will fail if not exactly 2 assertions are called
|
|
388
|
+
|
|
389
|
+
expect(1 + 1).toBe(2);
|
|
390
|
+
expect("hello").toContain("ell");
|
|
391
|
+
});
|
|
392
|
+
```
|
|
393
|
+
|
|
394
|
+
This helps ensure all your assertions run, especially in complex async code with multiple code paths.
|
|
395
|
+
|
|
396
|
+
## Type Testing
|
|
397
|
+
|
|
398
|
+
Bun includes `expectTypeOf` for testing TypeScript types, compatible with Vitest.
|
|
399
|
+
|
|
400
|
+
### expectTypeOf
|
|
401
|
+
|
|
402
|
+
<Warning>
|
|
403
|
+
These functions are no-ops at runtime - you need to run TypeScript separately to verify the type checks.
|
|
404
|
+
</Warning>
|
|
405
|
+
|
|
406
|
+
The `expectTypeOf` function provides type-level assertions that are checked by TypeScript's type checker. To test your types:
|
|
407
|
+
|
|
408
|
+
1. Write your type assertions using `expectTypeOf`
|
|
409
|
+
2. Run `bunx tsc --noEmit` to check that your types are correct
|
|
410
|
+
|
|
411
|
+
```ts title="example.test.ts" icon="/icons/typescript.svg"
|
|
412
|
+
import { expectTypeOf } from "bun:test";
|
|
413
|
+
|
|
414
|
+
// Basic type assertions
|
|
415
|
+
expectTypeOf<string>().toEqualTypeOf<string>();
|
|
416
|
+
expectTypeOf(123).toBeNumber();
|
|
417
|
+
expectTypeOf("hello").toBeString();
|
|
418
|
+
|
|
419
|
+
// Object type matching
|
|
420
|
+
expectTypeOf({ a: 1, b: "hello" }).toMatchObjectType<{ a: number }>();
|
|
421
|
+
|
|
422
|
+
// Function types
|
|
423
|
+
function greet(name: string): string {
|
|
424
|
+
return `Hello ${name}`;
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
expectTypeOf(greet).toBeFunction();
|
|
428
|
+
expectTypeOf(greet).parameters.toEqualTypeOf<[string]>();
|
|
429
|
+
expectTypeOf(greet).returns.toEqualTypeOf<string>();
|
|
430
|
+
|
|
431
|
+
// Array types
|
|
432
|
+
expectTypeOf([1, 2, 3]).items.toBeNumber();
|
|
433
|
+
|
|
434
|
+
// Promise types
|
|
435
|
+
expectTypeOf(Promise.resolve(42)).resolves.toBeNumber();
|
|
436
|
+
```
|
|
437
|
+
|
|
438
|
+
For full documentation on expectTypeOf matchers, see the [API Reference](https://bun.com/reference/bun/test/expectTypeOf).
|
|
439
|
+
|
|
440
|
+
## Matchers
|
|
441
|
+
|
|
442
|
+
Bun implements the following matchers. Full Jest compatibility is on the roadmap; [track progress here](https://github.com/oven-sh/bun/issues/1825).
|
|
443
|
+
|
|
444
|
+
### Basic Matchers
|
|
445
|
+
|
|
446
|
+
| Status | Matcher |
|
|
447
|
+
| ------ | ------------------ |
|
|
448
|
+
| ✅ | `.not` |
|
|
449
|
+
| ✅ | `.toBe()` |
|
|
450
|
+
| ✅ | `.toEqual()` |
|
|
451
|
+
| ✅ | `.toBeNull()` |
|
|
452
|
+
| ✅ | `.toBeUndefined()` |
|
|
453
|
+
| ✅ | `.toBeNaN()` |
|
|
454
|
+
| ✅ | `.toBeDefined()` |
|
|
455
|
+
| ✅ | `.toBeFalsy()` |
|
|
456
|
+
| ✅ | `.toBeTruthy()` |
|
|
457
|
+
| ✅ | `.toStrictEqual()` |
|
|
458
|
+
|
|
459
|
+
### String and Array Matchers
|
|
460
|
+
|
|
461
|
+
| Status | Matcher |
|
|
462
|
+
| ------ | --------------------- |
|
|
463
|
+
| ✅ | `.toContain()` |
|
|
464
|
+
| ✅ | `.toHaveLength()` |
|
|
465
|
+
| ✅ | `.toMatch()` |
|
|
466
|
+
| ✅ | `.toContainEqual()` |
|
|
467
|
+
| ✅ | `.stringContaining()` |
|
|
468
|
+
| ✅ | `.stringMatching()` |
|
|
469
|
+
| ✅ | `.arrayContaining()` |
|
|
470
|
+
|
|
471
|
+
### Object Matchers
|
|
472
|
+
|
|
473
|
+
| Status | Matcher |
|
|
474
|
+
| ------ | ----------------------- |
|
|
475
|
+
| ✅ | `.toHaveProperty()` |
|
|
476
|
+
| ✅ | `.toMatchObject()` |
|
|
477
|
+
| ✅ | `.toContainAllKeys()` |
|
|
478
|
+
| ✅ | `.toContainValue()` |
|
|
479
|
+
| ✅ | `.toContainValues()` |
|
|
480
|
+
| ✅ | `.toContainAllValues()` |
|
|
481
|
+
| ✅ | `.toContainAnyValues()` |
|
|
482
|
+
| ✅ | `.objectContaining()` |
|
|
483
|
+
|
|
484
|
+
### Number Matchers
|
|
485
|
+
|
|
486
|
+
| Status | Matcher |
|
|
487
|
+
| ------ | --------------------------- |
|
|
488
|
+
| ✅ | `.toBeCloseTo()` |
|
|
489
|
+
| ✅ | `.closeTo()` |
|
|
490
|
+
| ✅ | `.toBeGreaterThan()` |
|
|
491
|
+
| ✅ | `.toBeGreaterThanOrEqual()` |
|
|
492
|
+
| ✅ | `.toBeLessThan()` |
|
|
493
|
+
| ✅ | `.toBeLessThanOrEqual()` |
|
|
494
|
+
|
|
495
|
+
### Function and Class Matchers
|
|
496
|
+
|
|
497
|
+
| Status | Matcher |
|
|
498
|
+
| ------ | ------------------- |
|
|
499
|
+
| ✅ | `.toThrow()` |
|
|
500
|
+
| ✅ | `.toBeInstanceOf()` |
|
|
501
|
+
|
|
502
|
+
### Promise Matchers
|
|
503
|
+
|
|
504
|
+
| Status | Matcher |
|
|
505
|
+
| ------ | ------------- |
|
|
506
|
+
| ✅ | `.resolves()` |
|
|
507
|
+
| ✅ | `.rejects()` |
|
|
508
|
+
|
|
509
|
+
### Mock Function Matchers
|
|
510
|
+
|
|
511
|
+
| Status | Matcher |
|
|
512
|
+
| ------ | ----------------------------- |
|
|
513
|
+
| ✅ | `.toHaveBeenCalled()` |
|
|
514
|
+
| ✅ | `.toHaveBeenCalledTimes()` |
|
|
515
|
+
| ✅ | `.toHaveBeenCalledWith()` |
|
|
516
|
+
| ✅ | `.toHaveBeenLastCalledWith()` |
|
|
517
|
+
| ✅ | `.toHaveBeenNthCalledWith()` |
|
|
518
|
+
| ✅ | `.toHaveReturned()` |
|
|
519
|
+
| ✅ | `.toHaveReturnedTimes()` |
|
|
520
|
+
| ✅ | `.toHaveReturnedWith()` |
|
|
521
|
+
| ✅ | `.toHaveLastReturnedWith()` |
|
|
522
|
+
| ✅ | `.toHaveNthReturnedWith()` |
|
|
523
|
+
|
|
524
|
+
### Snapshot Matchers
|
|
525
|
+
|
|
526
|
+
| Status | Matcher |
|
|
527
|
+
| ------ | --------------------------------------- |
|
|
528
|
+
| ✅ | `.toMatchSnapshot()` |
|
|
529
|
+
| ✅ | `.toMatchInlineSnapshot()` |
|
|
530
|
+
| ✅ | `.toThrowErrorMatchingSnapshot()` |
|
|
531
|
+
| ✅ | `.toThrowErrorMatchingInlineSnapshot()` |
|
|
532
|
+
|
|
533
|
+
### Utility Matchers
|
|
534
|
+
|
|
535
|
+
| Status | Matcher |
|
|
536
|
+
| ------ | ------------------ |
|
|
537
|
+
| ✅ | `.extend` |
|
|
538
|
+
| ✅ | `.anything()` |
|
|
539
|
+
| ✅ | `.any()` |
|
|
540
|
+
| ✅ | `.assertions()` |
|
|
541
|
+
| ✅ | `.hasAssertions()` |
|
|
542
|
+
|
|
543
|
+
### Not Yet Implemented
|
|
544
|
+
|
|
545
|
+
| Status | Matcher |
|
|
546
|
+
| ------ | -------------------------- |
|
|
547
|
+
| ❌ | `.addSnapshotSerializer()` |
|
|
548
|
+
|
|
549
|
+
## Best Practices
|
|
550
|
+
|
|
551
|
+
### Use Descriptive Test Names
|
|
552
|
+
|
|
553
|
+
```ts title="example.test.ts" icon="/icons/typescript.svg"
|
|
554
|
+
// Good
|
|
555
|
+
test("should calculate total price including tax for multiple items", () => {
|
|
556
|
+
// test implementation
|
|
557
|
+
});
|
|
558
|
+
|
|
559
|
+
// Avoid
|
|
560
|
+
test("price calculation", () => {
|
|
561
|
+
// test implementation
|
|
562
|
+
});
|
|
563
|
+
```
|
|
564
|
+
|
|
565
|
+
### Group Related Tests
|
|
566
|
+
|
|
567
|
+
```ts title="auth.test.ts" icon="/icons/typescript.svg"
|
|
568
|
+
describe("User authentication", () => {
|
|
569
|
+
describe("with valid credentials", () => {
|
|
570
|
+
test("should return user data", () => {
|
|
571
|
+
// test implementation
|
|
572
|
+
});
|
|
573
|
+
|
|
574
|
+
test("should set authentication token", () => {
|
|
575
|
+
// test implementation
|
|
576
|
+
});
|
|
577
|
+
});
|
|
578
|
+
|
|
579
|
+
describe("with invalid credentials", () => {
|
|
580
|
+
test("should throw authentication error", () => {
|
|
581
|
+
// test implementation
|
|
582
|
+
});
|
|
583
|
+
});
|
|
584
|
+
});
|
|
585
|
+
```
|
|
586
|
+
|
|
587
|
+
### Use Appropriate Matchers
|
|
588
|
+
|
|
589
|
+
```ts title="auth.test.ts" icon="/icons/typescript.svg"
|
|
590
|
+
// Good: Use specific matchers
|
|
591
|
+
expect(users).toHaveLength(3);
|
|
592
|
+
expect(user.email).toContain("@");
|
|
593
|
+
expect(response.status).toBeGreaterThanOrEqual(200);
|
|
594
|
+
|
|
595
|
+
// Avoid: Using toBe for everything
|
|
596
|
+
expect(users.length === 3).toBe(true);
|
|
597
|
+
expect(user.email.includes("@")).toBe(true);
|
|
598
|
+
expect(response.status >= 200).toBe(true);
|
|
599
|
+
```
|
|
600
|
+
|
|
601
|
+
### Test Error Conditions
|
|
602
|
+
|
|
603
|
+
```ts title="example.test.ts" icon="/icons/typescript.svg"
|
|
604
|
+
test("should throw error for invalid input", () => {
|
|
605
|
+
expect(() => {
|
|
606
|
+
validateEmail("not-an-email");
|
|
607
|
+
}).toThrow("Invalid email format");
|
|
608
|
+
});
|
|
609
|
+
|
|
610
|
+
test("should handle async errors", async () => {
|
|
611
|
+
await expect(async () => {
|
|
612
|
+
await fetchUser("invalid-id");
|
|
613
|
+
}).rejects.toThrow("User not found");
|
|
614
|
+
});
|
|
615
|
+
```
|
|
616
|
+
|
|
617
|
+
### Use Setup and Teardown
|
|
618
|
+
|
|
619
|
+
```ts title="example.test.ts" icon="/icons/typescript.svg"
|
|
620
|
+
import { beforeEach, afterEach, test } from "bun:test";
|
|
621
|
+
|
|
622
|
+
let testUser;
|
|
623
|
+
|
|
624
|
+
beforeEach(() => {
|
|
625
|
+
testUser = createTestUser();
|
|
626
|
+
});
|
|
627
|
+
|
|
628
|
+
afterEach(() => {
|
|
629
|
+
cleanupTestUser(testUser);
|
|
630
|
+
});
|
|
631
|
+
|
|
632
|
+
test("should update user profile", () => {
|
|
633
|
+
// Use testUser in test
|
|
634
|
+
});
|
|
635
|
+
```
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: TypeScript
|
|
3
|
+
description: Using TypeScript with Bun, including type definitions and compiler options
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
To install the TypeScript definitions for Bun's built-in APIs, install `@types/bun`.
|
|
7
|
+
|
|
8
|
+
```zsh terminal icon="terminal"
|
|
9
|
+
bun add -d @types/bun # dev dependency
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
At this point, you should be able to reference the `Bun` global in your TypeScript files without seeing errors in your editor.
|
|
13
|
+
|
|
14
|
+
## Suggested `compilerOptions`
|
|
15
|
+
|
|
16
|
+
Bun supports things like top-level await, JSX, and extensioned `.ts` imports, which TypeScript doesn't allow by default. Below is a set of recommended `compilerOptions` for a Bun project, so you can use these features without seeing compiler warnings from TypeScript.
|
|
17
|
+
|
|
18
|
+
```json tsconfig.json icon="file-code"
|
|
19
|
+
{
|
|
20
|
+
"compilerOptions": {
|
|
21
|
+
// Environment setup & latest features
|
|
22
|
+
"lib": ["ESNext"],
|
|
23
|
+
"target": "ESNext",
|
|
24
|
+
"module": "Preserve",
|
|
25
|
+
"moduleDetection": "force",
|
|
26
|
+
"jsx": "react-jsx",
|
|
27
|
+
"allowJs": true,
|
|
28
|
+
|
|
29
|
+
// Bundler mode
|
|
30
|
+
"moduleResolution": "bundler",
|
|
31
|
+
"allowImportingTsExtensions": true,
|
|
32
|
+
"verbatimModuleSyntax": true,
|
|
33
|
+
"noEmit": true,
|
|
34
|
+
|
|
35
|
+
// Best practices
|
|
36
|
+
"strict": true,
|
|
37
|
+
"skipLibCheck": true,
|
|
38
|
+
"noFallthroughCasesInSwitch": true,
|
|
39
|
+
"noUncheckedIndexedAccess": true,
|
|
40
|
+
"noImplicitOverride": true,
|
|
41
|
+
|
|
42
|
+
// Some stricter flags (disabled by default)
|
|
43
|
+
"noUnusedLocals": false,
|
|
44
|
+
"noUnusedParameters": false,
|
|
45
|
+
"noPropertyAccessFromIndexSignature": false
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
If you run `bun init` in a new directory, this `tsconfig.json` will be generated for you. (The stricter flags are disabled by default.)
|
|
51
|
+
|
|
52
|
+
```sh terminal icon="terminal"
|
|
53
|
+
bun init
|
|
54
|
+
```
|