bun-types 1.1.37-canary.20241124T140524 → 1.1.37-canary.20241125T140601
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,128 @@
|
|
|
1
|
+
Some Web APIs aren't relevant in the context of a server-first runtime like Bun, such as the [DOM API](https://developer.mozilla.org/en-US/docs/Web/API/HTML_DOM_API#html_dom_api_interfaces) or [History API](https://developer.mozilla.org/en-US/docs/Web/API/History_API). Many others, though, are broadly useful outside of the browser context; when possible, Bun implements these Web-standard APIs instead of introducing new APIs.
|
|
2
|
+
|
|
3
|
+
The following Web APIs are partially or completely supported.
|
|
4
|
+
|
|
5
|
+
{% table %}
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
- HTTP
|
|
10
|
+
- [`fetch`](https://developer.mozilla.org/en-US/docs/Web/API/fetch)
|
|
11
|
+
[`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response)
|
|
12
|
+
[`Request`](https://developer.mozilla.org/en-US/docs/Web/API/Request)
|
|
13
|
+
[`Headers`](https://developer.mozilla.org/en-US/docs/Web/API/Headers)
|
|
14
|
+
[`AbortController`](https://developer.mozilla.org/en-US/docs/Web/API/AbortController)
|
|
15
|
+
[`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal)
|
|
16
|
+
|
|
17
|
+
---
|
|
18
|
+
|
|
19
|
+
- URLs
|
|
20
|
+
- [`URL`](https://developer.mozilla.org/en-US/docs/Web/API/URL)
|
|
21
|
+
[`URLSearchParams`](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams)
|
|
22
|
+
|
|
23
|
+
---
|
|
24
|
+
|
|
25
|
+
- Web Workers
|
|
26
|
+
- [`Worker`](https://developer.mozilla.org/en-US/docs/Web/API/Worker)
|
|
27
|
+
[`self.postMessage`](https://developer.mozilla.org/en-US/docs/Web/API/DedicatedWorkerGlobalScope/postMessage)
|
|
28
|
+
[`structuredClone`](https://developer.mozilla.org/en-US/docs/Web/API/structuredClone)
|
|
29
|
+
[`MessagePort`](https://developer.mozilla.org/en-US/docs/Web/API/MessagePort)
|
|
30
|
+
[`MessageChannel`](https://developer.mozilla.org/en-US/docs/Web/API/MessageChannel)
|
|
31
|
+
[`BroadcastChannel`](https://developer.mozilla.org/en-US/docs/Web/API/BroadcastChannel).
|
|
32
|
+
|
|
33
|
+
---
|
|
34
|
+
|
|
35
|
+
- Streams
|
|
36
|
+
- [`ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream)
|
|
37
|
+
[`WritableStream`](https://developer.mozilla.org/en-US/docs/Web/API/WritableStream)
|
|
38
|
+
[`TransformStream`](https://developer.mozilla.org/en-US/docs/Web/API/TransformStream)
|
|
39
|
+
[`ByteLengthQueuingStrategy`](https://developer.mozilla.org/en-US/docs/Web/API/ByteLengthQueuingStrategy)
|
|
40
|
+
[`CountQueuingStrategy`](https://developer.mozilla.org/en-US/docs/Web/API/CountQueuingStrategy) and associated classes
|
|
41
|
+
|
|
42
|
+
---
|
|
43
|
+
|
|
44
|
+
- Blob
|
|
45
|
+
- [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob)
|
|
46
|
+
|
|
47
|
+
---
|
|
48
|
+
|
|
49
|
+
- WebSockets
|
|
50
|
+
- [`WebSocket`](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket)
|
|
51
|
+
|
|
52
|
+
---
|
|
53
|
+
|
|
54
|
+
- Encoding and decoding
|
|
55
|
+
- [`atob`](https://developer.mozilla.org/en-US/docs/Web/API/atob)
|
|
56
|
+
[`btoa`](https://developer.mozilla.org/en-US/docs/Web/API/btoa)
|
|
57
|
+
[`TextEncoder`](https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder)
|
|
58
|
+
[`TextDecoder`](https://developer.mozilla.org/en-US/docs/Web/API/TextDecoder)
|
|
59
|
+
|
|
60
|
+
---
|
|
61
|
+
|
|
62
|
+
- JSON
|
|
63
|
+
- [`JSON`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON)
|
|
64
|
+
|
|
65
|
+
---
|
|
66
|
+
|
|
67
|
+
- Timeouts
|
|
68
|
+
- [`setTimeout`](https://developer.mozilla.org/en-US/docs/Web/API/setTimeout)
|
|
69
|
+
[`clearTimeout`](https://developer.mozilla.org/en-US/docs/Web/API/clearTimeout)
|
|
70
|
+
|
|
71
|
+
---
|
|
72
|
+
|
|
73
|
+
- Intervals
|
|
74
|
+
- [`setInterval`](https://developer.mozilla.org/en-US/docs/Web/API/setInterval)
|
|
75
|
+
[`clearInterval`](https://developer.mozilla.org/en-US/docs/Web/API/clearInterval)
|
|
76
|
+
|
|
77
|
+
---
|
|
78
|
+
|
|
79
|
+
- Crypto
|
|
80
|
+
- [`crypto`](https://developer.mozilla.org/en-US/docs/Web/API/Crypto)
|
|
81
|
+
[`SubtleCrypto`](https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto)
|
|
82
|
+
[`CryptoKey`](https://developer.mozilla.org/en-US/docs/Web/API/CryptoKey)
|
|
83
|
+
|
|
84
|
+
---
|
|
85
|
+
|
|
86
|
+
- Debugging
|
|
87
|
+
|
|
88
|
+
- [`console`](https://developer.mozilla.org/en-US/docs/Web/API/console)
|
|
89
|
+
[`performance`](https://developer.mozilla.org/en-US/docs/Web/API/Performance)
|
|
90
|
+
|
|
91
|
+
---
|
|
92
|
+
|
|
93
|
+
- Microtasks
|
|
94
|
+
- [`queueMicrotask`](https://developer.mozilla.org/en-US/docs/Web/API/queueMicrotask)
|
|
95
|
+
|
|
96
|
+
---
|
|
97
|
+
|
|
98
|
+
- Errors
|
|
99
|
+
- [`reportError`](https://developer.mozilla.org/en-US/docs/Web/API/reportError)
|
|
100
|
+
|
|
101
|
+
---
|
|
102
|
+
|
|
103
|
+
- User interaction
|
|
104
|
+
- [`alert`](https://developer.mozilla.org/en-US/docs/Web/API/Window/alert)
|
|
105
|
+
[`confirm`](https://developer.mozilla.org/en-US/docs/Web/API/Window/confirm)
|
|
106
|
+
[`prompt`](https://developer.mozilla.org/en-US/docs/Web/API/Window/prompt) (intended for interactive CLIs)
|
|
107
|
+
|
|
108
|
+
<!-- - Blocking. Prints the alert message to terminal and awaits `[ENTER]` before proceeding. -->
|
|
109
|
+
<!-- - Blocking. Prints confirmation message and awaits `[y/N]` input from user. Returns `true` if user entered `y` or `Y`, `false` otherwise.
|
|
110
|
+
- Blocking. Prints prompt message and awaits user input. Returns the user input as a string. -->
|
|
111
|
+
|
|
112
|
+
---
|
|
113
|
+
|
|
114
|
+
- Realms
|
|
115
|
+
- [`ShadowRealm`](https://github.com/tc39/proposal-shadowrealm)
|
|
116
|
+
|
|
117
|
+
---
|
|
118
|
+
|
|
119
|
+
- Events
|
|
120
|
+
- [`EventTarget`](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget)
|
|
121
|
+
[`Event`](https://developer.mozilla.org/en-US/docs/Web/API/Event)
|
|
122
|
+
[`ErrorEvent`](https://developer.mozilla.org/en-US/docs/Web/API/ErrorEvent)
|
|
123
|
+
[`CloseEvent`](https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent)
|
|
124
|
+
[`MessageEvent`](https://developer.mozilla.org/en-US/docs/Web/API/MessageEvent)
|
|
125
|
+
|
|
126
|
+
---
|
|
127
|
+
|
|
128
|
+
{% /table %}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
Bun's test runner now 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.
|
|
2
|
+
|
|
3
|
+
## Enabling coverage
|
|
4
|
+
|
|
5
|
+
`bun:test` supports seeing which lines of code are covered by tests. To use this feature, pass `--coverage` to the CLI. It will print out a coverage report to the console:
|
|
6
|
+
|
|
7
|
+
```js
|
|
8
|
+
$ bun test --coverage
|
|
9
|
+
-------------|---------|---------|-------------------
|
|
10
|
+
File | % Funcs | % Lines | Uncovered Line #s
|
|
11
|
+
-------------|---------|---------|-------------------
|
|
12
|
+
All files | 38.89 | 42.11 |
|
|
13
|
+
index-0.ts | 33.33 | 36.84 | 10-15,19-24
|
|
14
|
+
index-1.ts | 33.33 | 36.84 | 10-15,19-24
|
|
15
|
+
index-10.ts | 33.33 | 36.84 | 10-15,19-24
|
|
16
|
+
index-2.ts | 33.33 | 36.84 | 10-15,19-24
|
|
17
|
+
index-3.ts | 33.33 | 36.84 | 10-15,19-24
|
|
18
|
+
index-4.ts | 33.33 | 36.84 | 10-15,19-24
|
|
19
|
+
index-5.ts | 33.33 | 36.84 | 10-15,19-24
|
|
20
|
+
index-6.ts | 33.33 | 36.84 | 10-15,19-24
|
|
21
|
+
index-7.ts | 33.33 | 36.84 | 10-15,19-24
|
|
22
|
+
index-8.ts | 33.33 | 36.84 | 10-15,19-24
|
|
23
|
+
index-9.ts | 33.33 | 36.84 | 10-15,19-24
|
|
24
|
+
index.ts | 100.00 | 100.00 |
|
|
25
|
+
-------------|---------|---------|-------------------
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
To always enable coverage reporting by default, add the following line to your `bunfig.toml`:
|
|
29
|
+
|
|
30
|
+
```toml
|
|
31
|
+
[test]
|
|
32
|
+
|
|
33
|
+
# always enable coverage
|
|
34
|
+
coverage = true
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
By default coverage reports will _include_ test files and _exclude_ sourcemaps. This is usually what you want, but it can be configured otherwise in `bunfig.toml`.
|
|
38
|
+
|
|
39
|
+
```toml
|
|
40
|
+
[test]
|
|
41
|
+
coverageSkipTestFiles = true # default false
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### Coverage thresholds
|
|
45
|
+
|
|
46
|
+
It is possible to specify a coverage threshold in `bunfig.toml`. If your test suite does not meet or exceed this threshold, `bun test` will exit with a non-zero exit code to indicate the failure.
|
|
47
|
+
|
|
48
|
+
```toml
|
|
49
|
+
[test]
|
|
50
|
+
|
|
51
|
+
# to require 90% line-level and function-level coverage
|
|
52
|
+
coverageThreshold = 0.9
|
|
53
|
+
|
|
54
|
+
# to set different thresholds for lines and functions
|
|
55
|
+
coverageThreshold = { lines = 0.9, functions = 0.9 }
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### Sourcemaps
|
|
59
|
+
|
|
60
|
+
Internally, Bun transpiles all files by default, so Bun automatically generates an internal [source map](https://web.dev/source-maps/) that maps lines of your original source code onto Bun's internal representation. If for any reason you want to disable this, set `test.coverageIgnoreSourcemaps` to `true`; this will rarely be desirable outside of advanced use cases.
|
|
61
|
+
|
|
62
|
+
```toml
|
|
63
|
+
[test]
|
|
64
|
+
coverageIgnoreSourcemaps = true # default false
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### Coverage reporters
|
|
68
|
+
|
|
69
|
+
By default, coverage reports will be printed to the console.
|
|
70
|
+
|
|
71
|
+
For persistent code coverage reports in CI environments and for other tools, you can pass a `--coverage-reporter=lcov` CLI option or `coverageReporter` option in `bunfig.toml`.
|
|
72
|
+
|
|
73
|
+
```toml
|
|
74
|
+
[test]
|
|
75
|
+
coverageReporter = ["text", "lcov"] # default ["text"]
|
|
76
|
+
coverageDir = "path/to/somewhere" # default "coverage"
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
| Reporter | Description |
|
|
80
|
+
| -------- | --------------------------------------------------------------------------- |
|
|
81
|
+
| `text` | Prints a text summary of the coverage to the console. |
|
|
82
|
+
| `lcov` | Save coverage in [lcov](https://github.com/linux-test-project/lcov) format. |
|
|
83
|
+
|
|
84
|
+
#### lcov coverage reporter
|
|
85
|
+
|
|
86
|
+
To generate an lcov report, you can use the `lcov` reporter. This will generate an `lcov.info` file in the `coverage` directory.
|
|
87
|
+
|
|
88
|
+
```toml
|
|
89
|
+
[test]
|
|
90
|
+
coverageReporter = "lcov"
|
|
91
|
+
```
|
package/docs/test/dom.md
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
Bun's test runner plays well with existing component and DOM testing libraries, including React Testing Library and [`happy-dom`](https://github.com/capricorn86/happy-dom).
|
|
2
|
+
|
|
3
|
+
## `happy-dom`
|
|
4
|
+
|
|
5
|
+
For writing headless tests for your frontend code and components, we recommend [`happy-dom`](https://github.com/capricorn86/happy-dom). Happy DOM implements a complete set of HTML and DOM APIs in plain JavaScript, making it possible to simulate a browser environment with high fidelity.
|
|
6
|
+
|
|
7
|
+
To get started install the `@happy-dom/global-registrator` package as a dev dependency.
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
$ bun add -d @happy-dom/global-registrator
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
We'll be using Bun's _preload_ functionality to register the `happy-dom` globals before running our tests. This step will make browser APIs like `document` available in the global scope. Create a file called `happydom.ts` in the root of your project and add the following code:
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { GlobalRegistrator } from "@happy-dom/global-registrator";
|
|
17
|
+
|
|
18
|
+
GlobalRegistrator.register();
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
To preload this file before `bun test`, open or create a `bunfig.toml` file and add the following lines.
|
|
22
|
+
|
|
23
|
+
```toml
|
|
24
|
+
[test]
|
|
25
|
+
preload = "./happydom.ts"
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
This will execute `happydom.ts` when you run `bun test`. Now you can write tests that use browser APIs like `document` and `window`.
|
|
29
|
+
|
|
30
|
+
```ts#dom.test.ts
|
|
31
|
+
import {test, expect} from 'bun:test';
|
|
32
|
+
|
|
33
|
+
test('dom test', () => {
|
|
34
|
+
document.body.innerHTML = `<button>My button</button>`;
|
|
35
|
+
const button = document.querySelector('button');
|
|
36
|
+
expect(button?.innerText).toEqual('My button');
|
|
37
|
+
});
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Depending on your `tsconfig.json` setup, you may see a `"Cannot find name 'document'"` type error in the code above. To "inject" the types for `document` and other browser APIs, add the following [triple-slash directive](https://www.typescriptlang.org/docs/handbook/triple-slash-directives.html) to the top of any test file.
|
|
41
|
+
|
|
42
|
+
```ts-diff#dom.test.ts
|
|
43
|
+
+ /// <reference lib="dom" />
|
|
44
|
+
|
|
45
|
+
import {test, expect} from 'bun:test';
|
|
46
|
+
|
|
47
|
+
test('dom test', () => {
|
|
48
|
+
document.body.innerHTML = `<button>My button</button>`;
|
|
49
|
+
const button = document.querySelector('button');
|
|
50
|
+
expect(button?.innerText).toEqual('My button');
|
|
51
|
+
});
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Let's run this test with `bun test`:
|
|
55
|
+
|
|
56
|
+
```bash
|
|
57
|
+
$ bun test
|
|
58
|
+
bun test v1.x
|
|
59
|
+
|
|
60
|
+
dom.test.ts:
|
|
61
|
+
✓ dom test [0.82ms]
|
|
62
|
+
|
|
63
|
+
1 pass
|
|
64
|
+
0 fail
|
|
65
|
+
1 expect() calls
|
|
66
|
+
Ran 1 tests across 1 files. 1 total [125.00ms]
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
<!-- ## React Testing Library
|
|
70
|
+
|
|
71
|
+
Once you've set up `happy-dom` as described above, you can use it with React Testing Library. To get started, install the `@testing-library/react` package as a dev dependency.
|
|
72
|
+
|
|
73
|
+
```bash
|
|
74
|
+
$ bun add -d @testing-library/react
|
|
75
|
+
``` -->
|
package/docs/test/hot.md
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
To automatically re-run tests when files change, use the `--watch` flag:
|
|
2
|
+
|
|
3
|
+
```sh
|
|
4
|
+
$ bun test --watch
|
|
5
|
+
```
|
|
6
|
+
|
|
7
|
+
Bun will watch for changes to any files imported in a test file, and re-run tests when a change is detected.
|
|
8
|
+
|
|
9
|
+
It's fast.
|
|
10
|
+
|
|
11
|
+
{% raw %}
|
|
12
|
+
|
|
13
|
+
<blockquote class="twitter-tweet"><p lang="en" dir="ltr">"bun test --watch url" in a large folder with multiple files that start with "url" <a href="https://t.co/aZV9BP4eFu">pic.twitter.com/aZV9BP4eFu</a></p>— Jarred Sumner (@jarredsumner) <a href="https://twitter.com/jarredsumner/status/1640890850535436288?ref_src=twsrc%5Etfw">March 29, 2023</a></blockquote> <script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
|
|
14
|
+
|
|
15
|
+
{% /raw %}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
The test runner supports the following lifecycle hooks. This is useful for loading test fixtures, mocking data, and configuring the test environment.
|
|
2
|
+
|
|
3
|
+
| Hook | Description |
|
|
4
|
+
| ------------ | --------------------------- |
|
|
5
|
+
| `beforeAll` | Runs once before all tests. |
|
|
6
|
+
| `beforeEach` | Runs before each test. |
|
|
7
|
+
| `afterEach` | Runs after each test. |
|
|
8
|
+
| `afterAll` | Runs once after all tests. |
|
|
9
|
+
|
|
10
|
+
Perform per-test setup and teardown logic with `beforeEach` and `afterEach`.
|
|
11
|
+
|
|
12
|
+
```ts
|
|
13
|
+
import { beforeEach, afterEach } from "bun:test";
|
|
14
|
+
|
|
15
|
+
beforeEach(() => {
|
|
16
|
+
console.log("running test.");
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
afterEach(() => {
|
|
20
|
+
console.log("done with test.");
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
// tests...
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Perform per-scope setup and teardown logic with `beforeAll` and `afterAll`. The _scope_ is determined by where the hook is defined.
|
|
27
|
+
|
|
28
|
+
To scope the hooks to a particular `describe` block:
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
import { describe, beforeAll } from "bun:test";
|
|
32
|
+
|
|
33
|
+
describe("test group", () => {
|
|
34
|
+
beforeAll(() => {
|
|
35
|
+
// setup
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
// tests...
|
|
39
|
+
});
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
To scope the hooks to a test file:
|
|
43
|
+
|
|
44
|
+
```ts
|
|
45
|
+
import { describe, beforeAll } from "bun:test";
|
|
46
|
+
|
|
47
|
+
beforeAll(() => {
|
|
48
|
+
// setup
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
describe("test group", () => {
|
|
52
|
+
// tests...
|
|
53
|
+
});
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
To scope the hooks to an entire multi-file test run, define the hooks in a separate file.
|
|
57
|
+
|
|
58
|
+
```ts#setup.ts
|
|
59
|
+
import { beforeAll, afterAll } from "bun:test";
|
|
60
|
+
|
|
61
|
+
beforeAll(() => {
|
|
62
|
+
// global setup
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
afterAll(() => {
|
|
66
|
+
// global teardown
|
|
67
|
+
});
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
Then use `--preload` to run the setup script before any test files.
|
|
71
|
+
|
|
72
|
+
```ts
|
|
73
|
+
$ bun test --preload ./setup.ts
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
To avoid typing `--preload` every time you run tests, it can be added to your `bunfig.toml`:
|
|
77
|
+
|
|
78
|
+
```toml
|
|
79
|
+
[test]
|
|
80
|
+
preload = ["./setup.ts"]
|
|
81
|
+
```
|
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
Create mocks with the `mock` function.
|
|
2
|
+
|
|
3
|
+
```ts
|
|
4
|
+
import { test, expect, mock } from "bun:test";
|
|
5
|
+
const random = mock(() => Math.random());
|
|
6
|
+
|
|
7
|
+
test("random", async () => {
|
|
8
|
+
const val = random();
|
|
9
|
+
expect(val).toBeGreaterThan(0);
|
|
10
|
+
expect(random).toHaveBeenCalled();
|
|
11
|
+
expect(random).toHaveBeenCalledTimes(1);
|
|
12
|
+
});
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
{% callout %}
|
|
16
|
+
Alternatively, you can use the `jest.fn()` function, as in Jest. It behaves identically.
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
import { test, expect, jest } from "bun:test";
|
|
20
|
+
const random = jest.fn(() => Math.random());
|
|
21
|
+
|
|
22
|
+
test("random", async () => {
|
|
23
|
+
const val = random();
|
|
24
|
+
expect(val).toBeGreaterThan(0);
|
|
25
|
+
expect(random).toHaveBeenCalled();
|
|
26
|
+
expect(random).toHaveBeenCalledTimes(1);
|
|
27
|
+
});
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
{% /callout %}
|
|
31
|
+
|
|
32
|
+
The result of `mock()` is a new function that's been decorated with some additional properties.
|
|
33
|
+
|
|
34
|
+
```ts
|
|
35
|
+
import { mock } from "bun:test";
|
|
36
|
+
const random = mock((multiplier: number) => multiplier * Math.random());
|
|
37
|
+
|
|
38
|
+
random(2);
|
|
39
|
+
random(10);
|
|
40
|
+
|
|
41
|
+
random.mock.calls;
|
|
42
|
+
// [[ 2 ], [ 10 ]]
|
|
43
|
+
|
|
44
|
+
random.mock.results;
|
|
45
|
+
// [
|
|
46
|
+
// { type: "return", value: 0.6533907460954099 },
|
|
47
|
+
// { type: "return", value: 0.6452713933037312 }
|
|
48
|
+
// ]
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
The following properties and methods are implemented on mock functions.
|
|
52
|
+
|
|
53
|
+
- [x] [mockFn.getMockName()](https://jestjs.io/docs/mock-function-api#mockfngetmockname)
|
|
54
|
+
- [x] [mockFn.mock.calls](https://jestjs.io/docs/mock-function-api#mockfnmockcalls)
|
|
55
|
+
- [x] [mockFn.mock.results](https://jestjs.io/docs/mock-function-api#mockfnmockresults)
|
|
56
|
+
- [x] [mockFn.mock.instances](https://jestjs.io/docs/mock-function-api#mockfnmockinstances)
|
|
57
|
+
- [x] [mockFn.mock.contexts](https://jestjs.io/docs/mock-function-api#mockfnmockcontexts)
|
|
58
|
+
- [x] [mockFn.mock.lastCall](https://jestjs.io/docs/mock-function-api#mockfnmocklastcall)
|
|
59
|
+
- [x] [mockFn.mockClear()](https://jestjs.io/docs/mock-function-api#mockfnmockclear)
|
|
60
|
+
- [x] [mockFn.mockReset()](https://jestjs.io/docs/mock-function-api#mockfnmockreset)
|
|
61
|
+
- [x] [mockFn.mockRestore()](https://jestjs.io/docs/mock-function-api#mockfnmockrestore)
|
|
62
|
+
- [x] [mockFn.mockImplementation(fn)](https://jestjs.io/docs/mock-function-api#mockfnmockimplementationfn)
|
|
63
|
+
- [x] [mockFn.mockImplementationOnce(fn)](https://jestjs.io/docs/mock-function-api#mockfnmockimplementationoncefn)
|
|
64
|
+
- [x] [mockFn.mockName(name)](https://jestjs.io/docs/mock-function-api#mockfnmocknamename)
|
|
65
|
+
- [x] [mockFn.mockReturnThis()](https://jestjs.io/docs/mock-function-api#mockfnmockreturnthis)
|
|
66
|
+
- [x] [mockFn.mockReturnValue(value)](https://jestjs.io/docs/mock-function-api#mockfnmockreturnvaluevalue)
|
|
67
|
+
- [x] [mockFn.mockReturnValueOnce(value)](https://jestjs.io/docs/mock-function-api#mockfnmockreturnvalueoncevalue)
|
|
68
|
+
- [x] [mockFn.mockResolvedValue(value)](https://jestjs.io/docs/mock-function-api#mockfnmockresolvedvaluevalue)
|
|
69
|
+
- [x] [mockFn.mockResolvedValueOnce(value)](https://jestjs.io/docs/mock-function-api#mockfnmockresolvedvalueoncevalue)
|
|
70
|
+
- [x] [mockFn.mockRejectedValue(value)](https://jestjs.io/docs/mock-function-api#mockfnmockrejectedvaluevalue)
|
|
71
|
+
- [x] [mockFn.mockRejectedValueOnce(value)](https://jestjs.io/docs/mock-function-api#mockfnmockrejectedvalueoncevalue)
|
|
72
|
+
- [x] [mockFn.withImplementation(fn, callback)](https://jestjs.io/docs/mock-function-api#mockfnwithimplementationfn-callback)
|
|
73
|
+
|
|
74
|
+
## `.spyOn()`
|
|
75
|
+
|
|
76
|
+
It's possible to track calls to a function without replacing it with a mock. Use `spyOn()` to create a spy; these spies can be passed to `.toHaveBeenCalled()` and `.toHaveBeenCalledTimes()`.
|
|
77
|
+
|
|
78
|
+
```ts
|
|
79
|
+
import { test, expect, spyOn } from "bun:test";
|
|
80
|
+
|
|
81
|
+
const ringo = {
|
|
82
|
+
name: "Ringo",
|
|
83
|
+
sayHi() {
|
|
84
|
+
console.log(`Hello I'm ${this.name}`);
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
const spy = spyOn(ringo, "sayHi");
|
|
89
|
+
|
|
90
|
+
test("spyon", () => {
|
|
91
|
+
expect(spy).toHaveBeenCalledTimes(0);
|
|
92
|
+
ringo.sayHi();
|
|
93
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
94
|
+
});
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
## Module mocks with `mock.module()`
|
|
98
|
+
|
|
99
|
+
Module mocking lets you override the behavior of a module. Use `mock.module(path: string, callback: () => Object)` to mock a module.
|
|
100
|
+
|
|
101
|
+
```ts
|
|
102
|
+
import { test, expect, mock } from "bun:test";
|
|
103
|
+
|
|
104
|
+
mock.module("./module", () => {
|
|
105
|
+
return {
|
|
106
|
+
foo: "bar",
|
|
107
|
+
};
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
test("mock.module", async () => {
|
|
111
|
+
const esm = await import("./module");
|
|
112
|
+
expect(esm.foo).toBe("bar");
|
|
113
|
+
|
|
114
|
+
const cjs = require("./module");
|
|
115
|
+
expect(cjs.foo).toBe("bar");
|
|
116
|
+
});
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
Like the rest of Bun, module mocks support both `import` and `require`.
|
|
120
|
+
|
|
121
|
+
### Overriding already imported modules
|
|
122
|
+
|
|
123
|
+
If you need to override a module that's already been imported, there's nothing special you need to do. Just call `mock.module()` and the module will be overridden.
|
|
124
|
+
|
|
125
|
+
```ts
|
|
126
|
+
import { test, expect, mock } from "bun:test";
|
|
127
|
+
|
|
128
|
+
// The module we're going to mock is here:
|
|
129
|
+
import { foo } from "./module";
|
|
130
|
+
|
|
131
|
+
test("mock.module", async () => {
|
|
132
|
+
const cjs = require("./module");
|
|
133
|
+
expect(foo).toBe("bar");
|
|
134
|
+
expect(cjs.foo).toBe("bar");
|
|
135
|
+
|
|
136
|
+
// We update it here:
|
|
137
|
+
mock.module("./module", () => {
|
|
138
|
+
return {
|
|
139
|
+
foo: "baz",
|
|
140
|
+
};
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
// And the live bindings are updated.
|
|
144
|
+
expect(foo).toBe("baz");
|
|
145
|
+
|
|
146
|
+
// The module is also updated for CJS.
|
|
147
|
+
expect(cjs.foo).toBe("baz");
|
|
148
|
+
});
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
### Hoisting & preloading
|
|
152
|
+
|
|
153
|
+
If you need to ensure a module is mocked before it's imported, you should use `--preload` to load your mocks before your tests run.
|
|
154
|
+
|
|
155
|
+
```ts
|
|
156
|
+
// my-preload.ts
|
|
157
|
+
import { mock } from "bun:test";
|
|
158
|
+
|
|
159
|
+
mock.module("./module", () => {
|
|
160
|
+
return {
|
|
161
|
+
foo: "bar",
|
|
162
|
+
};
|
|
163
|
+
});
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
```sh
|
|
167
|
+
bun test --preload ./my-preload
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
To make your life easier, you can put `preload` in your `bunfig.toml`:
|
|
171
|
+
|
|
172
|
+
```toml
|
|
173
|
+
|
|
174
|
+
[test]
|
|
175
|
+
# Load these modules before running tests.
|
|
176
|
+
preload = ["./my-preload"]
|
|
177
|
+
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
#### What happens if I mock a module that's already been imported?
|
|
181
|
+
|
|
182
|
+
If you mock a module that's already been imported, the module will be updated in the module cache. This means that any modules that import the module will get the mocked version, BUT the original module will still have been evaluated. That means that any side effects from the original module will still have happened.
|
|
183
|
+
|
|
184
|
+
If you want to prevent the original module from being evaluated, you should use `--preload` to load your mocks before your tests run.
|
|
185
|
+
|
|
186
|
+
### `__mocks__` directory and auto-mocking
|
|
187
|
+
|
|
188
|
+
Auto-mocking is not supported yet. If this is blocking you from switching to Bun, please file an issue.
|
|
189
|
+
|
|
190
|
+
### Implementation details
|
|
191
|
+
|
|
192
|
+
Module mocks have different implementations for ESM and CommonJS modules. For ES Modules, we've added patches to JavaScriptCore that allow Bun to override export values at runtime and update live bindings recursively.
|
|
193
|
+
|
|
194
|
+
As of Bun v1.0.19, Bun automatically resolves the `specifier` argument to `mock.module()` as though you did an `import`. If it successfully resolves, then the resolved specifier string is used as the key in the module cache. This means that you can use relative paths, absolute paths, and even module names. If the `specifier` doesn't resolve, then the original `specifier` is used as the key in the module cache.
|
|
195
|
+
|
|
196
|
+
After resolution, the mocked module is stored in the ES Module registry **and** the CommonJS require cache. This means that you can use `import` and `require` interchangeably for mocked modules.
|
|
197
|
+
|
|
198
|
+
The callback function is called lazily, only if the module is imported or required. This means that you can use `mock.module()` to mock modules that don't exist yet, and it means that you can use `mock.module()` to mock modules that are imported by other modules.
|
|
199
|
+
|
|
200
|
+
## Restore all function mocks to their original values with `mock.restore()`
|
|
201
|
+
|
|
202
|
+
Instead of manually restoring each mock individually with `mockFn.mockRestore()`, restore all mocks with one command by calling `mock.restore()`. Doing so does not reset the value of modules overridden with `mock.module()`.
|
|
203
|
+
|
|
204
|
+
Using `mock.restore()` can reduce the amount of code in your tests by adding it to `afterEach` blocks in each test file or even in your [test preload code](https://bun.sh/docs/runtime/bunfig#test-preload).
|
|
205
|
+
|
|
206
|
+
```ts
|
|
207
|
+
import { expect, mock, spyOn, test } from "bun:test";
|
|
208
|
+
|
|
209
|
+
import * as fooModule from './foo.ts';
|
|
210
|
+
import * as barModule from './bar.ts';
|
|
211
|
+
import * as bazModule from './baz.ts';
|
|
212
|
+
|
|
213
|
+
test('foo, bar, baz', () => {
|
|
214
|
+
const fooSpy = spyOn(fooModule, 'foo');
|
|
215
|
+
const barSpy = spyOn(barModule, 'bar');
|
|
216
|
+
const bazSpy = spyOn(bazModule, 'baz');
|
|
217
|
+
|
|
218
|
+
expect(fooSpy).toBe('foo');
|
|
219
|
+
expect(barSpy).toBe('bar');
|
|
220
|
+
expect(bazSpy).toBe('baz');
|
|
221
|
+
|
|
222
|
+
fooSpy.mockImplementation(() => 42);
|
|
223
|
+
barSpy.mockImplementation(() => 43);
|
|
224
|
+
bazSpy.mockImplementation(() => 44);
|
|
225
|
+
|
|
226
|
+
expect(fooSpy).toBe(42);
|
|
227
|
+
expect(barSpy).toBe(43);
|
|
228
|
+
expect(bazSpy).toBe(44);
|
|
229
|
+
|
|
230
|
+
mock.restore();
|
|
231
|
+
|
|
232
|
+
expect(fooSpy).toBe('foo');
|
|
233
|
+
expect(barSpy).toBe('bar');
|
|
234
|
+
expect(bazSpy).toBe('baz');
|
|
235
|
+
});
|
|
236
|
+
```
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
Snapshot tests are written using the `.toMatchSnapshot()` matcher:
|
|
2
|
+
|
|
3
|
+
```ts
|
|
4
|
+
import { test, expect } from "bun:test";
|
|
5
|
+
|
|
6
|
+
test("snap", () => {
|
|
7
|
+
expect("foo").toMatchSnapshot();
|
|
8
|
+
});
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
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:
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
$ bun test --update-snapshots
|
|
15
|
+
```
|