bun-types 1.1.37-canary.20241124T140524 → 1.1.37
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/api/binary-data.md +1028 -0
- package/docs/api/cc.md +197 -0
- package/docs/api/color.md +262 -0
- package/docs/api/console.md +38 -0
- package/docs/api/dns.md +113 -0
- package/docs/api/fetch.md +308 -0
- package/docs/api/ffi.md +536 -0
- package/docs/api/file-io.md +358 -0
- package/docs/api/file-system-router.md +112 -0
- package/docs/api/file.md +19 -0
- package/docs/api/glob.md +157 -0
- package/docs/api/globals.md +387 -0
- package/docs/api/hashing.md +314 -0
- package/docs/api/html-rewriter.md +31 -0
- package/docs/api/http.md +661 -0
- package/docs/api/import-meta.md +69 -0
- package/docs/api/node-api.md +16 -0
- package/docs/api/semver.md +52 -0
- package/docs/api/spawn.md +441 -0
- package/docs/api/sqlite.md +692 -0
- package/docs/api/streams.md +214 -0
- package/docs/api/tcp.md +221 -0
- package/docs/api/test.md +1 -0
- package/docs/api/transpiler.md +274 -0
- package/docs/api/udp.md +125 -0
- package/docs/api/utils.md +773 -0
- package/docs/api/websockets.md +547 -0
- package/docs/api/workers.md +230 -0
- package/docs/benchmarks.md +120 -0
- package/docs/bun-flavored-toml.md +42 -0
- package/docs/bundler/executables.md +291 -0
- package/docs/bundler/index.md +1543 -0
- package/docs/bundler/intro.md +75 -0
- package/docs/bundler/loaders.md +288 -0
- package/docs/bundler/macros.md +327 -0
- package/docs/bundler/plugins.md +30 -0
- package/docs/bundler/vs-esbuild.md +1127 -0
- package/docs/cli/add.md +163 -0
- package/docs/cli/bun-completions.md +3 -0
- package/docs/cli/bun-create.md +254 -0
- package/docs/cli/bun-dev.md +21 -0
- package/docs/cli/bun-install.md +255 -0
- package/docs/cli/bun-upgrade.md +39 -0
- package/docs/cli/bunx.md +80 -0
- package/docs/cli/filter.md +57 -0
- package/docs/cli/init.md +40 -0
- package/docs/cli/install.md +205 -0
- package/docs/cli/link.md +38 -0
- package/docs/cli/outdated.md +61 -0
- package/docs/cli/patch-commit.md +9 -0
- package/docs/cli/pm.md +150 -0
- package/docs/cli/publish.md +107 -0
- package/docs/cli/remove.md +5 -0
- package/docs/cli/run.md +196 -0
- package/docs/cli/test.md +247 -0
- package/docs/cli/unlink.md +7 -0
- package/docs/cli/update.md +34 -0
- package/docs/contributing/upgrading-webkit.md +57 -0
- package/docs/ecosystem/elysia.md +24 -0
- package/docs/ecosystem/express.md +37 -0
- package/docs/ecosystem/hono.md +18 -0
- package/docs/ecosystem/react.md +65 -0
- package/docs/ecosystem/stric.md +37 -0
- package/docs/guides/binary/arraybuffer-to-array.md +27 -0
- package/docs/guides/binary/arraybuffer-to-blob.md +24 -0
- package/docs/guides/binary/arraybuffer-to-buffer.md +25 -0
- package/docs/guides/binary/arraybuffer-to-string.md +15 -0
- package/docs/guides/binary/arraybuffer-to-typedarray.md +39 -0
- package/docs/guides/binary/blob-to-arraybuffer.md +14 -0
- package/docs/guides/binary/blob-to-dataview.md +14 -0
- package/docs/guides/binary/blob-to-stream.md +14 -0
- package/docs/guides/binary/blob-to-string.md +15 -0
- package/docs/guides/binary/blob-to-typedarray.md +14 -0
- package/docs/guides/binary/buffer-to-arraybuffer.md +14 -0
- package/docs/guides/binary/buffer-to-blob.md +14 -0
- package/docs/guides/binary/buffer-to-readablestream.md +41 -0
- package/docs/guides/binary/buffer-to-string.md +25 -0
- package/docs/guides/binary/buffer-to-typedarray.md +14 -0
- package/docs/guides/binary/dataview-to-string.md +15 -0
- package/docs/guides/binary/typedarray-to-arraybuffer.md +25 -0
- package/docs/guides/binary/typedarray-to-blob.md +16 -0
- package/docs/guides/binary/typedarray-to-buffer.md +14 -0
- package/docs/guides/binary/typedarray-to-dataview.md +14 -0
- package/docs/guides/binary/typedarray-to-readablestream.md +41 -0
- package/docs/guides/binary/typedarray-to-string.md +16 -0
- package/docs/guides/ecosystem/astro.md +72 -0
- package/docs/guides/ecosystem/discordjs.md +77 -0
- package/docs/guides/ecosystem/docker.md +140 -0
- package/docs/guides/ecosystem/drizzle.md +185 -0
- package/docs/guides/ecosystem/edgedb.md +228 -0
- package/docs/guides/ecosystem/elysia.md +31 -0
- package/docs/guides/ecosystem/express.md +40 -0
- package/docs/guides/ecosystem/hono.md +39 -0
- package/docs/guides/ecosystem/mongoose.md +87 -0
- package/docs/guides/ecosystem/neon-drizzle.md +220 -0
- package/docs/guides/ecosystem/neon-serverless-postgres.md +55 -0
- package/docs/guides/ecosystem/nextjs.md +38 -0
- package/docs/guides/ecosystem/nuxt.md +56 -0
- package/docs/guides/ecosystem/pm2.md +57 -0
- package/docs/guides/ecosystem/prisma.md +140 -0
- package/docs/guides/ecosystem/qwik.md +107 -0
- package/docs/guides/ecosystem/react.md +49 -0
- package/docs/guides/ecosystem/remix.md +78 -0
- package/docs/guides/ecosystem/render.md +79 -0
- package/docs/guides/ecosystem/sentry.md +52 -0
- package/docs/guides/ecosystem/solidstart.md +58 -0
- package/docs/guides/ecosystem/ssr-react.md +51 -0
- package/docs/guides/ecosystem/stric.md +55 -0
- package/docs/guides/ecosystem/sveltekit.md +125 -0
- package/docs/guides/ecosystem/systemd.md +113 -0
- package/docs/guides/ecosystem/vite.md +70 -0
- package/docs/guides/http/cluster.md +66 -0
- package/docs/guides/http/fetch-unix.md +33 -0
- package/docs/guides/http/fetch.md +24 -0
- package/docs/guides/http/file-uploads.md +94 -0
- package/docs/guides/http/hot.md +22 -0
- package/docs/guides/http/proxy.md +24 -0
- package/docs/guides/http/server.md +46 -0
- package/docs/guides/http/simple.md +18 -0
- package/docs/guides/http/stream-file.md +48 -0
- package/docs/guides/http/stream-iterator.md +47 -0
- package/docs/guides/http/stream-node-streams-in-bun.md +20 -0
- package/docs/guides/http/tls.md +30 -0
- package/docs/guides/install/add-dev.md +26 -0
- package/docs/guides/install/add-git.md +36 -0
- package/docs/guides/install/add-optional.md +25 -0
- package/docs/guides/install/add-peer.md +17 -0
- package/docs/guides/install/add-tarball.md +33 -0
- package/docs/guides/install/add.md +42 -0
- package/docs/guides/install/azure-artifacts.md +73 -0
- package/docs/guides/install/cicd.md +41 -0
- package/docs/guides/install/custom-registry.md +30 -0
- package/docs/guides/install/from-npm-install-to-bun-install.md +214 -0
- package/docs/guides/install/git-diff-bun-lockfile.md +38 -0
- package/docs/guides/install/jfrog-artifactory.md +28 -0
- package/docs/guides/install/npm-alias.md +23 -0
- package/docs/guides/install/registry-scope.md +36 -0
- package/docs/guides/install/trusted.md +48 -0
- package/docs/guides/install/workspaces.md +70 -0
- package/docs/guides/install/yarnlock.md +42 -0
- package/docs/guides/process/argv.md +57 -0
- package/docs/guides/process/ctrl-c.md +16 -0
- package/docs/guides/process/ipc.md +66 -0
- package/docs/guides/process/nanoseconds.md +13 -0
- package/docs/guides/process/os-signals.md +39 -0
- package/docs/guides/process/spawn-stderr.md +31 -0
- package/docs/guides/process/spawn-stdout.md +26 -0
- package/docs/guides/process/spawn.md +41 -0
- package/docs/guides/process/stdin.md +54 -0
- package/docs/guides/read-file/arraybuffer.md +28 -0
- package/docs/guides/read-file/buffer.md +19 -0
- package/docs/guides/read-file/exists.md +16 -0
- package/docs/guides/read-file/json.md +17 -0
- package/docs/guides/read-file/mime.md +20 -0
- package/docs/guides/read-file/stream.md +26 -0
- package/docs/guides/read-file/string.md +22 -0
- package/docs/guides/read-file/uint8array.md +21 -0
- package/docs/guides/read-file/watch.md +68 -0
- package/docs/guides/runtime/cicd.md +43 -0
- package/docs/guides/runtime/define-constant.md +145 -0
- package/docs/guides/runtime/import-html.md +15 -0
- package/docs/guides/runtime/import-json.md +44 -0
- package/docs/guides/runtime/import-toml.md +30 -0
- package/docs/guides/runtime/read-env.md +32 -0
- package/docs/guides/runtime/set-env.md +37 -0
- package/docs/guides/runtime/shell.md +40 -0
- package/docs/guides/runtime/timezone.md +35 -0
- package/docs/guides/runtime/tsconfig-paths.md +29 -0
- package/docs/guides/runtime/typescript.md +47 -0
- package/docs/guides/runtime/vscode-debugger.md +47 -0
- package/docs/guides/runtime/web-debugger.md +82 -0
- package/docs/guides/streams/node-readable-to-arraybuffer.md +11 -0
- package/docs/guides/streams/node-readable-to-blob.md +11 -0
- package/docs/guides/streams/node-readable-to-json.md +12 -0
- package/docs/guides/streams/node-readable-to-string.md +12 -0
- package/docs/guides/streams/node-readable-to-uint8array.md +11 -0
- package/docs/guides/streams/to-array.md +14 -0
- package/docs/guides/streams/to-arraybuffer.md +14 -0
- package/docs/guides/streams/to-blob.md +14 -0
- package/docs/guides/streams/to-buffer.md +15 -0
- package/docs/guides/streams/to-json.md +14 -0
- package/docs/guides/streams/to-string.md +14 -0
- package/docs/guides/streams/to-typedarray.md +22 -0
- package/docs/guides/test/bail.md +22 -0
- package/docs/guides/test/coverage-threshold.md +60 -0
- package/docs/guides/test/coverage.md +44 -0
- package/docs/guides/test/happy-dom.md +68 -0
- package/docs/guides/test/migrate-from-jest.md +110 -0
- package/docs/guides/test/mock-clock.md +48 -0
- package/docs/guides/test/mock-functions.md +68 -0
- package/docs/guides/test/rerun-each.md +14 -0
- package/docs/guides/test/run-tests.md +111 -0
- package/docs/guides/test/skip-tests.md +39 -0
- package/docs/guides/test/snapshot.md +99 -0
- package/docs/guides/test/spy-on.md +46 -0
- package/docs/guides/test/testing-library.md +87 -0
- package/docs/guides/test/timeout.md +15 -0
- package/docs/guides/test/todo-tests.md +67 -0
- package/docs/guides/test/update-snapshots.md +50 -0
- package/docs/guides/test/watch-mode.md +19 -0
- package/docs/guides/util/base64.md +15 -0
- package/docs/guides/util/deep-equals.md +39 -0
- package/docs/guides/util/deflate.md +18 -0
- package/docs/guides/util/detect-bun.md +23 -0
- package/docs/guides/util/entrypoint.md +17 -0
- package/docs/guides/util/escape-html.md +22 -0
- package/docs/guides/util/file-url-to-path.md +14 -0
- package/docs/guides/util/gzip.md +18 -0
- package/docs/guides/util/hash-a-password.md +54 -0
- package/docs/guides/util/import-meta-dir.md +13 -0
- package/docs/guides/util/import-meta-file.md +13 -0
- package/docs/guides/util/import-meta-path.md +13 -0
- package/docs/guides/util/main.md +32 -0
- package/docs/guides/util/path-to-file-url.md +14 -0
- package/docs/guides/util/sleep.md +22 -0
- package/docs/guides/util/version.md +21 -0
- package/docs/guides/util/which-path-to-executable-bin.md +15 -0
- package/docs/guides/websocket/compression.md +31 -0
- package/docs/guides/websocket/context.md +72 -0
- package/docs/guides/websocket/pubsub.md +38 -0
- package/docs/guides/websocket/simple.md +33 -0
- package/docs/guides/write-file/append.md +52 -0
- package/docs/guides/write-file/basic.md +44 -0
- package/docs/guides/write-file/blob.md +28 -0
- package/docs/guides/write-file/cat.md +17 -0
- package/docs/guides/write-file/file-cp.md +16 -0
- package/docs/guides/write-file/filesink.md +52 -0
- package/docs/guides/write-file/response.md +17 -0
- package/docs/guides/write-file/stdout.md +21 -0
- package/docs/guides/write-file/stream.md +17 -0
- package/docs/guides/write-file/unlink.md +23 -0
- package/docs/index.md +77 -0
- package/docs/install/cache.md +57 -0
- package/docs/install/index.md +202 -0
- package/docs/install/lifecycle.md +46 -0
- package/docs/install/lockfile.md +90 -0
- package/docs/install/npmrc.md +75 -0
- package/docs/install/overrides.md +73 -0
- package/docs/install/patch.md +57 -0
- package/docs/install/registries.md +30 -0
- package/docs/install/workspaces.md +70 -0
- package/docs/installation.md +289 -0
- package/docs/project/benchmarking.md +203 -0
- package/docs/project/building-windows.md +162 -0
- package/docs/project/internals/build-process-for-ci.md +75 -0
- package/docs/project/roadmap.md +87 -0
- package/docs/quickstart.md +144 -0
- package/docs/runtime/autoimport.md +94 -0
- package/docs/runtime/bun-apis.md +129 -0
- package/docs/runtime/bunfig.md +532 -0
- package/docs/runtime/debugger.md +325 -0
- package/docs/runtime/env.md +214 -0
- package/docs/runtime/hot.md +139 -0
- package/docs/runtime/index.md +309 -0
- package/docs/runtime/jsx.md +326 -0
- package/docs/runtime/loaders.md +127 -0
- package/docs/runtime/modules.md +298 -0
- package/docs/runtime/nodejs-apis.md +456 -0
- package/docs/runtime/plugins.md +605 -0
- package/docs/runtime/shell.md +537 -0
- package/docs/runtime/typescript.md +60 -0
- package/docs/runtime/web-apis.md +128 -0
- package/docs/test/coverage.md +91 -0
- package/docs/test/dom.md +75 -0
- package/docs/test/hot.md +15 -0
- package/docs/test/lifecycle.md +81 -0
- package/docs/test/mocks.md +236 -0
- package/docs/test/snapshots.md +15 -0
- package/docs/test/time.md +106 -0
- package/docs/test/writing.md +547 -0
- package/docs/typescript.md +51 -0
- package/package.json +5 -4
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Debugging Bun with the VS Code extension
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
{% note %}
|
|
6
|
+
|
|
7
|
+
VSCode extension support is currently buggy. We recommend the [Web Debugger](https://bun.sh/guides/runtime/web-debugger) for now.
|
|
8
|
+
|
|
9
|
+
{% /note %}
|
|
10
|
+
|
|
11
|
+
Bun speaks the [WebKit Inspector Protocol](https://github.com/oven-sh/bun/blob/main/packages/bun-vscode/types/jsc.d.ts) so you can debug your code with an interactive debugger.
|
|
12
|
+
|
|
13
|
+
---
|
|
14
|
+
|
|
15
|
+
To install the extension, visit the [Bun for Visual Studio Code](https://marketplace.visualstudio.com/items?itemName=oven.bun-vscode) page on the VS Code marketplace website, then click Install.
|
|
16
|
+
|
|
17
|
+
{% image src="https://github.com/oven-sh/bun/assets/3084745/7c8c80e6-d49e-457a-a45e-45ebed946d56" /%}
|
|
18
|
+
|
|
19
|
+
---
|
|
20
|
+
|
|
21
|
+
Alternatively, search `bun-vscode` in the Extensions tab of VS Code.
|
|
22
|
+
|
|
23
|
+
{% image src="https://github.com/oven-sh/bun/assets/3084745/664b4c40-944c-4076-a4c2-f812aebd3dc9" /%}
|
|
24
|
+
|
|
25
|
+
---
|
|
26
|
+
|
|
27
|
+
Make sure you are installing the extension published by the verified Oven organization.
|
|
28
|
+
|
|
29
|
+
{% image src="https://github.com/oven-sh/bun/assets/3084745/73e6b09f-9ff1-4d85-b725-c5eb7215b6ae" /%}
|
|
30
|
+
|
|
31
|
+
---
|
|
32
|
+
|
|
33
|
+
Once installed, two new Bun-specific commands will appear in the Command Palette. To open the palette, click View > Command Palette, or type `Ctrl+Shift+P` (Windows, Linux) or `Cmd+Shift+P` on (Mac).
|
|
34
|
+
|
|
35
|
+
---
|
|
36
|
+
|
|
37
|
+
The `Bun: Run File` command will execute your code and print the output to the Debug Console in VS Code. Breakpoints will be ignored; this is similar to executing the file with `bun <file>` from the command line.
|
|
38
|
+
|
|
39
|
+
{% image src="https://github.com/oven-sh/bun/assets/3084745/1b2c7fd9-fbb9-486a-84d0-eb7ec135ded3" /%}
|
|
40
|
+
|
|
41
|
+
---
|
|
42
|
+
|
|
43
|
+
The `Bun: Debug File` command will execute your code and print the output to the Debug Console in VS Code. You can set breakpoints in your code by clicking to the left of a line number; a red dot should appear.
|
|
44
|
+
|
|
45
|
+
When you run the file with `Bun: Debug File`, execution will pause at the breakpoint. You can inspect the variables in scope and step through the code line-by-line using the VS Code controls.
|
|
46
|
+
|
|
47
|
+
{% image src="https://github.com/oven-sh/bun/assets/3084745/c579a36c-eb21-4a58-bc9c-74612aad82af" /%}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Debugging Bun with the web debugger
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
Bun speaks the [WebKit Inspector Protocol](https://github.com/oven-sh/bun/blob/main/packages/bun-vscode/types/jsc.d.ts). To enable debugging when running code with Bun, use the `--inspect` flag. For demonstration purposes, consider the following simple web server.
|
|
6
|
+
|
|
7
|
+
```ts#server.ts
|
|
8
|
+
Bun.serve({
|
|
9
|
+
fetch(req){
|
|
10
|
+
console.log(req.url);
|
|
11
|
+
return new Response("Hello, world!");
|
|
12
|
+
}
|
|
13
|
+
})
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
---
|
|
17
|
+
|
|
18
|
+
Let's run this file with the `--inspect` flag.
|
|
19
|
+
|
|
20
|
+
This automatically starts a WebSocket server on an available port that can be used to introspect the running Bun process. Various debugging tools can connect to this server to provide an interactive debugging experience.
|
|
21
|
+
|
|
22
|
+
Bun hosts a web-based debugger at [debug.bun.sh](https://debug.bun.sh). It is a modified version of WebKit's [Web Inspector Interface](https://webkit.org/web-inspector/web-inspector-interface/), which will look familiar to Safari users.
|
|
23
|
+
|
|
24
|
+
```sh
|
|
25
|
+
$ bun --inspect server.ts
|
|
26
|
+
------------------ Bun Inspector ------------------
|
|
27
|
+
Listening at:
|
|
28
|
+
ws://localhost:6499/0tqxs9exrgrm
|
|
29
|
+
|
|
30
|
+
Inspect in browser:
|
|
31
|
+
https://debug.bun.sh/#localhost:6499/0tqxs9exrgrm
|
|
32
|
+
------------------ Bun Inspector ------------------
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
---
|
|
36
|
+
|
|
37
|
+
Open the provided `debug.bun.sh` URL in your browser to start a debugging session. From this interface, you'll be able to view the source code of the running file, view and set breakpoints, and execute code with the built-in console.
|
|
38
|
+
|
|
39
|
+
{% image src="https://github.com/oven-sh/bun/assets/3084745/e6a976a8-80cc-4394-8925-539025cc025d" alt="Screenshot of Bun debugger, Console tab" /%}
|
|
40
|
+
|
|
41
|
+
---
|
|
42
|
+
|
|
43
|
+
Let's set a breakpoint. Navigate to the Sources tab; you should see the code from earlier. Click on the line number `3` to set a breakpoint on our `console.log(req.url)` statement.
|
|
44
|
+
|
|
45
|
+
{% image src="https://github.com/oven-sh/bun/assets/3084745/3b69c7e9-25ff-4f9d-acc4-caa736862935" alt="screenshot of Bun debugger" /%}
|
|
46
|
+
|
|
47
|
+
---
|
|
48
|
+
|
|
49
|
+
Then visit [`http://localhost:3000`](http://localhost:3000) in your web browser. This will send an HTTP request to our `localhost` web server. It will seem like the page isn't loading. Why? Because the program has paused execution at the breakpoint we set earlier.
|
|
50
|
+
|
|
51
|
+
Note how the UI has changed.
|
|
52
|
+
|
|
53
|
+
{% image src="https://github.com/oven-sh/bun/assets/3084745/8b565e58-5445-4061-9bc4-f41090dfe769" alt="screenshot of Bun debugger" /%}
|
|
54
|
+
|
|
55
|
+
---
|
|
56
|
+
|
|
57
|
+
At this point there's a lot we can do to introspect the current execution environment. We can use the console at the bottom to run arbitrary code in the context of the program, with full access to the variables in scope at our breakpoint.
|
|
58
|
+
|
|
59
|
+
{% image src="https://github.com/oven-sh/bun/assets/3084745/f4312b76-48ba-4a7d-b3b6-6205968ac681" /%}
|
|
60
|
+
|
|
61
|
+
---
|
|
62
|
+
|
|
63
|
+
On the right side of the Sources pane, we can see all local variables currently in scope, and drill down to see their properties and methods. Here, we're inspecting the `req` variable.
|
|
64
|
+
|
|
65
|
+
{% image src="https://github.com/oven-sh/bun/assets/3084745/63d7f843-5180-489c-aa94-87c486e68646" /%}
|
|
66
|
+
|
|
67
|
+
---
|
|
68
|
+
|
|
69
|
+
In the upper left of the Sources pane, we can control the execution of the program.
|
|
70
|
+
|
|
71
|
+
{% image src="https://github.com/oven-sh/bun/assets/3084745/41b76deb-7371-4461-9d5d-81b5a6d2f7a4" /%}
|
|
72
|
+
|
|
73
|
+
---
|
|
74
|
+
|
|
75
|
+
Here's a cheat sheet explaining the functions of the control flow buttons.
|
|
76
|
+
|
|
77
|
+
- _Continue script execution_ — continue running the program until the next breakpoint or exception.
|
|
78
|
+
- _Step over_ — The program will continue to the next line.
|
|
79
|
+
- _Step into_ — If the current statement contains a function call, the debugger will "step into" the called function.
|
|
80
|
+
- _Step out_ — If the current statement is a function call, the debugger will finish executing the call, then "step out" of the function to the location where it was called.
|
|
81
|
+
|
|
82
|
+
{% image src="https://github-production-user-asset-6210df.s3.amazonaws.com/3084745/261510346-6a94441c-75d3-413a-99a7-efa62365f83d.png" /%}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Convert a Node.js Readable to an ArrayBuffer
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
To convert a Node.js `Readable` stream to an `ArrayBuffer` in Bun, you can create a new `Response` object with the stream as the body, then use `arrayBuffer()` to read the stream into an `ArrayBuffer`.
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
import { Readable } from "stream";
|
|
9
|
+
const stream = Readable.from(["Hello, ", "world!"]);
|
|
10
|
+
const buf = await new Response(stream).arrayBuffer();
|
|
11
|
+
```
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Convert a Node.js Readable to a Blob
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
To convert a Node.js `Readable` stream to a [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob) in Bun, you can create a new [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) object with the stream as the body, then use [`response.blob()`](https://developer.mozilla.org/en-US/docs/Web/API/Response/blob) to read the stream into a [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob).
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
import { Readable } from "stream";
|
|
9
|
+
const stream = Readable.from(["Hello, ", "world!"]);
|
|
10
|
+
const blob = await new Response(stream).blob();
|
|
11
|
+
```
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Convert a Node.js Readable to JSON
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
To convert a Node.js `Readable` stream to a JSON object in Bun, you can create a new [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) object with the stream as the body, then use [`response.json()`](https://developer.mozilla.org/en-US/docs/Web/API/Response/json) to read the stream into a JSON object.
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
import { Readable } from "stream";
|
|
9
|
+
const stream = Readable.from([JSON.stringify({ hello: "world" })]);
|
|
10
|
+
const json = await new Response(stream).json();
|
|
11
|
+
console.log(json); // { hello: "world" }
|
|
12
|
+
```
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Convert a Node.js Readable to a string
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
To convert a Node.js `Readable` stream to a string in Bun, you can create a new [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) object with the stream as the body, then use [`response.text()`](https://developer.mozilla.org/en-US/docs/Web/API/Response/text) to read the stream into a string.
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
import { Readable } from "stream";
|
|
9
|
+
const stream = Readable.from([Buffer.from("Hello, world!")]);
|
|
10
|
+
const text = await new Response(stream).text();
|
|
11
|
+
console.log(text); // "Hello, world!"
|
|
12
|
+
```
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Convert a Node.js Readable to an Uint8Array
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
To convert a Node.js `Readable` stream to an `Uint8Array` in Bun, you can create a new `Response` object with the stream as the body, then use `bytes()` to read the stream into an `Uint8Array`.
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
import { Readable } from "stream";
|
|
9
|
+
const stream = Readable.from(["Hello, ", "world!"]);
|
|
10
|
+
const buf = await new Response(stream).bytes();
|
|
11
|
+
```
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Convert a ReadableStream to an array of chunks
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
Bun provides a number of convenience functions for reading the contents of a [`ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream) into different formats. The `Bun.readableStreamToArray` function reads the contents of a `ReadableStream` to an array of chunks.
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
const stream = new ReadableStream();
|
|
9
|
+
const str = await Bun.readableStreamToArray(stream);
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
---
|
|
13
|
+
|
|
14
|
+
See [Docs > API > Utils](https://bun.sh/docs/api/utils#bun-readablestreamto) for documentation on Bun's other `ReadableStream` conversion functions.
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Convert a ReadableStream to an ArrayBuffer
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
Bun provides a number of convenience functions for reading the contents of a [`ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream) into different formats.
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
const stream = new ReadableStream();
|
|
9
|
+
const buf = await Bun.readableStreamToArrayBuffer(stream);
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
---
|
|
13
|
+
|
|
14
|
+
See [Docs > API > Utils](https://bun.sh/docs/api/utils#bun-readablestreamto) for documentation on Bun's other `ReadableStream` conversion functions.
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Convert a ReadableStream to a Blob
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
Bun provides a number of convenience functions for reading the contents of a [`ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream) into different formats.
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
const stream = new ReadableStream();
|
|
9
|
+
const blob = await Bun.readableStreamToBlob(stream);
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
---
|
|
13
|
+
|
|
14
|
+
See [Docs > API > Utils](https://bun.sh/docs/api/utils#bun-readablestreamto) for documentation on Bun's other `ReadableStream` conversion functions.
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Convert a ReadableStream to a Buffer
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
Bun provides a number of convenience functions for reading the contents of a [`ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream) into different formats. This snippet reads the contents of a `ReadableStream` to an [`ArrayBuffer`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer), then creates a [`Buffer`](https://nodejs.org/api/buffer.html) that points to it.
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
const stream = new ReadableStream();
|
|
9
|
+
const arrBuf = await Bun.readableStreamToArrayBuffer(stream);
|
|
10
|
+
const nodeBuf = Buffer.from(arrBuf);
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
---
|
|
14
|
+
|
|
15
|
+
See [Docs > API > Utils](https://bun.sh/docs/api/utils#bun-readablestreamto) for documentation on Bun's other `ReadableStream` conversion functions.
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Convert a ReadableStream to JSON
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
Bun provides a number of convenience functions for reading the contents of a [`ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream) into different formats.
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
const stream = new ReadableStream();
|
|
9
|
+
const json = await Bun.readableStreamToJSON(stream);
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
---
|
|
13
|
+
|
|
14
|
+
See [Docs > API > Utils](https://bun.sh/docs/api/utils#bun-readablestreamto) for documentation on Bun's other `ReadableStream` conversion functions.
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Convert a ReadableStream to a string
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
Bun provides a number of convenience functions for reading the contents of a [`ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream) into different formats.
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
const stream = new ReadableStream();
|
|
9
|
+
const str = await Bun.readableStreamToText(stream);
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
---
|
|
13
|
+
|
|
14
|
+
See [Docs > API > Utils](https://bun.sh/docs/api/utils#bun-readablestreamto) for documentation on Bun's other `ReadableStream` conversion functions.
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Convert a ReadableStream to a Uint8Array
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
Bun provides a number of convenience functions for reading the contents of a [`ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream) into different formats. This snippet reads the contents of a `ReadableStream` to an [`ArrayBuffer`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer), then creates a [`Uint8Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array) that points to the buffer.
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
const stream = new ReadableStream();
|
|
9
|
+
const buf = await Bun.readableStreamToArrayBuffer(stream);
|
|
10
|
+
const uint8 = new Uint8Array(buf);
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Additionally, there is a convenience method to convert to `Uint8Array` directly.
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
const stream = new ReadableStream();
|
|
17
|
+
const uint8 = await Bun.readableStreamToBytes(stream);
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
---
|
|
21
|
+
|
|
22
|
+
See [Docs > API > Utils](https://bun.sh/docs/api/utils#bun-readablestreamto) for documentation on Bun's other `ReadableStream` conversion functions.
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Bail early with the Bun test runner
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
Use the `--bail` flag to bail on a test run after a single failure. This is useful for aborting as soon as possible in a continuous integration environment.
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
$ bun test --bail
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
To bail after a certain threshold of failures, optionally specify a number after the flag.
|
|
14
|
+
|
|
15
|
+
```sh
|
|
16
|
+
# bail after 10 failures
|
|
17
|
+
$ bun test --bail=10
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
---
|
|
21
|
+
|
|
22
|
+
See [Docs > Test runner](https://bun.sh/docs/cli/test) for complete documentation of `bun test`.
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Set a code coverage threshold with the Bun test runner
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
Bun's test runner supports built-in code coverage reporting via the `--coverage` flag.
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
$ bun test --coverage
|
|
9
|
+
|
|
10
|
+
test.test.ts:
|
|
11
|
+
✓ math > add [0.71ms]
|
|
12
|
+
✓ math > multiply [0.03ms]
|
|
13
|
+
✓ random [0.13ms]
|
|
14
|
+
-------------|---------|---------|-------------------
|
|
15
|
+
File | % Funcs | % Lines | Uncovered Line #s
|
|
16
|
+
-------------|---------|---------|-------------------
|
|
17
|
+
All files | 66.67 | 77.78 |
|
|
18
|
+
math.ts | 50.00 | 66.67 |
|
|
19
|
+
random.ts | 50.00 | 66.67 |
|
|
20
|
+
-------------|---------|---------|-------------------
|
|
21
|
+
|
|
22
|
+
3 pass
|
|
23
|
+
0 fail
|
|
24
|
+
3 expect() calls
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
---
|
|
28
|
+
|
|
29
|
+
To set a minimum coverage threshold, add the following line to your `bunfig.toml`. This requires that 90% of your codebase is covered by tests.
|
|
30
|
+
|
|
31
|
+
```toml
|
|
32
|
+
[test]
|
|
33
|
+
# to require 90% line-level and function-level coverage
|
|
34
|
+
coverageThreshold = 0.9
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
---
|
|
38
|
+
|
|
39
|
+
If your test suite does not meet this threshold, `bun test` will exit with a non-zero exit code to signal a failure.
|
|
40
|
+
|
|
41
|
+
```sh
|
|
42
|
+
$ bun test --coverage
|
|
43
|
+
<test output>
|
|
44
|
+
$ echo $?
|
|
45
|
+
1 # this is the exit code of the previous command
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
---
|
|
49
|
+
|
|
50
|
+
Different thresholds can be set for line-level and function-level coverage.
|
|
51
|
+
|
|
52
|
+
```toml
|
|
53
|
+
[test]
|
|
54
|
+
# to set different thresholds for lines and functions
|
|
55
|
+
coverageThreshold = { lines = 0.5, functions = 0.7 }
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
---
|
|
59
|
+
|
|
60
|
+
See [Docs > Test runner > Coverage](https://bun.sh/docs/test/coverage) for complete documentation on code coverage reporting in Bun.
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Generate code coverage reports with the Bun test runner
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
Bun's test runner supports built-in _code coverage reporting_. This makes it easy to see how much of the codebase is covered by tests and find areas that are not currently well-tested.
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
Pass the `--coverage` flag to `bun test` to enable this feature. This will print a coverage report after the test run.
|
|
10
|
+
|
|
11
|
+
The coverage report lists the source files that were executed during the test run, the percentage of functions and lines that were executed, and the line ranges that were not executed during the run.
|
|
12
|
+
|
|
13
|
+
```sh
|
|
14
|
+
$ bun test --coverage
|
|
15
|
+
|
|
16
|
+
test.test.ts:
|
|
17
|
+
✓ math > add [0.71ms]
|
|
18
|
+
✓ math > multiply [0.03ms]
|
|
19
|
+
✓ random [0.13ms]
|
|
20
|
+
-------------|---------|---------|-------------------
|
|
21
|
+
File | % Funcs | % Lines | Uncovered Line #s
|
|
22
|
+
-------------|---------|---------|-------------------
|
|
23
|
+
All files | 66.67 | 77.78 |
|
|
24
|
+
math.ts | 50.00 | 66.67 |
|
|
25
|
+
random.ts | 50.00 | 66.67 |
|
|
26
|
+
-------------|---------|---------|-------------------
|
|
27
|
+
|
|
28
|
+
3 pass
|
|
29
|
+
0 fail
|
|
30
|
+
3 expect() calls
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
---
|
|
34
|
+
|
|
35
|
+
To always enable coverage reporting by default, add the following line to your `bunfig.toml`:
|
|
36
|
+
|
|
37
|
+
```toml
|
|
38
|
+
[test]
|
|
39
|
+
coverage = true # always enable coverage
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
---
|
|
43
|
+
|
|
44
|
+
Refer to [Docs > Test runner > Coverage](https://bun.sh/docs/test/coverage) for complete documentation on code coverage reporting in Bun.
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Write browser DOM tests with Bun and happy-dom
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
You can write and run browser tests with Bun's test runner in conjunction with [Happy DOM](https://github.com/capricorn86/happy-dom). Happy DOM implements mocked versions of browser APIs like `document` and `location`.
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
To get started, install `happy-dom`.
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
$ bun add -d @happy-dom/global-registrator
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
---
|
|
16
|
+
|
|
17
|
+
This module exports a "registrator" that injects the mocked browser APIs to the global scope.
|
|
18
|
+
|
|
19
|
+
```ts#happydom.ts
|
|
20
|
+
import { GlobalRegistrator } from "@happy-dom/global-registrator";
|
|
21
|
+
|
|
22
|
+
GlobalRegistrator.register();
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
---
|
|
26
|
+
|
|
27
|
+
We need to make sure this file is executed before any of our test files. That's a job for Bun's built-in [_preload_]() functionality. Create a `bunfig.toml` file in the root of your project (if it doesn't already exist) and add the following lines.
|
|
28
|
+
|
|
29
|
+
The `./happydom.ts` file should contain the registration code above.
|
|
30
|
+
|
|
31
|
+
```toml#bunfig.toml
|
|
32
|
+
[test]
|
|
33
|
+
preload = "./happydom.ts"
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
---
|
|
37
|
+
|
|
38
|
+
Now running `bun test` inside our project will automatically execute `happydom.ts` first. We can start writing tests that use browser APIs.
|
|
39
|
+
|
|
40
|
+
```ts
|
|
41
|
+
import { test, expect } from "bun:test";
|
|
42
|
+
|
|
43
|
+
test("set button text", () => {
|
|
44
|
+
document.body.innerHTML = `<button>My button</button>`;
|
|
45
|
+
const button = document.querySelector("button");
|
|
46
|
+
expect(button?.innerText).toEqual("My button");
|
|
47
|
+
});
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
---
|
|
51
|
+
|
|
52
|
+
With Happy DOM properly configured, this test runs as expected.
|
|
53
|
+
|
|
54
|
+
```sh
|
|
55
|
+
$ bun test
|
|
56
|
+
|
|
57
|
+
dom.test.ts:
|
|
58
|
+
✓ set button text [0.82ms]
|
|
59
|
+
|
|
60
|
+
1 pass
|
|
61
|
+
0 fail
|
|
62
|
+
1 expect() calls
|
|
63
|
+
Ran 1 tests across 1 files. 1 total [125.00ms]
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
---
|
|
67
|
+
|
|
68
|
+
Refer to the [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,110 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Migrate from Jest to Bun's test runner
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
In many cases, Bun's test runner can run Jest test suites with no code changes. Just run `bun test` instead of `npx jest`, `yarn test`, etc.
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
- $ npx jest
|
|
9
|
+
- $ yarn test
|
|
10
|
+
+ $ bun test
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
---
|
|
14
|
+
|
|
15
|
+
There's often no need for code changes.
|
|
16
|
+
|
|
17
|
+
- Bun internally re-writes imports from `@jest/globals` to use the `bun:test` equivalents.
|
|
18
|
+
- If you're relying on Jest to inject `test`, `expect`, etc. as globals, Bun does that too.
|
|
19
|
+
|
|
20
|
+
But if you'd rather switch to the `bun:test` imports, you can do that too.
|
|
21
|
+
|
|
22
|
+
```ts-diff
|
|
23
|
+
- import {test, expect} from "@jest/globals";
|
|
24
|
+
+ import {test, expect} from "bun:test";
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
---
|
|
28
|
+
|
|
29
|
+
Bun implements the vast majority of Jest's matchers, but compatibility isn't 100% yet. Refer to the full compatibility table at [Docs > Test runner > Writing tests](https://bun.sh/docs/test/writing#matchers).
|
|
30
|
+
|
|
31
|
+
Some notable missing features:
|
|
32
|
+
|
|
33
|
+
- `expect().toMatchInlineSnapshot()`
|
|
34
|
+
- `expect().toHaveReturned()`
|
|
35
|
+
|
|
36
|
+
---
|
|
37
|
+
|
|
38
|
+
If you're using `testEnvironment: "jsdom"` to run your tests in a browser-like environment, you should follow the [DOM testing with Bun and happy-dom](/guides/test/happy-dom) guide to inject browser APIs into the global scope. This guide relies on [`happy-dom`](https://github.com/capricorn86/happy-dom), which is a leaner and faster alternative to [`jsdom`](https://github.com/jsdom/jsdom).
|
|
39
|
+
|
|
40
|
+
At the moment jsdom does not work in Bun due to its internal use of V8 APIs. Track support for it [here](https://github.com/oven-sh/bun/issues/3554).
|
|
41
|
+
|
|
42
|
+
```toml#bunfig.toml
|
|
43
|
+
[test]
|
|
44
|
+
preload = ["./happy-dom.ts"]
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
---
|
|
48
|
+
|
|
49
|
+
Replace `bail` in your Jest config with the `--bail` CLI flag.
|
|
50
|
+
|
|
51
|
+
<!-- ```ts-diff
|
|
52
|
+
- import type {Config} from 'jest';
|
|
53
|
+
-
|
|
54
|
+
- const config: Config = {
|
|
55
|
+
- bail: 3
|
|
56
|
+
- };
|
|
57
|
+
``` -->
|
|
58
|
+
|
|
59
|
+
```sh
|
|
60
|
+
$ bun test --bail=3
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
---
|
|
64
|
+
|
|
65
|
+
Replace `collectCoverage` with the `--coverage` CLI flag.
|
|
66
|
+
|
|
67
|
+
<!-- ```ts-diff
|
|
68
|
+
- import type {Config} from 'jest';
|
|
69
|
+
-
|
|
70
|
+
- const config: Config = {
|
|
71
|
+
- collectCoverageFrom: [
|
|
72
|
+
- '**/*.{js,jsx}',
|
|
73
|
+
- '!**/node_modules/**',
|
|
74
|
+
- '!**/vendor/**',
|
|
75
|
+
- ],
|
|
76
|
+
- };
|
|
77
|
+
``` -->
|
|
78
|
+
|
|
79
|
+
```sh
|
|
80
|
+
$ bun test --coverage
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
---
|
|
84
|
+
|
|
85
|
+
Replace `testTimeout` with the `--test-timeout` CLI flag.
|
|
86
|
+
|
|
87
|
+
```sh
|
|
88
|
+
$ bun test --timeout 10000
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
---
|
|
92
|
+
|
|
93
|
+
Many other flags become irrelevant or obsolete when using `bun test`.
|
|
94
|
+
|
|
95
|
+
- `transform` — Bun supports TypeScript & JSX. Other file types can be configured with [Plugins](https://bun.sh/docs/runtime/plugins).
|
|
96
|
+
- `extensionsToTreatAsEsm`
|
|
97
|
+
- `haste` — Bun uses it's own internal source maps
|
|
98
|
+
- `watchman`, `watchPlugins`, `watchPathIgnorePatterns` — use `--watch` to run tests in watch mode
|
|
99
|
+
- `verbose` — set `logLevel: "debug"` in [`bunfig.toml`](https://bun.sh/docs/runtime/bunfig#loglevel)
|
|
100
|
+
|
|
101
|
+
---
|
|
102
|
+
|
|
103
|
+
Settings that aren't mentioned here are not supported or have no equivalent. Please [file a feature request](https://github.com/oven-sh/bun) if something important is missing.
|
|
104
|
+
|
|
105
|
+
---
|
|
106
|
+
|
|
107
|
+
See also:
|
|
108
|
+
|
|
109
|
+
- [Mark a test as a todo](/guides/test/todo-tests)
|
|
110
|
+
- [Docs > Test runner > Writing tests](https://bun.sh/docs/test/writing)
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Set the system time in Bun's test runner
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
Bun's test runner supports setting the system time programmatically with the `setSystemTime` function.
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
import { test, expect, setSystemTime } from "bun:test";
|
|
9
|
+
|
|
10
|
+
test("party like it's 1999", () => {
|
|
11
|
+
const date = new Date("1999-01-01T00:00:00.000Z");
|
|
12
|
+
setSystemTime(date); // it's now January 1, 1999
|
|
13
|
+
|
|
14
|
+
const now = new Date();
|
|
15
|
+
expect(now.getFullYear()).toBe(1999);
|
|
16
|
+
expect(now.getMonth()).toBe(0);
|
|
17
|
+
expect(now.getDate()).toBe(1);
|
|
18
|
+
});
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
---
|
|
22
|
+
|
|
23
|
+
The `setSystemTime` function is commonly used on conjunction with [Lifecycle Hooks](https://bun.sh/docs/test/lifecycle) to configure a testing environment with a deterministic "fake clock".
|
|
24
|
+
|
|
25
|
+
```ts
|
|
26
|
+
import { test, expect, beforeAll, setSystemTime } from "bun:test";
|
|
27
|
+
|
|
28
|
+
beforeAll(() => {
|
|
29
|
+
const date = new Date("1999-01-01T00:00:00.000Z");
|
|
30
|
+
setSystemTime(date); // it's now January 1, 1999
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
// tests...
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
---
|
|
37
|
+
|
|
38
|
+
To reset the system clock to the actual time, call `setSystemTime` with no arguments.
|
|
39
|
+
|
|
40
|
+
```ts
|
|
41
|
+
import { test, expect, beforeAll, setSystemTime } from "bun:test";
|
|
42
|
+
|
|
43
|
+
setSystemTime(); // reset to actual time
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
---
|
|
47
|
+
|
|
48
|
+
See [Docs > Test Runner > Date and time](https://bun.sh/docs/test/time) for complete documentation on mocking with the Bun test runner.
|