bun-types 1.3.2-canary.20251106T140813 → 1.3.2-canary.20251108T140624
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,116 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Run your tests with the Bun test runner
|
|
3
|
+
sidebarTitle: Run tests
|
|
4
|
+
mode: center
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
Bun has a built-in [test runner](https://bun.sh/docs/cli/test) with a Jest-like `expect` API.
|
|
8
|
+
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
To use it, run the `bun test` command from your project directory. The test runner will recursively search for all files in the directory that match the following patterns and execute the tests they contain.
|
|
12
|
+
|
|
13
|
+
```txt File Tree icon="folder-tree"
|
|
14
|
+
*.test.{js|jsx|ts|tsx}
|
|
15
|
+
*_test.{js|jsx|ts|tsx}
|
|
16
|
+
*.spec.{js|jsx|ts|tsx}
|
|
17
|
+
*_spec.{js|jsx|ts|tsx}
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
---
|
|
21
|
+
|
|
22
|
+
Here's what the output of a typical test run looks like. In this case, there are three tests files (`test.test.js`, `test2.test.js`, and `test3.test.js`) containing two tests each (`add` and `multiply`).
|
|
23
|
+
|
|
24
|
+
```sh terminal icon="terminal"
|
|
25
|
+
bun test
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
```txt
|
|
29
|
+
test.test.js:
|
|
30
|
+
✓ add [0.87ms]
|
|
31
|
+
✓ multiply [0.02ms]
|
|
32
|
+
|
|
33
|
+
test2.test.js:
|
|
34
|
+
✓ add [0.72ms]
|
|
35
|
+
✓ multiply [0.01ms]
|
|
36
|
+
|
|
37
|
+
test3.test.js:
|
|
38
|
+
✓ add [0.54ms]
|
|
39
|
+
✓ multiply [0.01ms]
|
|
40
|
+
|
|
41
|
+
6 pass
|
|
42
|
+
0 fail
|
|
43
|
+
6 expect() calls
|
|
44
|
+
Ran 6 tests across 3 files. [9.00ms]
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
---
|
|
48
|
+
|
|
49
|
+
To only run certain test files, pass a positional argument to `bun test`. The runner will only execute files that contain that argument in their path.
|
|
50
|
+
|
|
51
|
+
```sh terminal icon="terminal"
|
|
52
|
+
bun test test3
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
```txt
|
|
56
|
+
test3.test.js:
|
|
57
|
+
✓ add [1.40ms]
|
|
58
|
+
✓ multiply [0.03ms]
|
|
59
|
+
|
|
60
|
+
2 pass
|
|
61
|
+
0 fail
|
|
62
|
+
2 expect() calls
|
|
63
|
+
Ran 2 tests across 1 files. [15.00ms]
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
---
|
|
67
|
+
|
|
68
|
+
All tests have a name, defined using the first parameter to the `test` function. Tests can also be grouped into suites with `describe`.
|
|
69
|
+
|
|
70
|
+
```ts
|
|
71
|
+
import { test, expect, describe } from "bun:test";
|
|
72
|
+
|
|
73
|
+
describe("math", () => {
|
|
74
|
+
test("add", () => {
|
|
75
|
+
expect(2 + 2).toEqual(4);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
test("multiply", () => {
|
|
79
|
+
expect(2 * 2).toEqual(4);
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
---
|
|
85
|
+
|
|
86
|
+
To filter which tests are executed by name, use the `-t`/`--test-name-pattern` flag.
|
|
87
|
+
|
|
88
|
+
Adding `-t add` will only run tests with "add" in the name. This works with test names defined with `test` or test suite names defined with `describe`.
|
|
89
|
+
|
|
90
|
+
```sh terminal icon="terminal"
|
|
91
|
+
bun test -t add
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
```txt
|
|
95
|
+
test.test.js:
|
|
96
|
+
✓ add [1.79ms]
|
|
97
|
+
» multiply
|
|
98
|
+
|
|
99
|
+
test2.test.js:
|
|
100
|
+
✓ add [2.30ms]
|
|
101
|
+
» multiply
|
|
102
|
+
|
|
103
|
+
test3.test.js:
|
|
104
|
+
✓ add [0.32ms]
|
|
105
|
+
» multiply
|
|
106
|
+
|
|
107
|
+
3 pass
|
|
108
|
+
3 skip
|
|
109
|
+
0 fail
|
|
110
|
+
3 expect() calls
|
|
111
|
+
Ran 6 tests across 3 files. [59.00ms]
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
---
|
|
115
|
+
|
|
116
|
+
See [Docs > Test Runner](https://bun.sh/docs/cli/test) for complete documentation on the test runner.
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Skip tests with the Bun test runner
|
|
3
|
+
sidebarTitle: Skip tests
|
|
4
|
+
mode: center
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
To skip a test with the Bun test runner, use the `test.skip` function.
|
|
8
|
+
|
|
9
|
+
```ts test.ts icon="/icons/typescript.svg"
|
|
10
|
+
import { test } from "bun:test";
|
|
11
|
+
|
|
12
|
+
test.skip("unimplemented feature", () => {
|
|
13
|
+
expect(Bun.isAwesome()).toBe(true);
|
|
14
|
+
});
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
---
|
|
18
|
+
|
|
19
|
+
Running `bun test` will not execute this test. It will be marked as skipped in the terminal output.
|
|
20
|
+
|
|
21
|
+
```sh terminal icon="terminal"
|
|
22
|
+
bun test
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
```txt
|
|
26
|
+
test.test.ts:
|
|
27
|
+
✓ add [0.03ms]
|
|
28
|
+
✓ multiply [0.02ms]
|
|
29
|
+
» unimplemented feature
|
|
30
|
+
|
|
31
|
+
2 pass
|
|
32
|
+
1 skip
|
|
33
|
+
0 fail
|
|
34
|
+
2 expect() calls
|
|
35
|
+
Ran 3 tests across 1 files. [74.00ms]
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
---
|
|
39
|
+
|
|
40
|
+
See also:
|
|
41
|
+
|
|
42
|
+
- [Mark a test as a todo](/guides/test/todo-tests)
|
|
43
|
+
- [Docs > Test runner > Writing tests](https://bun.sh/docs/test/writing)
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Use snapshot testing in `bun test`
|
|
3
|
+
sidebarTitle: Snapshot testing
|
|
4
|
+
mode: center
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
Bun's test runner supports Jest-style snapshot testing via `.toMatchSnapshot()`.
|
|
8
|
+
|
|
9
|
+
```ts snap.test.ts icon="/icons/typescript.svg"
|
|
10
|
+
import { test, expect } from "bun:test";
|
|
11
|
+
|
|
12
|
+
test("snapshot", () => {
|
|
13
|
+
expect({ foo: "bar" }).toMatchSnapshot();
|
|
14
|
+
});
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
---
|
|
18
|
+
|
|
19
|
+
The first time this test is executed, Bun will evaluate the value passed into `expect()` and write it to disk in a directory called `__snapshots__` that lives alongside the test file. (Note the `snapshots: +1 added` line in the output.)
|
|
20
|
+
|
|
21
|
+
```sh terminal icon="terminal"
|
|
22
|
+
bun test test/snap
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
```txt
|
|
26
|
+
test/snap.test.ts:
|
|
27
|
+
✓ snapshot [1.48ms]
|
|
28
|
+
|
|
29
|
+
1 pass
|
|
30
|
+
0 fail
|
|
31
|
+
snapshots: +1 added
|
|
32
|
+
1 expect() calls
|
|
33
|
+
Ran 1 tests across 1 files. [82.00ms]
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
---
|
|
37
|
+
|
|
38
|
+
The `__snapshots__` directory contains a `.snap` file for each test file in the directory.
|
|
39
|
+
|
|
40
|
+
```txt File Tree icon="folder-tree"
|
|
41
|
+
test
|
|
42
|
+
├── __snapshots__
|
|
43
|
+
│ └── snap.test.ts.snap
|
|
44
|
+
└── snap.test.ts
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
---
|
|
48
|
+
|
|
49
|
+
The `snap.test.ts.snap` file is a JavaScript file that exports a serialized version of the value passed into `expect()`. The `{foo: "bar"}` object has been serialized to JSON.
|
|
50
|
+
|
|
51
|
+
```js snap.test.ts.snap icon="file-code"
|
|
52
|
+
// Bun Snapshot v1, https://bun.sh/docs/test/snapshots
|
|
53
|
+
|
|
54
|
+
exports[`snapshot 1`] = `
|
|
55
|
+
{
|
|
56
|
+
"foo": "bar",
|
|
57
|
+
}
|
|
58
|
+
`;
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
---
|
|
62
|
+
|
|
63
|
+
Later, when this test file is executed again, Bun will read the snapshot file and compare it to the value passed into `expect()`. If the values are different, the test will fail.
|
|
64
|
+
|
|
65
|
+
```sh terminal icon="terminal"
|
|
66
|
+
bun test
|
|
67
|
+
bun test v1.3.1 (9c68abdb)
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
```txt
|
|
71
|
+
test/snap.test.ts:
|
|
72
|
+
✓ snapshot [1.05ms]
|
|
73
|
+
|
|
74
|
+
1 pass
|
|
75
|
+
0 fail
|
|
76
|
+
1 snapshots, 1 expect() calls
|
|
77
|
+
Ran 1 tests across 1 files. [101.00ms]
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
---
|
|
81
|
+
|
|
82
|
+
To update snapshots, use the `--update-snapshots` flag.
|
|
83
|
+
|
|
84
|
+
```sh terminal icon="terminal"
|
|
85
|
+
bun test --update-snapshots
|
|
86
|
+
bun test v1.3.1 (9c68abdb)
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
```txt
|
|
90
|
+
test/snap.test.ts:
|
|
91
|
+
✓ snapshot [0.86ms]
|
|
92
|
+
|
|
93
|
+
1 pass
|
|
94
|
+
0 fail
|
|
95
|
+
snapshots: +1 added # the snapshot was regenerated
|
|
96
|
+
1 expect() calls
|
|
97
|
+
Ran 1 tests across 1 files. [102.00ms]
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
---
|
|
101
|
+
|
|
102
|
+
See [Docs > Test Runner > Snapshots](https://bun.sh/docs/test/snapshots) for complete documentation on snapshots with the Bun test runner.
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Spy on methods in `bun test`
|
|
3
|
+
sidebarTitle: Spy on methods
|
|
4
|
+
mode: center
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
Use the `spyOn` utility to track method calls with Bun's test runner.
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import { test, expect, spyOn } from "bun:test";
|
|
11
|
+
|
|
12
|
+
const leo = {
|
|
13
|
+
name: "Leonardo",
|
|
14
|
+
sayHi(thing: string) {
|
|
15
|
+
console.log(`Sup I'm ${this.name} and I like ${thing}`);
|
|
16
|
+
},
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const spy = spyOn(leo, "sayHi");
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
---
|
|
23
|
+
|
|
24
|
+
Once the spy is created, it can be used to write `expect` assertions relating to method calls.
|
|
25
|
+
|
|
26
|
+
```ts
|
|
27
|
+
import { test, expect, spyOn } from "bun:test";
|
|
28
|
+
|
|
29
|
+
const leo = {
|
|
30
|
+
name: "Leonardo",
|
|
31
|
+
sayHi(thing: string) {
|
|
32
|
+
console.log(`Sup I'm ${this.name} and I like ${thing}`);
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const spy = spyOn(leo, "sayHi");
|
|
37
|
+
|
|
38
|
+
test("turtles", () => {
|
|
39
|
+
// [!code ++]
|
|
40
|
+
expect(spy).toHaveBeenCalledTimes(0); // [!code ++]
|
|
41
|
+
leo.sayHi("pizza"); // [!code ++]
|
|
42
|
+
expect(spy).toHaveBeenCalledTimes(1); // [!code ++]
|
|
43
|
+
expect(spy.mock.calls).toEqual([["pizza"]]); // [!code ++]
|
|
44
|
+
}); // [!code ++]
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
---
|
|
48
|
+
|
|
49
|
+
See [Docs > Test Runner > Mocks](https://bun.sh/docs/test/mocks) for complete documentation on mocking with the Bun test runner.
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "import, require, and test Svelte components with bun test"
|
|
3
|
+
sidebarTitle: Test Svelte
|
|
4
|
+
mode: center
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
Bun's [Plugin API](/runtime/plugins) lets you add custom loaders to your project. The `test.preload` option in `bunfig.toml` lets you configure your loader to start before your tests run.
|
|
8
|
+
|
|
9
|
+
Firstly, install `@testing-library/svelte`, `svelte`, and `@happy-dom/global-registrator`.
|
|
10
|
+
|
|
11
|
+
```bash terminal icon="terminal"
|
|
12
|
+
bun add @testing-library/svelte svelte@4 @happy-dom/global-registrator
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Then, save this plugin in your project.
|
|
16
|
+
|
|
17
|
+
```ts svelte-loader.js icon="/icons/typescript.svg"
|
|
18
|
+
import { plugin } from "bun";
|
|
19
|
+
import { compile } from "svelte/compiler";
|
|
20
|
+
import { readFileSync } from "fs";
|
|
21
|
+
import { beforeEach, afterEach } from "bun:test";
|
|
22
|
+
import { GlobalRegistrator } from "@happy-dom/global-registrator";
|
|
23
|
+
|
|
24
|
+
beforeEach(async () => {
|
|
25
|
+
await GlobalRegistrator.register();
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
afterEach(async () => {
|
|
29
|
+
await GlobalRegistrator.unregister();
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
plugin({
|
|
33
|
+
title: "svelte loader",
|
|
34
|
+
setup(builder) {
|
|
35
|
+
builder.onLoad({ filter: /\.svelte(\?[^.]+)?$/ }, ({ path }) => {
|
|
36
|
+
try {
|
|
37
|
+
const source = readFileSync(path.substring(0, path.includes("?") ? path.indexOf("?") : path.length), "utf-8");
|
|
38
|
+
|
|
39
|
+
const result = compile(source, {
|
|
40
|
+
filetitle: path,
|
|
41
|
+
generate: "client",
|
|
42
|
+
dev: false,
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
return {
|
|
46
|
+
contents: result.js.code,
|
|
47
|
+
loader: "js",
|
|
48
|
+
};
|
|
49
|
+
} catch (err) {
|
|
50
|
+
throw new Error(`Failed to compile Svelte component: ${err.message}`);
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
},
|
|
54
|
+
});
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
---
|
|
58
|
+
|
|
59
|
+
Add this to `bunfig.toml` to tell Bun to preload the plugin, so it loads before your tests run.
|
|
60
|
+
|
|
61
|
+
```toml bunfig.toml icon="settings"
|
|
62
|
+
[test]
|
|
63
|
+
# Tell Bun to load this plugin before your tests run
|
|
64
|
+
preload = ["./svelte-loader.js"]
|
|
65
|
+
|
|
66
|
+
# This also works:
|
|
67
|
+
# test.preload = ["./svelte-loader.js"]
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
---
|
|
71
|
+
|
|
72
|
+
Add an example `.svelte` file in your project.
|
|
73
|
+
|
|
74
|
+
```html Counter.svelte icon="file-code"
|
|
75
|
+
<script>
|
|
76
|
+
export let initialCount = 0;
|
|
77
|
+
let count = initialCount;
|
|
78
|
+
</script>
|
|
79
|
+
|
|
80
|
+
<button on:click="{()" ="">(count += 1)}>+1</button>
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
---
|
|
84
|
+
|
|
85
|
+
Now you can `import` or `require` `*.svelte` files in your tests, and it will load the Svelte component as a JavaScript module.
|
|
86
|
+
|
|
87
|
+
```ts hello-svelte.test.ts icon="/icons/typescript.svg"
|
|
88
|
+
import { test, expect } from "bun:test";
|
|
89
|
+
import { render, fireEvent } from "@testing-library/svelte";
|
|
90
|
+
import Counter from "./Counter.svelte";
|
|
91
|
+
|
|
92
|
+
test("Counter increments when clicked", async () => {
|
|
93
|
+
const { getByText, component } = render(Counter);
|
|
94
|
+
const button = getByText("+1");
|
|
95
|
+
|
|
96
|
+
// Initial state
|
|
97
|
+
expect(component.$$.ctx[0]).toBe(0); // initialCount is the first prop
|
|
98
|
+
|
|
99
|
+
// Click the increment button
|
|
100
|
+
await fireEvent.click(button);
|
|
101
|
+
|
|
102
|
+
// Check the new state
|
|
103
|
+
expect(component.$$.ctx[0]).toBe(1);
|
|
104
|
+
});
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
---
|
|
108
|
+
|
|
109
|
+
Use `bun test` to run your tests.
|
|
110
|
+
|
|
111
|
+
```bash terminal icon="terminal"
|
|
112
|
+
bun test
|
|
113
|
+
```
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Using Testing Library with Bun
|
|
3
|
+
sidebarTitle: Testing Library
|
|
4
|
+
mode: center
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
You can use [Testing Library](https://testing-library.com/) with Bun's test runner.
|
|
8
|
+
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
As a prerequisite to using Testing Library you will need to install [Happy Dom](https://github.com/capricorn86/happy-dom). ([see Bun's Happy DOM guide for more information](https://bun.sh/guides/test/happy-dom)).
|
|
12
|
+
|
|
13
|
+
```sh terminal icon="terminal"
|
|
14
|
+
bun add -D @happy-dom/global-registrator
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
---
|
|
18
|
+
|
|
19
|
+
Next you should install the Testing Library packages you are planning on using. For example, if you are setting up testing for React your installs may look like this. You will also need to install `@testing-library/jest-dom` to get matchers working later.
|
|
20
|
+
|
|
21
|
+
```sh terminal icon="terminal"
|
|
22
|
+
bun add -D @testing-library/react @testing-library/dom @testing-library/jest-dom
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
---
|
|
26
|
+
|
|
27
|
+
Next you will need to create a preload script for Happy DOM and for Testing Library. For more details about the Happy DOM setup script see [Bun's Happy DOM guide](https://bun.sh/guides/test/happy-dom).
|
|
28
|
+
|
|
29
|
+
```ts happydom.ts icon="/icons/typescript.svg"
|
|
30
|
+
import { GlobalRegistrator } from "@happy-dom/global-registrator";
|
|
31
|
+
|
|
32
|
+
GlobalRegistrator.register();
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
---
|
|
36
|
+
|
|
37
|
+
For Testing Library, you will want to extend Bun's `expect` function with Testing Library's matchers. Optionally, to better match the behavior of test-runners like Jest, you may want to run cleanup after each test.
|
|
38
|
+
|
|
39
|
+
```ts testing-library.ts icon="/icons/typescript.svg"
|
|
40
|
+
import { afterEach, expect } from "bun:test";
|
|
41
|
+
import { cleanup } from "@testing-library/react";
|
|
42
|
+
import * as matchers from "@testing-library/jest-dom/matchers";
|
|
43
|
+
|
|
44
|
+
expect.extend(matchers);
|
|
45
|
+
|
|
46
|
+
// Optional: cleans up `render` after each test
|
|
47
|
+
afterEach(() => {
|
|
48
|
+
cleanup();
|
|
49
|
+
});
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
---
|
|
53
|
+
|
|
54
|
+
Next, add these preload scripts to your `bunfig.toml` (you can also have everything in a single `preload.ts` script if you prefer).
|
|
55
|
+
|
|
56
|
+
```toml bunfig.toml icon="settings"
|
|
57
|
+
[test]
|
|
58
|
+
preload = ["./happydom.ts", "./testing-library.ts"]
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
---
|
|
62
|
+
|
|
63
|
+
If you are using TypeScript you will also need to make use of declaration merging in order to get the new matcher types to show up in your editor. To do this, create a type declaration file that extends `Matchers` like this.
|
|
64
|
+
|
|
65
|
+
```ts matchers.d.ts icon="/icons/typescript.svg"
|
|
66
|
+
import { TestingLibraryMatchers } from "@testing-library/jest-dom/matchers";
|
|
67
|
+
import { Matchers, AsymmetricMatchers } from "bun:test";
|
|
68
|
+
|
|
69
|
+
declare module "bun:test" {
|
|
70
|
+
interface Matchers<T> extends TestingLibraryMatchers<typeof expect.stringContaining, T> {}
|
|
71
|
+
interface AsymmetricMatchers extends TestingLibraryMatchers {}
|
|
72
|
+
}
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
---
|
|
76
|
+
|
|
77
|
+
You should now be able to use Testing Library in your tests
|
|
78
|
+
|
|
79
|
+
```ts matchers.d.ts icon="/icons/typescript.svg"
|
|
80
|
+
import { test, expect } from "bun:test";
|
|
81
|
+
import { screen, render } from "@testing-library/react";
|
|
82
|
+
import { MyComponent } from "./myComponent";
|
|
83
|
+
|
|
84
|
+
test("Can use Testing Library", () => {
|
|
85
|
+
render(MyComponent);
|
|
86
|
+
const myComponent = screen.getByTestId("my-component");
|
|
87
|
+
expect(myComponent).toBeInTheDocument();
|
|
88
|
+
});
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
---
|
|
92
|
+
|
|
93
|
+
Refer to the [Testing Library docs](https://testing-library.com/), [Happy DOM repo](https://github.com/capricorn86/happy-dom) and [Docs > Test runner > DOM](https://bun.sh/docs/test/dom) for complete documentation on writing browser tests with Bun.
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Set a per-test timeout with the Bun test runner
|
|
3
|
+
sidebarTitle: Test timeout
|
|
4
|
+
mode: center
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
Use the `--timeout` flag to set a timeout for each test in milliseconds. If any test exceeds this timeout, it will be marked as failed.
|
|
8
|
+
|
|
9
|
+
The default timeout is `5000` (5 seconds).
|
|
10
|
+
|
|
11
|
+
```sh terminal icon="terminal"
|
|
12
|
+
bun test --timeout 3000 # 3 seconds
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
---
|
|
16
|
+
|
|
17
|
+
See [Docs > Test runner](https://bun.sh/docs/cli/test) for complete documentation of `bun test`.
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Mark a test as a "todo" with the Bun test runner
|
|
3
|
+
sidebarTitle: Todo tests
|
|
4
|
+
mode: center
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
To remind yourself to write a test later, use the `test.todo` function. There's no need to provide a test implementation.
|
|
8
|
+
|
|
9
|
+
```ts test.ts icon="/icons/typescript.svg"
|
|
10
|
+
import { test, expect } from "bun:test";
|
|
11
|
+
|
|
12
|
+
// write this later
|
|
13
|
+
test.todo("unimplemented feature");
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
---
|
|
17
|
+
|
|
18
|
+
The output of `bun test` indicates how many `todo` tests were encountered.
|
|
19
|
+
|
|
20
|
+
```sh terminal icon="terminal"
|
|
21
|
+
bun test
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
```txt
|
|
25
|
+
test.test.ts:
|
|
26
|
+
✓ add [0.03ms]
|
|
27
|
+
✓ multiply [0.02ms]
|
|
28
|
+
✎ unimplemented feature
|
|
29
|
+
|
|
30
|
+
2 pass
|
|
31
|
+
1 todo
|
|
32
|
+
0 fail
|
|
33
|
+
2 expect() calls
|
|
34
|
+
Ran 3 tests across 1 files. [74.00ms]
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
---
|
|
38
|
+
|
|
39
|
+
Optionally, you can provide a test implementation.
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
import { test, expect } from "bun:test";
|
|
43
|
+
|
|
44
|
+
test.todo("unimplemented feature", () => {
|
|
45
|
+
expect(Bun.isAwesome()).toBe(true);
|
|
46
|
+
});
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
---
|
|
50
|
+
|
|
51
|
+
If an implementation is provided, it will not be run unless the `--todo` flag is passed. If the `--todo` flag is passed, the test will be executed and _expected to fail_ by test runner! If a todo test passes, the `bun test` run will return a non-zero exit code to signal the failure.
|
|
52
|
+
|
|
53
|
+
```sh terminal icon="terminal"
|
|
54
|
+
bun test --todo
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
```txt
|
|
58
|
+
my.test.ts:
|
|
59
|
+
✗ unimplemented feature
|
|
60
|
+
^ this test is marked as todo but passes. Remove `.todo` or check that test is correct.
|
|
61
|
+
|
|
62
|
+
0 pass
|
|
63
|
+
1 fail
|
|
64
|
+
1 expect() calls
|
|
65
|
+
$ echo $?
|
|
66
|
+
1 # this is the exit code of the previous command
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
---
|
|
70
|
+
|
|
71
|
+
See also:
|
|
72
|
+
|
|
73
|
+
- [Skip a test](/guides/test/skip-tests)
|
|
74
|
+
- [Docs > Test runner > Writing tests](https://bun.sh/docs/test/writing)
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Update snapshots in `bun test`
|
|
3
|
+
sidebarTitle: Update snapshots
|
|
4
|
+
mode: center
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
Bun's test runner supports Jest-style snapshot testing via `.toMatchSnapshot()`.
|
|
8
|
+
|
|
9
|
+
```ts snap.test.ts icon="/icons/typescript.svg"
|
|
10
|
+
import { test, expect } from "bun:test";
|
|
11
|
+
|
|
12
|
+
test("snapshot", () => {
|
|
13
|
+
expect({ foo: "bar" }).toMatchSnapshot();
|
|
14
|
+
});
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
---
|
|
18
|
+
|
|
19
|
+
The first time this test is executed, Bun will write a snapshot file to disk in a directory called `__snapshots__` that lives alongside the test file.
|
|
20
|
+
|
|
21
|
+
```txt File Tree icon="folder-tree"
|
|
22
|
+
test
|
|
23
|
+
├── __snapshots__
|
|
24
|
+
│ └── snap.test.ts.snap
|
|
25
|
+
└── snap.test.ts
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
---
|
|
29
|
+
|
|
30
|
+
To regenerate snapshots, use the `--update-snapshots` flag.
|
|
31
|
+
|
|
32
|
+
```sh terminal icon="terminal"
|
|
33
|
+
bun test --update-snapshots
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
```txt
|
|
37
|
+
test/snap.test.ts:
|
|
38
|
+
✓ snapshot [0.86ms]
|
|
39
|
+
|
|
40
|
+
1 pass
|
|
41
|
+
0 fail
|
|
42
|
+
snapshots: +1 added # the snapshot was regenerated
|
|
43
|
+
1 expect() calls
|
|
44
|
+
Ran 1 tests across 1 files. [102.00ms]
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
---
|
|
48
|
+
|
|
49
|
+
See [Docs > Test Runner > Snapshots](https://bun.sh/docs/test/snapshots) for complete documentation on snapshots with the Bun test runner.
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Run tests in watch mode with Bun
|
|
3
|
+
sidebarTitle: Watch mode
|
|
4
|
+
mode: center
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
Use the `--watch` flag to run your tests in watch mode.
|
|
8
|
+
|
|
9
|
+
```sh terminal icon="terminal"
|
|
10
|
+
bun test --watch
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
---
|
|
14
|
+
|
|
15
|
+
This will restart the running Bun process whenever a file change is detected. It's fast. In this example, the editor is configured to save the file on every keystroke.
|
|
16
|
+
|
|
17
|
+
<Frame>
|
|
18
|
+

|
|
20
|
+
</Frame>
|
|
21
|
+
|
|
22
|
+
---
|
|
23
|
+
|
|
24
|
+
See [Docs > Test Runner](https://bun.sh/docs/cli/test) for complete documentation on the test runner.
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Encode and decode base64 strings
|
|
3
|
+
sidebarTitle: Base64 encoding
|
|
4
|
+
mode: center
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
Bun implements the Web-standard [`atob`](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/atob) and [`btoa`](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/btoa) functions for encoding and decoding base64 strings.
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
const data = "hello world";
|
|
11
|
+
const encoded = btoa(data); // => "aGVsbG8gd29ybGQ="
|
|
12
|
+
const decoded = atob(encoded); // => "hello world"
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
---
|
|
16
|
+
|
|
17
|
+
See [Docs > Web APIs](https://bun.com/docs/runtime/web-apis) for a complete breakdown of the Web APIs implemented in Bun.
|