bun-types 1.3.2-canary.20251105T140650 → 1.3.2-canary.20251106T140813
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/docs/README.md +28 -0
- package/package.json +1 -1
- package/docs/api/binary-data.md +0 -1038
- package/docs/api/cc.md +0 -197
- package/docs/api/color.md +0 -262
- package/docs/api/console.md +0 -57
- package/docs/api/cookie.md +0 -449
- package/docs/api/dns.md +0 -110
- package/docs/api/fetch.md +0 -463
- package/docs/api/ffi.md +0 -557
- package/docs/api/file-io.md +0 -366
- package/docs/api/file-system-router.md +0 -112
- package/docs/api/file.md +0 -19
- package/docs/api/glob.md +0 -178
- package/docs/api/globals.md +0 -387
- package/docs/api/hashing.md +0 -319
- package/docs/api/html-rewriter.md +0 -334
- package/docs/api/http.md +0 -1408
- package/docs/api/import-meta.md +0 -69
- package/docs/api/node-api.md +0 -14
- package/docs/api/redis.md +0 -597
- package/docs/api/s3.md +0 -850
- package/docs/api/secrets.md +0 -319
- package/docs/api/semver.md +0 -52
- package/docs/api/spawn.md +0 -582
- package/docs/api/sql.md +0 -1374
- package/docs/api/sqlite.md +0 -694
- package/docs/api/streams.md +0 -230
- package/docs/api/tcp.md +0 -221
- package/docs/api/transpiler.md +0 -276
- package/docs/api/udp.md +0 -123
- package/docs/api/utils.md +0 -891
- package/docs/api/websockets.md +0 -573
- package/docs/api/workers.md +0 -322
- package/docs/api/yaml.md +0 -668
- package/docs/benchmarks.md +0 -120
- package/docs/bun-flavored-toml.md +0 -42
- package/docs/bundler/css.md +0 -1028
- package/docs/bundler/css_modules.md +0 -145
- package/docs/bundler/executables.md +0 -626
- package/docs/bundler/fullstack.md +0 -418
- package/docs/bundler/hmr.md +0 -234
- package/docs/bundler/html.md +0 -349
- package/docs/bundler/index.md +0 -1735
- package/docs/bundler/intro.md +0 -75
- package/docs/bundler/loaders.md +0 -410
- package/docs/bundler/macros.md +0 -329
- package/docs/bundler/plugins.md +0 -449
- package/docs/bundler/vs-esbuild.md +0 -1127
- package/docs/cli/add.md +0 -173
- package/docs/cli/bun-completions.md +0 -3
- package/docs/cli/bun-create.md +0 -338
- package/docs/cli/bun-install.md +0 -349
- package/docs/cli/bun-upgrade.md +0 -39
- package/docs/cli/bunx.md +0 -89
- package/docs/cli/filter.md +0 -90
- package/docs/cli/info.md +0 -65
- package/docs/cli/init.md +0 -85
- package/docs/cli/install.md +0 -344
- package/docs/cli/link.md +0 -40
- package/docs/cli/outdated.md +0 -90
- package/docs/cli/patch-commit.md +0 -11
- package/docs/cli/pm.md +0 -285
- package/docs/cli/publish.md +0 -120
- package/docs/cli/remove.md +0 -7
- package/docs/cli/run.md +0 -261
- package/docs/cli/test.md +0 -397
- package/docs/cli/unlink.md +0 -9
- package/docs/cli/update.md +0 -129
- package/docs/cli/why.md +0 -67
- package/docs/contributing/upgrading-webkit.md +0 -57
- package/docs/ecosystem/elysia.md +0 -24
- package/docs/ecosystem/express.md +0 -37
- package/docs/ecosystem/hono.md +0 -18
- package/docs/ecosystem/react.md +0 -65
- package/docs/ecosystem/stric.md +0 -38
- package/docs/guides/binary/arraybuffer-to-array.md +0 -27
- package/docs/guides/binary/arraybuffer-to-blob.md +0 -24
- package/docs/guides/binary/arraybuffer-to-buffer.md +0 -25
- package/docs/guides/binary/arraybuffer-to-string.md +0 -15
- package/docs/guides/binary/arraybuffer-to-typedarray.md +0 -39
- package/docs/guides/binary/blob-to-arraybuffer.md +0 -14
- package/docs/guides/binary/blob-to-dataview.md +0 -14
- package/docs/guides/binary/blob-to-stream.md +0 -14
- package/docs/guides/binary/blob-to-string.md +0 -15
- package/docs/guides/binary/blob-to-typedarray.md +0 -14
- package/docs/guides/binary/buffer-to-arraybuffer.md +0 -14
- package/docs/guides/binary/buffer-to-blob.md +0 -14
- package/docs/guides/binary/buffer-to-readablestream.md +0 -41
- package/docs/guides/binary/buffer-to-string.md +0 -25
- package/docs/guides/binary/buffer-to-typedarray.md +0 -14
- package/docs/guides/binary/dataview-to-string.md +0 -15
- package/docs/guides/binary/typedarray-to-arraybuffer.md +0 -25
- package/docs/guides/binary/typedarray-to-blob.md +0 -16
- package/docs/guides/binary/typedarray-to-buffer.md +0 -14
- package/docs/guides/binary/typedarray-to-dataview.md +0 -14
- package/docs/guides/binary/typedarray-to-readablestream.md +0 -41
- package/docs/guides/binary/typedarray-to-string.md +0 -16
- package/docs/guides/deployment/railway.md +0 -157
- package/docs/guides/ecosystem/astro.md +0 -72
- package/docs/guides/ecosystem/discordjs.md +0 -77
- package/docs/guides/ecosystem/docker.md +0 -140
- package/docs/guides/ecosystem/drizzle.md +0 -185
- package/docs/guides/ecosystem/edgedb.md +0 -228
- package/docs/guides/ecosystem/elysia.md +0 -31
- package/docs/guides/ecosystem/express.md +0 -40
- package/docs/guides/ecosystem/hono.md +0 -39
- package/docs/guides/ecosystem/mongoose.md +0 -87
- package/docs/guides/ecosystem/neon-drizzle.md +0 -220
- package/docs/guides/ecosystem/neon-serverless-postgres.md +0 -55
- package/docs/guides/ecosystem/nextjs.md +0 -49
- package/docs/guides/ecosystem/nuxt.md +0 -56
- package/docs/guides/ecosystem/pm2.md +0 -57
- package/docs/guides/ecosystem/prisma.md +0 -141
- package/docs/guides/ecosystem/qwik.md +0 -107
- package/docs/guides/ecosystem/react.md +0 -50
- package/docs/guides/ecosystem/remix.md +0 -78
- package/docs/guides/ecosystem/render.md +0 -79
- package/docs/guides/ecosystem/sentry.md +0 -52
- package/docs/guides/ecosystem/solidstart.md +0 -58
- package/docs/guides/ecosystem/ssr-react.md +0 -51
- package/docs/guides/ecosystem/stric.md +0 -55
- package/docs/guides/ecosystem/sveltekit.md +0 -125
- package/docs/guides/ecosystem/systemd.md +0 -113
- package/docs/guides/ecosystem/vite.md +0 -71
- package/docs/guides/html-rewriter/extract-links.md +0 -68
- package/docs/guides/html-rewriter/extract-social-meta.md +0 -93
- package/docs/guides/http/cluster.md +0 -66
- package/docs/guides/http/fetch-unix.md +0 -33
- package/docs/guides/http/fetch.md +0 -24
- package/docs/guides/http/file-uploads.md +0 -94
- package/docs/guides/http/hot.md +0 -26
- package/docs/guides/http/proxy.md +0 -24
- package/docs/guides/http/server.md +0 -46
- package/docs/guides/http/simple.md +0 -18
- package/docs/guides/http/stream-file.md +0 -48
- package/docs/guides/http/stream-iterator.md +0 -47
- package/docs/guides/http/stream-node-streams-in-bun.md +0 -20
- package/docs/guides/http/tls.md +0 -30
- package/docs/guides/install/add-dev.md +0 -26
- package/docs/guides/install/add-git.md +0 -36
- package/docs/guides/install/add-optional.md +0 -25
- package/docs/guides/install/add-peer.md +0 -43
- package/docs/guides/install/add-tarball.md +0 -33
- package/docs/guides/install/add.md +0 -42
- package/docs/guides/install/azure-artifacts.md +0 -73
- package/docs/guides/install/cicd.md +0 -41
- package/docs/guides/install/custom-registry.md +0 -30
- package/docs/guides/install/from-npm-install-to-bun-install.md +0 -214
- package/docs/guides/install/git-diff-bun-lockfile.md +0 -44
- package/docs/guides/install/jfrog-artifactory.md +0 -28
- package/docs/guides/install/npm-alias.md +0 -23
- package/docs/guides/install/registry-scope.md +0 -38
- package/docs/guides/install/trusted.md +0 -46
- package/docs/guides/install/workspaces.md +0 -70
- package/docs/guides/install/yarnlock.md +0 -44
- package/docs/guides/process/argv.md +0 -58
- package/docs/guides/process/ctrl-c.md +0 -16
- package/docs/guides/process/ipc.md +0 -66
- package/docs/guides/process/nanoseconds.md +0 -13
- package/docs/guides/process/os-signals.md +0 -39
- package/docs/guides/process/spawn-stderr.md +0 -31
- package/docs/guides/process/spawn-stdout.md +0 -26
- package/docs/guides/process/spawn.md +0 -41
- package/docs/guides/process/stdin.md +0 -54
- package/docs/guides/read-file/arraybuffer.md +0 -28
- package/docs/guides/read-file/buffer.md +0 -19
- package/docs/guides/read-file/exists.md +0 -16
- package/docs/guides/read-file/json.md +0 -17
- package/docs/guides/read-file/mime.md +0 -20
- package/docs/guides/read-file/stream.md +0 -26
- package/docs/guides/read-file/string.md +0 -22
- package/docs/guides/read-file/uint8array.md +0 -21
- package/docs/guides/read-file/watch.md +0 -68
- package/docs/guides/runtime/build-time-constants.md +0 -293
- package/docs/guides/runtime/cicd.md +0 -43
- package/docs/guides/runtime/codesign-macos-executable.md +0 -56
- package/docs/guides/runtime/define-constant.md +0 -145
- package/docs/guides/runtime/delete-directory.md +0 -37
- package/docs/guides/runtime/delete-file.md +0 -19
- package/docs/guides/runtime/heap-snapshot.md +0 -26
- package/docs/guides/runtime/import-html.md +0 -15
- package/docs/guides/runtime/import-json.md +0 -44
- package/docs/guides/runtime/import-toml.md +0 -30
- package/docs/guides/runtime/import-yaml.md +0 -102
- package/docs/guides/runtime/read-env.md +0 -32
- package/docs/guides/runtime/set-env.md +0 -47
- package/docs/guides/runtime/shell.md +0 -40
- package/docs/guides/runtime/timezone.md +0 -35
- package/docs/guides/runtime/tsconfig-paths.md +0 -29
- package/docs/guides/runtime/typescript.md +0 -49
- package/docs/guides/runtime/vscode-debugger.md +0 -47
- package/docs/guides/runtime/web-debugger.md +0 -82
- package/docs/guides/streams/node-readable-to-arraybuffer.md +0 -11
- package/docs/guides/streams/node-readable-to-blob.md +0 -11
- package/docs/guides/streams/node-readable-to-json.md +0 -12
- package/docs/guides/streams/node-readable-to-string.md +0 -12
- package/docs/guides/streams/node-readable-to-uint8array.md +0 -11
- package/docs/guides/streams/to-array.md +0 -14
- package/docs/guides/streams/to-arraybuffer.md +0 -14
- package/docs/guides/streams/to-blob.md +0 -14
- package/docs/guides/streams/to-buffer.md +0 -15
- package/docs/guides/streams/to-json.md +0 -14
- package/docs/guides/streams/to-string.md +0 -14
- package/docs/guides/streams/to-typedarray.md +0 -22
- package/docs/guides/test/bail.md +0 -22
- package/docs/guides/test/coverage-threshold.md +0 -60
- package/docs/guides/test/coverage.md +0 -44
- package/docs/guides/test/happy-dom.md +0 -68
- package/docs/guides/test/migrate-from-jest.md +0 -143
- package/docs/guides/test/mock-clock.md +0 -48
- package/docs/guides/test/mock-functions.md +0 -68
- package/docs/guides/test/rerun-each.md +0 -14
- package/docs/guides/test/run-tests.md +0 -111
- package/docs/guides/test/skip-tests.md +0 -39
- package/docs/guides/test/snapshot.md +0 -95
- package/docs/guides/test/spy-on.md +0 -46
- package/docs/guides/test/svelte-test.md +0 -120
- package/docs/guides/test/testing-library.md +0 -92
- package/docs/guides/test/timeout.md +0 -15
- package/docs/guides/test/todo-tests.md +0 -67
- package/docs/guides/test/update-snapshots.md +0 -46
- package/docs/guides/test/watch-mode.md +0 -19
- package/docs/guides/util/base64.md +0 -15
- package/docs/guides/util/deep-equals.md +0 -39
- package/docs/guides/util/deflate.md +0 -18
- package/docs/guides/util/detect-bun.md +0 -23
- package/docs/guides/util/entrypoint.md +0 -17
- package/docs/guides/util/escape-html.md +0 -22
- package/docs/guides/util/file-url-to-path.md +0 -14
- package/docs/guides/util/gzip.md +0 -18
- package/docs/guides/util/hash-a-password.md +0 -54
- package/docs/guides/util/import-meta-dir.md +0 -13
- package/docs/guides/util/import-meta-file.md +0 -13
- package/docs/guides/util/import-meta-path.md +0 -13
- package/docs/guides/util/javascript-uuid.md +0 -23
- package/docs/guides/util/main.md +0 -32
- package/docs/guides/util/path-to-file-url.md +0 -14
- package/docs/guides/util/sleep.md +0 -22
- package/docs/guides/util/version.md +0 -21
- package/docs/guides/util/which-path-to-executable-bin.md +0 -15
- package/docs/guides/websocket/compression.md +0 -31
- package/docs/guides/websocket/context.md +0 -77
- package/docs/guides/websocket/pubsub.md +0 -41
- package/docs/guides/websocket/simple.md +0 -33
- package/docs/guides/write-file/append.md +0 -52
- package/docs/guides/write-file/basic.md +0 -44
- package/docs/guides/write-file/blob.md +0 -28
- package/docs/guides/write-file/cat.md +0 -17
- package/docs/guides/write-file/file-cp.md +0 -16
- package/docs/guides/write-file/filesink.md +0 -52
- package/docs/guides/write-file/response.md +0 -17
- package/docs/guides/write-file/stdout.md +0 -21
- package/docs/guides/write-file/stream.md +0 -17
- package/docs/guides/write-file/unlink.md +0 -16
- package/docs/index.md +0 -77
- package/docs/install/audit.md +0 -57
- package/docs/install/cache.md +0 -59
- package/docs/install/catalogs.md +0 -298
- package/docs/install/index.md +0 -248
- package/docs/install/isolated.md +0 -197
- package/docs/install/lifecycle.md +0 -46
- package/docs/install/lockfile.md +0 -58
- package/docs/install/npmrc.md +0 -105
- package/docs/install/overrides.md +0 -73
- package/docs/install/patch.md +0 -59
- package/docs/install/registries.md +0 -30
- package/docs/install/security-scanner-api.md +0 -81
- package/docs/install/workspaces.md +0 -114
- package/docs/installation.md +0 -327
- package/docs/project/asan.md +0 -124
- package/docs/project/benchmarking.md +0 -203
- package/docs/project/bindgen.md +0 -225
- package/docs/project/building-windows.md +0 -162
- package/docs/project/contributing.md +0 -347
- package/docs/project/internals/build-process-for-ci.md +0 -75
- package/docs/project/licensing.md +0 -73
- package/docs/project/roadmap.md +0 -87
- package/docs/quickstart.md +0 -157
- package/docs/runtime/autoimport.md +0 -94
- package/docs/runtime/bun-apis.md +0 -207
- package/docs/runtime/bunfig.md +0 -772
- package/docs/runtime/debugger.md +0 -325
- package/docs/runtime/env.md +0 -253
- package/docs/runtime/hot.md +0 -145
- package/docs/runtime/index.md +0 -312
- package/docs/runtime/jsx.md +0 -385
- package/docs/runtime/loaders.md +0 -130
- package/docs/runtime/modules.md +0 -320
- package/docs/runtime/nodejs-apis.md +0 -463
- package/docs/runtime/plugins.md +0 -561
- package/docs/runtime/shell.md +0 -605
- package/docs/runtime/typescript.md +0 -139
- package/docs/runtime/web-apis.md +0 -128
- package/docs/test/configuration.md +0 -154
- package/docs/test/coverage.md +0 -142
- package/docs/test/discovery.md +0 -85
- package/docs/test/dom.md +0 -75
- package/docs/test/examples/concurrent-test-glob.md +0 -132
- package/docs/test/hot.md +0 -15
- package/docs/test/lifecycle.md +0 -81
- package/docs/test/mocks.md +0 -313
- package/docs/test/reporters.md +0 -117
- package/docs/test/runtime-behavior.md +0 -95
- package/docs/test/snapshots.md +0 -68
- package/docs/test/time.md +0 -126
- package/docs/test/writing.md +0 -825
- package/docs/typescript.md +0 -53
package/docs/test/reporters.md
DELETED
|
@@ -1,117 +0,0 @@
|
|
|
1
|
-
bun test supports different output formats through reporters. This document covers both built-in reporters and how to implement your own custom reporters.
|
|
2
|
-
|
|
3
|
-
## Built-in Reporters
|
|
4
|
-
|
|
5
|
-
### Default Console Reporter
|
|
6
|
-
|
|
7
|
-
By default, bun test outputs results to the console in a human-readable format:
|
|
8
|
-
|
|
9
|
-
```sh
|
|
10
|
-
test/package-json-lint.test.ts:
|
|
11
|
-
✓ test/package.json [0.88ms]
|
|
12
|
-
✓ test/js/third_party/grpc-js/package.json [0.18ms]
|
|
13
|
-
✓ test/js/third_party/svelte/package.json [0.21ms]
|
|
14
|
-
✓ test/js/third_party/express/package.json [1.05ms]
|
|
15
|
-
|
|
16
|
-
4 pass
|
|
17
|
-
0 fail
|
|
18
|
-
4 expect() calls
|
|
19
|
-
Ran 4 tests in 1.44ms
|
|
20
|
-
```
|
|
21
|
-
|
|
22
|
-
When a terminal doesn't support colors, the output avoids non-ascii characters:
|
|
23
|
-
|
|
24
|
-
```sh
|
|
25
|
-
test/package-json-lint.test.ts:
|
|
26
|
-
(pass) test/package.json [0.48ms]
|
|
27
|
-
(pass) test/js/third_party/grpc-js/package.json [0.10ms]
|
|
28
|
-
(pass) test/js/third_party/svelte/package.json [0.04ms]
|
|
29
|
-
(pass) test/js/third_party/express/package.json [0.04ms]
|
|
30
|
-
|
|
31
|
-
4 pass
|
|
32
|
-
0 fail
|
|
33
|
-
4 expect() calls
|
|
34
|
-
Ran 4 tests across 1 files. [0.66ms]
|
|
35
|
-
```
|
|
36
|
-
|
|
37
|
-
### Dots Reporter
|
|
38
|
-
|
|
39
|
-
The dots reporter shows `.` for passing tests and `F` for failures—useful for large test suites.
|
|
40
|
-
|
|
41
|
-
```sh
|
|
42
|
-
$ bun test --dots
|
|
43
|
-
$ bun test --reporter=dots
|
|
44
|
-
```
|
|
45
|
-
|
|
46
|
-
### JUnit XML Reporter
|
|
47
|
-
|
|
48
|
-
For CI/CD environments, Bun supports generating JUnit XML reports. JUnit XML is a widely-adopted format for test results that can be parsed by many CI/CD systems, including GitLab, Jenkins, and others.
|
|
49
|
-
|
|
50
|
-
#### Using the JUnit Reporter
|
|
51
|
-
|
|
52
|
-
To generate a JUnit XML report, use the `--reporter=junit` flag along with `--reporter-outfile` to specify the output file:
|
|
53
|
-
|
|
54
|
-
```sh
|
|
55
|
-
$ bun test --reporter=junit --reporter-outfile=./junit.xml
|
|
56
|
-
```
|
|
57
|
-
|
|
58
|
-
This continues to output to the console as usual while also writing the JUnit XML report to the specified path at the end of the test run.
|
|
59
|
-
|
|
60
|
-
#### Configuring via bunfig.toml
|
|
61
|
-
|
|
62
|
-
You can also configure the JUnit reporter in your `bunfig.toml` file:
|
|
63
|
-
|
|
64
|
-
```toml
|
|
65
|
-
[test.reporter]
|
|
66
|
-
junit = "path/to/junit.xml" # Output path for JUnit XML report
|
|
67
|
-
```
|
|
68
|
-
|
|
69
|
-
#### Environment Variables in JUnit Reports
|
|
70
|
-
|
|
71
|
-
The JUnit reporter automatically includes environment information as `<properties>` in the XML output. This can be helpful for tracking test runs in CI environments.
|
|
72
|
-
|
|
73
|
-
Specifically, it includes the following environment variables when available:
|
|
74
|
-
|
|
75
|
-
| Environment Variable | Property Name | Description |
|
|
76
|
-
| ----------------------------------------------------------------------- | ------------- | ---------------------- |
|
|
77
|
-
| `GITHUB_RUN_ID`, `GITHUB_SERVER_URL`, `GITHUB_REPOSITORY`, `CI_JOB_URL` | `ci` | CI build information |
|
|
78
|
-
| `GITHUB_SHA`, `CI_COMMIT_SHA`, `GIT_SHA` | `commit` | Git commit identifiers |
|
|
79
|
-
| System hostname | `hostname` | Machine hostname |
|
|
80
|
-
|
|
81
|
-
This makes it easier to track which environment and commit a particular test run was for.
|
|
82
|
-
|
|
83
|
-
#### Current Limitations
|
|
84
|
-
|
|
85
|
-
The JUnit reporter currently has a few limitations that will be addressed in future updates:
|
|
86
|
-
|
|
87
|
-
- `stdout` and `stderr` output from individual tests are not included in the report
|
|
88
|
-
- Precise timestamp fields per test case are not included
|
|
89
|
-
|
|
90
|
-
### GitHub Actions reporter
|
|
91
|
-
|
|
92
|
-
Bun test automatically detects when it's running inside GitHub Actions and emits GitHub Actions annotations to the console directly. No special configuration is needed beyond installing Bun and running `bun test`.
|
|
93
|
-
|
|
94
|
-
For a GitHub Actions workflow configuration example, see the [CI/CD integration](../cli/test.md#cicd-integration) section of the CLI documentation.
|
|
95
|
-
|
|
96
|
-
## Custom Reporters
|
|
97
|
-
|
|
98
|
-
Bun allows developers to implement custom test reporters by extending the WebKit Inspector Protocol with additional testing-specific domains.
|
|
99
|
-
|
|
100
|
-
### Inspector Protocol for Testing
|
|
101
|
-
|
|
102
|
-
To support test reporting, Bun extends the standard WebKit Inspector Protocol with two custom domains:
|
|
103
|
-
|
|
104
|
-
1. **TestReporter**: Reports test discovery, execution start, and completion events
|
|
105
|
-
2. **LifecycleReporter**: Reports errors and exceptions during test execution
|
|
106
|
-
|
|
107
|
-
These extensions allow you to build custom reporting tools that can receive detailed information about test execution in real-time.
|
|
108
|
-
|
|
109
|
-
### Key Events
|
|
110
|
-
|
|
111
|
-
Custom reporters can listen for these key events:
|
|
112
|
-
|
|
113
|
-
- `TestReporter.found`: Emitted when a test is discovered
|
|
114
|
-
- `TestReporter.start`: Emitted when a test starts running
|
|
115
|
-
- `TestReporter.end`: Emitted when a test completes
|
|
116
|
-
- `Console.messageAdded`: Emitted when console output occurs during a test
|
|
117
|
-
- `LifecycleReporter.error`: Emitted when an error or exception occurs
|
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
`bun test` is deeply integrated with Bun's runtime. This is part of what makes `bun test` fast and simple to use.
|
|
2
|
-
|
|
3
|
-
#### `$NODE_ENV` environment variable
|
|
4
|
-
|
|
5
|
-
`bun test` automatically sets `$NODE_ENV` to `"test"` unless it's already set in the environment or via .env files. This is standard behavior for most test runners and helps ensure consistent test behavior.
|
|
6
|
-
|
|
7
|
-
```ts
|
|
8
|
-
import { test, expect } from "bun:test";
|
|
9
|
-
|
|
10
|
-
test("NODE_ENV is set to test", () => {
|
|
11
|
-
expect(process.env.NODE_ENV).toBe("test");
|
|
12
|
-
});
|
|
13
|
-
```
|
|
14
|
-
|
|
15
|
-
When `NODE_ENV` is set to `"test"`, Bun will not load `.env.local` files. This ensures consistent test environments across different executions by preventing local overrides during testing. Instead, use `.env.test` for test-specific environment variables, which should be committed to your repository for consistency across all developers and CI environments.
|
|
16
|
-
|
|
17
|
-
#### `$TZ` environment variable
|
|
18
|
-
|
|
19
|
-
By default, all `bun test` runs use UTC (`Etc/UTC`) as the time zone unless overridden by the `TZ` environment variable. This ensures consistent date and time behavior across different development environments.
|
|
20
|
-
|
|
21
|
-
#### Test Timeouts
|
|
22
|
-
|
|
23
|
-
Each test has a default timeout of 5000ms (5 seconds) if not explicitly overridden. Tests that exceed this timeout will fail. This can be changed globally with the `--timeout` flag or per-test as the third parameter to the test function.
|
|
24
|
-
|
|
25
|
-
## Error Handling
|
|
26
|
-
|
|
27
|
-
### Unhandled Errors
|
|
28
|
-
|
|
29
|
-
`bun test` tracks unhandled promise rejections and errors that occur between tests. If such errors occur, the final exit code will be non-zero (specifically, the count of such errors), even if all tests pass.
|
|
30
|
-
|
|
31
|
-
This helps catch errors in asynchronous code that might otherwise go unnoticed:
|
|
32
|
-
|
|
33
|
-
```ts
|
|
34
|
-
import { test } from "bun:test";
|
|
35
|
-
|
|
36
|
-
test("test 1", () => {
|
|
37
|
-
// This test passes
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
// This error happens outside any test
|
|
41
|
-
setTimeout(() => {
|
|
42
|
-
throw new Error("Unhandled error");
|
|
43
|
-
}, 0);
|
|
44
|
-
|
|
45
|
-
test("test 2", () => {
|
|
46
|
-
// This test also passes
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
// The test run will still fail with a non-zero exit code
|
|
50
|
-
// because of the unhandled error
|
|
51
|
-
```
|
|
52
|
-
|
|
53
|
-
Internally, this occurs with a higher precedence than `process.on("unhandledRejection")` or `process.on("uncaughtException")`, which makes it simpler to integrate with existing code.
|
|
54
|
-
|
|
55
|
-
## Using General CLI Flags with Tests
|
|
56
|
-
|
|
57
|
-
Several Bun CLI flags can be used with `bun test` to modify its behavior:
|
|
58
|
-
|
|
59
|
-
### Memory Usage
|
|
60
|
-
|
|
61
|
-
- `--smol`: Reduces memory usage for the test runner VM
|
|
62
|
-
|
|
63
|
-
### Debugging
|
|
64
|
-
|
|
65
|
-
- `--inspect`, `--inspect-brk`: Attaches the debugger to the test runner process
|
|
66
|
-
|
|
67
|
-
### Module Loading
|
|
68
|
-
|
|
69
|
-
- `--preload`: Runs scripts before test files (useful for global setup/mocks)
|
|
70
|
-
- `--define`: Sets compile-time constants
|
|
71
|
-
- `--loader`: Configures custom loaders
|
|
72
|
-
- `--tsconfig-override`: Uses a different tsconfig
|
|
73
|
-
- `--conditions`: Sets package.json conditions for module resolution
|
|
74
|
-
- `--env-file`: Loads environment variables for tests
|
|
75
|
-
|
|
76
|
-
### Installation-related Flags
|
|
77
|
-
|
|
78
|
-
- `--prefer-offline`, `--frozen-lockfile`, etc.: Affect any network requests or auto-installs during test execution
|
|
79
|
-
|
|
80
|
-
## Watch and Hot Reloading
|
|
81
|
-
|
|
82
|
-
When running `bun test` with the `--watch` flag, the test runner will watch for file changes and re-run affected tests.
|
|
83
|
-
|
|
84
|
-
The `--hot` flag provides similar functionality but is more aggressive about trying to preserve state between runs. For most test scenarios, `--watch` is the recommended option.
|
|
85
|
-
|
|
86
|
-
## Global Variables
|
|
87
|
-
|
|
88
|
-
The following globals are automatically available in test files without importing (though they can be imported from `bun:test` if preferred):
|
|
89
|
-
|
|
90
|
-
- `test`, `it`: Define tests
|
|
91
|
-
- `describe`: Group tests
|
|
92
|
-
- `expect`: Make assertions
|
|
93
|
-
- `beforeAll`, `beforeEach`, `afterAll`, `afterEach`: Lifecycle hooks
|
|
94
|
-
- `jest`: Jest global object
|
|
95
|
-
- `vi`: Vitest compatibility alias for common jest methods
|
package/docs/test/snapshots.md
DELETED
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
Snapshot testing saves the output of a value and compares it against future test runs. This is particularly useful for UI components, complex objects, or any output that needs to remain consistent.
|
|
2
|
-
|
|
3
|
-
## Basic snapshots
|
|
4
|
-
|
|
5
|
-
Snapshot tests are written using the `.toMatchSnapshot()` matcher:
|
|
6
|
-
|
|
7
|
-
```ts
|
|
8
|
-
import { test, expect } from "bun:test";
|
|
9
|
-
|
|
10
|
-
test("snap", () => {
|
|
11
|
-
expect("foo").toMatchSnapshot();
|
|
12
|
-
});
|
|
13
|
-
```
|
|
14
|
-
|
|
15
|
-
The first time this test is run, the argument to `expect` will be serialized and written to a special snapshot file in a `__snapshots__` directory alongside the test file. On future runs, the argument is compared against the snapshot on disk. Snapshots can be re-generated with the following command:
|
|
16
|
-
|
|
17
|
-
```bash
|
|
18
|
-
$ bun test --update-snapshots
|
|
19
|
-
```
|
|
20
|
-
|
|
21
|
-
## Inline snapshots
|
|
22
|
-
|
|
23
|
-
For smaller values, you can use inline snapshots with `.toMatchInlineSnapshot()`. These snapshots are stored directly in your test file:
|
|
24
|
-
|
|
25
|
-
```ts
|
|
26
|
-
import { test, expect } from "bun:test";
|
|
27
|
-
|
|
28
|
-
test("inline snapshot", () => {
|
|
29
|
-
// First run: snapshot will be inserted automatically
|
|
30
|
-
expect({ hello: "world" }).toMatchInlineSnapshot();
|
|
31
|
-
|
|
32
|
-
// After first run, the test file will be updated to:
|
|
33
|
-
// expect({ hello: "world" }).toMatchInlineSnapshot(`
|
|
34
|
-
// {
|
|
35
|
-
// "hello": "world",
|
|
36
|
-
// }
|
|
37
|
-
// `);
|
|
38
|
-
});
|
|
39
|
-
```
|
|
40
|
-
|
|
41
|
-
When you run the test, Bun automatically updates the test file itself with the generated snapshot string. This makes the tests more portable and easier to understand, since the expected output is right next to the test.
|
|
42
|
-
|
|
43
|
-
### Using inline snapshots
|
|
44
|
-
|
|
45
|
-
1. Write your test with `.toMatchInlineSnapshot()`
|
|
46
|
-
2. Run the test once
|
|
47
|
-
3. Bun automatically updates your test file with the snapshot
|
|
48
|
-
4. On subsequent runs, the value will be compared against the inline snapshot
|
|
49
|
-
|
|
50
|
-
Inline snapshots are particularly useful for small, simple values where it's helpful to see the expected output right in the test file.
|
|
51
|
-
|
|
52
|
-
## Error snapshots
|
|
53
|
-
|
|
54
|
-
You can also snapshot error messages using `.toThrowErrorMatchingSnapshot()` and `.toThrowErrorMatchingInlineSnapshot()`:
|
|
55
|
-
|
|
56
|
-
```ts
|
|
57
|
-
import { test, expect } from "bun:test";
|
|
58
|
-
|
|
59
|
-
test("error snapshot", () => {
|
|
60
|
-
expect(() => {
|
|
61
|
-
throw new Error("Something went wrong");
|
|
62
|
-
}).toThrowErrorMatchingSnapshot();
|
|
63
|
-
|
|
64
|
-
expect(() => {
|
|
65
|
-
throw new Error("Another error");
|
|
66
|
-
}).toThrowErrorMatchingInlineSnapshot();
|
|
67
|
-
});
|
|
68
|
-
```
|
package/docs/test/time.md
DELETED
|
@@ -1,126 +0,0 @@
|
|
|
1
|
-
`bun:test` lets you change what time it is in your tests.
|
|
2
|
-
|
|
3
|
-
This works with any of the following:
|
|
4
|
-
|
|
5
|
-
- `Date.now`
|
|
6
|
-
- `new Date()`
|
|
7
|
-
- `new Intl.DateTimeFormat().format()`
|
|
8
|
-
|
|
9
|
-
Timers are not impacted yet, but may be in a future release of Bun.
|
|
10
|
-
|
|
11
|
-
## `setSystemTime`
|
|
12
|
-
|
|
13
|
-
To change the system time, use `setSystemTime`:
|
|
14
|
-
|
|
15
|
-
```ts
|
|
16
|
-
import { setSystemTime, beforeAll, test, expect } from "bun:test";
|
|
17
|
-
|
|
18
|
-
beforeAll(() => {
|
|
19
|
-
setSystemTime(new Date("2020-01-01T00:00:00.000Z"));
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
test("it is 2020", () => {
|
|
23
|
-
expect(new Date().getFullYear()).toBe(2020);
|
|
24
|
-
});
|
|
25
|
-
```
|
|
26
|
-
|
|
27
|
-
To support existing tests that use Jest's `useFakeTimers` and `useRealTimers`, you can use `useFakeTimers` and `useRealTimers`:
|
|
28
|
-
|
|
29
|
-
```ts
|
|
30
|
-
test("just like in jest", () => {
|
|
31
|
-
jest.useFakeTimers();
|
|
32
|
-
jest.setSystemTime(new Date("2020-01-01T00:00:00.000Z"));
|
|
33
|
-
expect(new Date().getFullYear()).toBe(2020);
|
|
34
|
-
jest.useRealTimers();
|
|
35
|
-
expect(new Date().getFullYear()).toBeGreaterThan(2020);
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
test("unlike in jest", () => {
|
|
39
|
-
const OriginalDate = Date;
|
|
40
|
-
jest.useFakeTimers();
|
|
41
|
-
if (typeof Bun === "undefined") {
|
|
42
|
-
// In Jest, the Date constructor changes
|
|
43
|
-
// That can cause all sorts of bugs because suddenly Date !== Date before the test.
|
|
44
|
-
expect(Date).not.toBe(OriginalDate);
|
|
45
|
-
expect(Date.now).not.toBe(OriginalDate.now);
|
|
46
|
-
} else {
|
|
47
|
-
// In bun:test, Date constructor does not change when you useFakeTimers
|
|
48
|
-
expect(Date).toBe(OriginalDate);
|
|
49
|
-
expect(Date.now).toBe(OriginalDate.now);
|
|
50
|
-
}
|
|
51
|
-
});
|
|
52
|
-
```
|
|
53
|
-
|
|
54
|
-
{% callout %}
|
|
55
|
-
**Timers** — Note that we have not implemented builtin support for mocking timers yet, but this is on the roadmap.
|
|
56
|
-
{% /callout %}
|
|
57
|
-
|
|
58
|
-
### Reset the system time
|
|
59
|
-
|
|
60
|
-
To reset the system time, pass no arguments to `setSystemTime`:
|
|
61
|
-
|
|
62
|
-
```ts
|
|
63
|
-
import { setSystemTime, expect, test } from "bun:test";
|
|
64
|
-
|
|
65
|
-
test("it was 2020, for a moment.", () => {
|
|
66
|
-
// Set it to something!
|
|
67
|
-
setSystemTime(new Date("2020-01-01T00:00:00.000Z"));
|
|
68
|
-
expect(new Date().getFullYear()).toBe(2020);
|
|
69
|
-
|
|
70
|
-
// reset it!
|
|
71
|
-
setSystemTime();
|
|
72
|
-
|
|
73
|
-
expect(new Date().getFullYear()).toBeGreaterThan(2020);
|
|
74
|
-
});
|
|
75
|
-
```
|
|
76
|
-
|
|
77
|
-
## Get mocked time with `jest.now()`
|
|
78
|
-
|
|
79
|
-
When you're using mocked time (with `setSystemTime` or `useFakeTimers`), you can use `jest.now()` to get the current mocked timestamp:
|
|
80
|
-
|
|
81
|
-
```ts
|
|
82
|
-
import { test, expect, jest } from "bun:test";
|
|
83
|
-
|
|
84
|
-
test("get the current mocked time", () => {
|
|
85
|
-
jest.useFakeTimers();
|
|
86
|
-
jest.setSystemTime(new Date("2020-01-01T00:00:00.000Z"));
|
|
87
|
-
|
|
88
|
-
expect(Date.now()).toBe(1577836800000); // Jan 1, 2020 timestamp
|
|
89
|
-
expect(jest.now()).toBe(1577836800000); // Same value
|
|
90
|
-
|
|
91
|
-
jest.useRealTimers();
|
|
92
|
-
});
|
|
93
|
-
```
|
|
94
|
-
|
|
95
|
-
This is useful when you need to access the mocked time directly without creating a new Date object.
|
|
96
|
-
|
|
97
|
-
## Set the time zone
|
|
98
|
-
|
|
99
|
-
By default, the time zone for all `bun test` runs is set to UTC (`Etc/UTC`) unless overridden. To change the time zone, either pass the `$TZ` environment variable to `bun test`.
|
|
100
|
-
|
|
101
|
-
```sh
|
|
102
|
-
TZ=America/Los_Angeles bun test
|
|
103
|
-
```
|
|
104
|
-
|
|
105
|
-
Or set `process.env.TZ` at runtime:
|
|
106
|
-
|
|
107
|
-
```ts
|
|
108
|
-
import { test, expect } from "bun:test";
|
|
109
|
-
|
|
110
|
-
test("Welcome to California!", () => {
|
|
111
|
-
process.env.TZ = "America/Los_Angeles";
|
|
112
|
-
expect(new Date().getTimezoneOffset()).toBe(420);
|
|
113
|
-
expect(new Intl.DateTimeFormat().resolvedOptions().timeZone).toBe(
|
|
114
|
-
"America/Los_Angeles",
|
|
115
|
-
);
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
test("Welcome to New York!", () => {
|
|
119
|
-
// Unlike in Jest, you can set the timezone multiple times at runtime and it will work.
|
|
120
|
-
process.env.TZ = "America/New_York";
|
|
121
|
-
expect(new Date().getTimezoneOffset()).toBe(240);
|
|
122
|
-
expect(new Intl.DateTimeFormat().resolvedOptions().timeZone).toBe(
|
|
123
|
-
"America/New_York",
|
|
124
|
-
);
|
|
125
|
-
});
|
|
126
|
-
```
|