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.
Files changed (272) hide show
  1. package/docs/api/binary-data.md +1028 -0
  2. package/docs/api/cc.md +197 -0
  3. package/docs/api/color.md +262 -0
  4. package/docs/api/console.md +38 -0
  5. package/docs/api/dns.md +113 -0
  6. package/docs/api/fetch.md +308 -0
  7. package/docs/api/ffi.md +536 -0
  8. package/docs/api/file-io.md +358 -0
  9. package/docs/api/file-system-router.md +112 -0
  10. package/docs/api/file.md +19 -0
  11. package/docs/api/glob.md +157 -0
  12. package/docs/api/globals.md +387 -0
  13. package/docs/api/hashing.md +314 -0
  14. package/docs/api/html-rewriter.md +31 -0
  15. package/docs/api/http.md +661 -0
  16. package/docs/api/import-meta.md +69 -0
  17. package/docs/api/node-api.md +16 -0
  18. package/docs/api/semver.md +52 -0
  19. package/docs/api/spawn.md +441 -0
  20. package/docs/api/sqlite.md +692 -0
  21. package/docs/api/streams.md +214 -0
  22. package/docs/api/tcp.md +221 -0
  23. package/docs/api/test.md +1 -0
  24. package/docs/api/transpiler.md +274 -0
  25. package/docs/api/udp.md +125 -0
  26. package/docs/api/utils.md +773 -0
  27. package/docs/api/websockets.md +547 -0
  28. package/docs/api/workers.md +230 -0
  29. package/docs/benchmarks.md +120 -0
  30. package/docs/bun-flavored-toml.md +42 -0
  31. package/docs/bundler/executables.md +291 -0
  32. package/docs/bundler/index.md +1543 -0
  33. package/docs/bundler/intro.md +75 -0
  34. package/docs/bundler/loaders.md +288 -0
  35. package/docs/bundler/macros.md +327 -0
  36. package/docs/bundler/plugins.md +30 -0
  37. package/docs/bundler/vs-esbuild.md +1127 -0
  38. package/docs/cli/add.md +163 -0
  39. package/docs/cli/bun-completions.md +3 -0
  40. package/docs/cli/bun-create.md +254 -0
  41. package/docs/cli/bun-dev.md +21 -0
  42. package/docs/cli/bun-install.md +255 -0
  43. package/docs/cli/bun-upgrade.md +39 -0
  44. package/docs/cli/bunx.md +80 -0
  45. package/docs/cli/filter.md +57 -0
  46. package/docs/cli/init.md +40 -0
  47. package/docs/cli/install.md +205 -0
  48. package/docs/cli/link.md +38 -0
  49. package/docs/cli/outdated.md +61 -0
  50. package/docs/cli/patch-commit.md +9 -0
  51. package/docs/cli/pm.md +150 -0
  52. package/docs/cli/publish.md +107 -0
  53. package/docs/cli/remove.md +5 -0
  54. package/docs/cli/run.md +196 -0
  55. package/docs/cli/test.md +247 -0
  56. package/docs/cli/unlink.md +7 -0
  57. package/docs/cli/update.md +34 -0
  58. package/docs/contributing/upgrading-webkit.md +57 -0
  59. package/docs/ecosystem/elysia.md +24 -0
  60. package/docs/ecosystem/express.md +37 -0
  61. package/docs/ecosystem/hono.md +18 -0
  62. package/docs/ecosystem/react.md +65 -0
  63. package/docs/ecosystem/stric.md +37 -0
  64. package/docs/guides/binary/arraybuffer-to-array.md +27 -0
  65. package/docs/guides/binary/arraybuffer-to-blob.md +24 -0
  66. package/docs/guides/binary/arraybuffer-to-buffer.md +25 -0
  67. package/docs/guides/binary/arraybuffer-to-string.md +15 -0
  68. package/docs/guides/binary/arraybuffer-to-typedarray.md +39 -0
  69. package/docs/guides/binary/blob-to-arraybuffer.md +14 -0
  70. package/docs/guides/binary/blob-to-dataview.md +14 -0
  71. package/docs/guides/binary/blob-to-stream.md +14 -0
  72. package/docs/guides/binary/blob-to-string.md +15 -0
  73. package/docs/guides/binary/blob-to-typedarray.md +14 -0
  74. package/docs/guides/binary/buffer-to-arraybuffer.md +14 -0
  75. package/docs/guides/binary/buffer-to-blob.md +14 -0
  76. package/docs/guides/binary/buffer-to-readablestream.md +41 -0
  77. package/docs/guides/binary/buffer-to-string.md +25 -0
  78. package/docs/guides/binary/buffer-to-typedarray.md +14 -0
  79. package/docs/guides/binary/dataview-to-string.md +15 -0
  80. package/docs/guides/binary/typedarray-to-arraybuffer.md +25 -0
  81. package/docs/guides/binary/typedarray-to-blob.md +16 -0
  82. package/docs/guides/binary/typedarray-to-buffer.md +14 -0
  83. package/docs/guides/binary/typedarray-to-dataview.md +14 -0
  84. package/docs/guides/binary/typedarray-to-readablestream.md +41 -0
  85. package/docs/guides/binary/typedarray-to-string.md +16 -0
  86. package/docs/guides/ecosystem/astro.md +72 -0
  87. package/docs/guides/ecosystem/discordjs.md +77 -0
  88. package/docs/guides/ecosystem/docker.md +140 -0
  89. package/docs/guides/ecosystem/drizzle.md +185 -0
  90. package/docs/guides/ecosystem/edgedb.md +228 -0
  91. package/docs/guides/ecosystem/elysia.md +31 -0
  92. package/docs/guides/ecosystem/express.md +40 -0
  93. package/docs/guides/ecosystem/hono.md +39 -0
  94. package/docs/guides/ecosystem/mongoose.md +87 -0
  95. package/docs/guides/ecosystem/neon-drizzle.md +220 -0
  96. package/docs/guides/ecosystem/neon-serverless-postgres.md +55 -0
  97. package/docs/guides/ecosystem/nextjs.md +38 -0
  98. package/docs/guides/ecosystem/nuxt.md +56 -0
  99. package/docs/guides/ecosystem/pm2.md +57 -0
  100. package/docs/guides/ecosystem/prisma.md +140 -0
  101. package/docs/guides/ecosystem/qwik.md +107 -0
  102. package/docs/guides/ecosystem/react.md +49 -0
  103. package/docs/guides/ecosystem/remix.md +78 -0
  104. package/docs/guides/ecosystem/render.md +79 -0
  105. package/docs/guides/ecosystem/sentry.md +52 -0
  106. package/docs/guides/ecosystem/solidstart.md +58 -0
  107. package/docs/guides/ecosystem/ssr-react.md +51 -0
  108. package/docs/guides/ecosystem/stric.md +55 -0
  109. package/docs/guides/ecosystem/sveltekit.md +125 -0
  110. package/docs/guides/ecosystem/systemd.md +113 -0
  111. package/docs/guides/ecosystem/vite.md +70 -0
  112. package/docs/guides/http/cluster.md +66 -0
  113. package/docs/guides/http/fetch-unix.md +33 -0
  114. package/docs/guides/http/fetch.md +24 -0
  115. package/docs/guides/http/file-uploads.md +94 -0
  116. package/docs/guides/http/hot.md +22 -0
  117. package/docs/guides/http/proxy.md +24 -0
  118. package/docs/guides/http/server.md +46 -0
  119. package/docs/guides/http/simple.md +18 -0
  120. package/docs/guides/http/stream-file.md +48 -0
  121. package/docs/guides/http/stream-iterator.md +47 -0
  122. package/docs/guides/http/stream-node-streams-in-bun.md +20 -0
  123. package/docs/guides/http/tls.md +30 -0
  124. package/docs/guides/install/add-dev.md +26 -0
  125. package/docs/guides/install/add-git.md +36 -0
  126. package/docs/guides/install/add-optional.md +25 -0
  127. package/docs/guides/install/add-peer.md +17 -0
  128. package/docs/guides/install/add-tarball.md +33 -0
  129. package/docs/guides/install/add.md +42 -0
  130. package/docs/guides/install/azure-artifacts.md +73 -0
  131. package/docs/guides/install/cicd.md +41 -0
  132. package/docs/guides/install/custom-registry.md +30 -0
  133. package/docs/guides/install/from-npm-install-to-bun-install.md +214 -0
  134. package/docs/guides/install/git-diff-bun-lockfile.md +38 -0
  135. package/docs/guides/install/jfrog-artifactory.md +28 -0
  136. package/docs/guides/install/npm-alias.md +23 -0
  137. package/docs/guides/install/registry-scope.md +36 -0
  138. package/docs/guides/install/trusted.md +48 -0
  139. package/docs/guides/install/workspaces.md +70 -0
  140. package/docs/guides/install/yarnlock.md +42 -0
  141. package/docs/guides/process/argv.md +57 -0
  142. package/docs/guides/process/ctrl-c.md +16 -0
  143. package/docs/guides/process/ipc.md +66 -0
  144. package/docs/guides/process/nanoseconds.md +13 -0
  145. package/docs/guides/process/os-signals.md +39 -0
  146. package/docs/guides/process/spawn-stderr.md +31 -0
  147. package/docs/guides/process/spawn-stdout.md +26 -0
  148. package/docs/guides/process/spawn.md +41 -0
  149. package/docs/guides/process/stdin.md +54 -0
  150. package/docs/guides/read-file/arraybuffer.md +28 -0
  151. package/docs/guides/read-file/buffer.md +19 -0
  152. package/docs/guides/read-file/exists.md +16 -0
  153. package/docs/guides/read-file/json.md +17 -0
  154. package/docs/guides/read-file/mime.md +20 -0
  155. package/docs/guides/read-file/stream.md +26 -0
  156. package/docs/guides/read-file/string.md +22 -0
  157. package/docs/guides/read-file/uint8array.md +21 -0
  158. package/docs/guides/read-file/watch.md +68 -0
  159. package/docs/guides/runtime/cicd.md +43 -0
  160. package/docs/guides/runtime/define-constant.md +145 -0
  161. package/docs/guides/runtime/import-html.md +15 -0
  162. package/docs/guides/runtime/import-json.md +44 -0
  163. package/docs/guides/runtime/import-toml.md +30 -0
  164. package/docs/guides/runtime/read-env.md +32 -0
  165. package/docs/guides/runtime/set-env.md +37 -0
  166. package/docs/guides/runtime/shell.md +40 -0
  167. package/docs/guides/runtime/timezone.md +35 -0
  168. package/docs/guides/runtime/tsconfig-paths.md +29 -0
  169. package/docs/guides/runtime/typescript.md +47 -0
  170. package/docs/guides/runtime/vscode-debugger.md +47 -0
  171. package/docs/guides/runtime/web-debugger.md +82 -0
  172. package/docs/guides/streams/node-readable-to-arraybuffer.md +11 -0
  173. package/docs/guides/streams/node-readable-to-blob.md +11 -0
  174. package/docs/guides/streams/node-readable-to-json.md +12 -0
  175. package/docs/guides/streams/node-readable-to-string.md +12 -0
  176. package/docs/guides/streams/node-readable-to-uint8array.md +11 -0
  177. package/docs/guides/streams/to-array.md +14 -0
  178. package/docs/guides/streams/to-arraybuffer.md +14 -0
  179. package/docs/guides/streams/to-blob.md +14 -0
  180. package/docs/guides/streams/to-buffer.md +15 -0
  181. package/docs/guides/streams/to-json.md +14 -0
  182. package/docs/guides/streams/to-string.md +14 -0
  183. package/docs/guides/streams/to-typedarray.md +22 -0
  184. package/docs/guides/test/bail.md +22 -0
  185. package/docs/guides/test/coverage-threshold.md +60 -0
  186. package/docs/guides/test/coverage.md +44 -0
  187. package/docs/guides/test/happy-dom.md +68 -0
  188. package/docs/guides/test/migrate-from-jest.md +110 -0
  189. package/docs/guides/test/mock-clock.md +48 -0
  190. package/docs/guides/test/mock-functions.md +68 -0
  191. package/docs/guides/test/rerun-each.md +14 -0
  192. package/docs/guides/test/run-tests.md +111 -0
  193. package/docs/guides/test/skip-tests.md +39 -0
  194. package/docs/guides/test/snapshot.md +99 -0
  195. package/docs/guides/test/spy-on.md +46 -0
  196. package/docs/guides/test/testing-library.md +87 -0
  197. package/docs/guides/test/timeout.md +15 -0
  198. package/docs/guides/test/todo-tests.md +67 -0
  199. package/docs/guides/test/update-snapshots.md +50 -0
  200. package/docs/guides/test/watch-mode.md +19 -0
  201. package/docs/guides/util/base64.md +15 -0
  202. package/docs/guides/util/deep-equals.md +39 -0
  203. package/docs/guides/util/deflate.md +18 -0
  204. package/docs/guides/util/detect-bun.md +23 -0
  205. package/docs/guides/util/entrypoint.md +17 -0
  206. package/docs/guides/util/escape-html.md +22 -0
  207. package/docs/guides/util/file-url-to-path.md +14 -0
  208. package/docs/guides/util/gzip.md +18 -0
  209. package/docs/guides/util/hash-a-password.md +54 -0
  210. package/docs/guides/util/import-meta-dir.md +13 -0
  211. package/docs/guides/util/import-meta-file.md +13 -0
  212. package/docs/guides/util/import-meta-path.md +13 -0
  213. package/docs/guides/util/main.md +32 -0
  214. package/docs/guides/util/path-to-file-url.md +14 -0
  215. package/docs/guides/util/sleep.md +22 -0
  216. package/docs/guides/util/version.md +21 -0
  217. package/docs/guides/util/which-path-to-executable-bin.md +15 -0
  218. package/docs/guides/websocket/compression.md +31 -0
  219. package/docs/guides/websocket/context.md +72 -0
  220. package/docs/guides/websocket/pubsub.md +38 -0
  221. package/docs/guides/websocket/simple.md +33 -0
  222. package/docs/guides/write-file/append.md +52 -0
  223. package/docs/guides/write-file/basic.md +44 -0
  224. package/docs/guides/write-file/blob.md +28 -0
  225. package/docs/guides/write-file/cat.md +17 -0
  226. package/docs/guides/write-file/file-cp.md +16 -0
  227. package/docs/guides/write-file/filesink.md +52 -0
  228. package/docs/guides/write-file/response.md +17 -0
  229. package/docs/guides/write-file/stdout.md +21 -0
  230. package/docs/guides/write-file/stream.md +17 -0
  231. package/docs/guides/write-file/unlink.md +23 -0
  232. package/docs/index.md +77 -0
  233. package/docs/install/cache.md +57 -0
  234. package/docs/install/index.md +202 -0
  235. package/docs/install/lifecycle.md +46 -0
  236. package/docs/install/lockfile.md +90 -0
  237. package/docs/install/npmrc.md +75 -0
  238. package/docs/install/overrides.md +73 -0
  239. package/docs/install/patch.md +57 -0
  240. package/docs/install/registries.md +30 -0
  241. package/docs/install/workspaces.md +70 -0
  242. package/docs/installation.md +289 -0
  243. package/docs/project/benchmarking.md +203 -0
  244. package/docs/project/building-windows.md +162 -0
  245. package/docs/project/internals/build-process-for-ci.md +75 -0
  246. package/docs/project/roadmap.md +87 -0
  247. package/docs/quickstart.md +144 -0
  248. package/docs/runtime/autoimport.md +94 -0
  249. package/docs/runtime/bun-apis.md +129 -0
  250. package/docs/runtime/bunfig.md +532 -0
  251. package/docs/runtime/debugger.md +325 -0
  252. package/docs/runtime/env.md +214 -0
  253. package/docs/runtime/hot.md +139 -0
  254. package/docs/runtime/index.md +309 -0
  255. package/docs/runtime/jsx.md +326 -0
  256. package/docs/runtime/loaders.md +127 -0
  257. package/docs/runtime/modules.md +298 -0
  258. package/docs/runtime/nodejs-apis.md +456 -0
  259. package/docs/runtime/plugins.md +605 -0
  260. package/docs/runtime/shell.md +537 -0
  261. package/docs/runtime/typescript.md +60 -0
  262. package/docs/runtime/web-apis.md +128 -0
  263. package/docs/test/coverage.md +91 -0
  264. package/docs/test/dom.md +75 -0
  265. package/docs/test/hot.md +15 -0
  266. package/docs/test/lifecycle.md +81 -0
  267. package/docs/test/mocks.md +236 -0
  268. package/docs/test/snapshots.md +15 -0
  269. package/docs/test/time.md +106 -0
  270. package/docs/test/writing.md +547 -0
  271. package/docs/typescript.md +51 -0
  272. package/package.json +5 -4
@@ -0,0 +1,68 @@
1
+ ---
2
+ name: Mock functions in `bun test`
3
+ ---
4
+
5
+ Create mocks with the `mock` function from `bun:test`.
6
+
7
+ ```ts
8
+ import { test, expect, mock } from "bun:test";
9
+
10
+ const random = mock(() => Math.random());
11
+ ```
12
+
13
+ ---
14
+
15
+ The mock function can accept arguments.
16
+
17
+ ```ts
18
+ import { test, expect, mock } from "bun:test";
19
+
20
+ const random = mock((multiplier: number) => multiplier * Math.random());
21
+ ```
22
+
23
+ ---
24
+
25
+ The result of `mock()` is a new function that's been decorated with some additional properties.
26
+
27
+ ```ts
28
+ import { mock } from "bun:test";
29
+
30
+ const random = mock((multiplier: number) => multiplier * Math.random());
31
+
32
+ random(2);
33
+ random(10);
34
+
35
+ random.mock.calls;
36
+ // [[ 2 ], [ 10 ]]
37
+
38
+ random.mock.results;
39
+ // [
40
+ // { type: "return", value: 0.6533907460954099 },
41
+ // { type: "return", value: 0.6452713933037312 }
42
+ // ]
43
+ ```
44
+
45
+ ---
46
+
47
+ These extra properties make it possible to write `expect` assertions about usage of the mock function, including how many times it was called, the arguments, and the return values.
48
+
49
+ ```ts
50
+ import { test, expect, mock } from "bun:test";
51
+
52
+ const random = mock((multiplier: number) => multiplier * Math.random());
53
+
54
+ test("random", async () => {
55
+ const a = random(1);
56
+ const b = random(2);
57
+ const c = random(3);
58
+
59
+ expect(random).toHaveBeenCalled();
60
+ expect(random).toHaveBeenCalledTimes(3);
61
+ expect(random.mock.args).toEqual([[1], [2], [3]]);
62
+ expect(random.mock.results[0]).toEqual({ type: "return", value: a });
63
+ });
64
+ ```
65
+
66
+ ---
67
+
68
+ See [Docs > Test Runner > Mocks](https://bun.sh/docs/test/mocks) for complete documentation on mocking with the Bun test runner.
@@ -0,0 +1,14 @@
1
+ ---
2
+ name: Re-run tests multiple times with the Bun test runner
3
+ ---
4
+
5
+ Use the `--rerun-each` flag to re-run every test multiple times with the Bun test runner. This is useful for finding flaky or non-deterministic tests.
6
+
7
+ ```sh
8
+ # re-run each test 10 times
9
+ $ bun test --rerun-each 10
10
+ ```
11
+
12
+ ---
13
+
14
+ See [Docs > Test runner](https://bun.sh/docs/cli/test) for complete documentation of `bun test`.
@@ -0,0 +1,111 @@
1
+ ---
2
+ name: Run your tests with the Bun test runner
3
+ ---
4
+
5
+ Bun has a built-in [test runner](https://bun.sh/docs/cli/test) with a Jest-like `expect` API.
6
+
7
+ ---
8
+
9
+ To use it, run the `bun test` command from your project directory. The test runner will recursively search for all files in the directory that match the following patterns and execute the tests they contain.
10
+
11
+ ```txt
12
+ *.test.{js|jsx|ts|tsx}
13
+ *_test.{js|jsx|ts|tsx}
14
+ *.spec.{js|jsx|ts|tsx}
15
+ *_spec.{js|jsx|ts|tsx}
16
+ ```
17
+
18
+ ---
19
+
20
+ Here's what the output of a typical test run looks like. In this case, there are three tests files (`test.test.js`, `test2.test.js`, and `test3.test.js`) containing two tests each (`add` and `multiply`).
21
+
22
+ ```sh
23
+ $ bun test
24
+ bun test v1.x (9c68abdb)
25
+
26
+ test.test.js:
27
+ ✓ add [0.87ms]
28
+ ✓ multiply [0.02ms]
29
+
30
+ test2.test.js:
31
+ ✓ add [0.72ms]
32
+ ✓ multiply [0.01ms]
33
+
34
+ test3.test.js:
35
+ ✓ add [0.54ms]
36
+ ✓ multiply [0.01ms]
37
+
38
+ 6 pass
39
+ 0 fail
40
+ 6 expect() calls
41
+ Ran 6 tests across 3 files. [9.00ms]
42
+ ```
43
+
44
+ ---
45
+
46
+ To only run certain test files, pass a positional argument to `bun test`. The runner will only execute files that contain that argument in their path.
47
+
48
+ ```sh
49
+ $ bun test test3
50
+ bun test v1.x (9c68abdb)
51
+
52
+ test3.test.js:
53
+ ✓ add [1.40ms]
54
+ ✓ multiply [0.03ms]
55
+
56
+ 2 pass
57
+ 0 fail
58
+ 2 expect() calls
59
+ Ran 2 tests across 1 files. [15.00ms]
60
+ ```
61
+
62
+ ---
63
+
64
+ All tests have a name, defined using the first parameter to the `test` function. Tests can also be grouped into suites with `describe`.
65
+
66
+ ```ts
67
+ import { test, expect, describe } from "bun:test";
68
+
69
+ describe("math", () => {
70
+ test("add", () => {
71
+ expect(2 + 2).toEqual(4);
72
+ });
73
+
74
+ test("multiply", () => {
75
+ expect(2 * 2).toEqual(4);
76
+ });
77
+ });
78
+ ```
79
+
80
+ ---
81
+
82
+ To filter which tests are executed by name, use the `-t`/`--test-name-pattern` flag.
83
+
84
+ Adding `-t add` will only run tests with "add" in the name. This works with test names defined with `test` or test suite names defined with `describe`.
85
+
86
+ ```sh
87
+ $ bun test -t add
88
+ bun test v1.x (9c68abdb)
89
+
90
+ test.test.js:
91
+ ✓ add [1.79ms]
92
+ » multiply
93
+
94
+ test2.test.js:
95
+ ✓ add [2.30ms]
96
+ » multiply
97
+
98
+ test3.test.js:
99
+ ✓ add [0.32ms]
100
+ » multiply
101
+
102
+ 3 pass
103
+ 3 skip
104
+ 0 fail
105
+ 3 expect() calls
106
+ Ran 6 tests across 3 files. [59.00ms]
107
+ ```
108
+
109
+ ---
110
+
111
+ See [Docs > Test Runner](https://bun.sh/docs/cli/test) for complete documentation on the test runner.
@@ -0,0 +1,39 @@
1
+ ---
2
+ name: Skip tests with the Bun test runner
3
+ ---
4
+
5
+ To skip a test with the Bun test runner, use the `test.skip` function.
6
+
7
+ ```ts
8
+ import { test } from "bun:test";
9
+
10
+ test.skip("unimplemented feature", () => {
11
+ expect(Bun.isAwesome()).toBe(true);
12
+ });
13
+ ```
14
+
15
+ ---
16
+
17
+ Running `bun test` will not execute this test. It will be marked as skipped in the terminal output.
18
+
19
+ ```sh
20
+ $ bun test
21
+
22
+ test.test.ts:
23
+ ✓ add [0.03ms]
24
+ ✓ multiply [0.02ms]
25
+ » unimplemented feature
26
+
27
+ 2 pass
28
+ 1 skip
29
+ 0 fail
30
+ 2 expect() calls
31
+ Ran 3 tests across 1 files. [74.00ms]
32
+ ```
33
+
34
+ ---
35
+
36
+ See also:
37
+
38
+ - [Mark a test as a todo](/guides/test/todo-tests)
39
+ - [Docs > Test runner > Writing tests](https://bun.sh/docs/test/writing)
@@ -0,0 +1,99 @@
1
+ ---
2
+ name: Use snapshot testing in `bun test`
3
+ ---
4
+
5
+ Bun's test runner supports Jest-style snapshot testing via `.toMatchSnapshot()`.
6
+
7
+ {% callout %}
8
+ The `.toMatchInlineSnapshot()` method is not yet supported.
9
+ {% /callout %}
10
+
11
+ ```ts#snap.test.ts
12
+ import { test, expect } from "bun:test";
13
+
14
+ test("snapshot", () => {
15
+ expect({ foo: "bar" }).toMatchSnapshot();
16
+ });
17
+ ```
18
+
19
+ ---
20
+
21
+ The first time this test is executed, Bun will evaluate the value passed into `expect()` and write it to disk in a directory called `__snapshots__` that lives alongside the test file. (Note the `snapshots: +1 added` line in the output.)
22
+
23
+ ```sh
24
+ $ bun test test/snap
25
+ bun test v1.x (9c68abdb)
26
+
27
+ test/snap.test.ts:
28
+ ✓ snapshot [1.48ms]
29
+
30
+ 1 pass
31
+ 0 fail
32
+ snapshots: +1 added
33
+ 1 expect() calls
34
+ Ran 1 tests across 1 files. [82.00ms]
35
+ ```
36
+
37
+ ---
38
+
39
+ The `__snapshots__` directory contains a `.snap` file for each test file in the directory.
40
+
41
+ ```txt
42
+ test
43
+ ├── __snapshots__
44
+ │   └── snap.test.ts.snap
45
+ └── snap.test.ts
46
+ ```
47
+
48
+ ---
49
+
50
+ The `snap.test.ts.snap` file is a JavaScript file that exports a serialized version of the value passed into `expect()`. The `{foo: "bar"}` object has been serialized to JSON.
51
+
52
+ ```js
53
+ // Bun Snapshot v1, https://goo.gl/fbAQLP
54
+
55
+ exports[`snapshot 1`] = `
56
+ {
57
+ "foo": "bar",
58
+ }
59
+ `;
60
+ ```
61
+
62
+ ---
63
+
64
+ Later, when this test file is executed again, Bun will read the snapshot file and compare it to the value passed into `expect()`. If the values are different, the test will fail.
65
+
66
+ ```sh
67
+ $ bun test
68
+ bun test v1.x (9c68abdb)
69
+
70
+ test/snap.test.ts:
71
+ ✓ snapshot [1.05ms]
72
+
73
+ 1 pass
74
+ 0 fail
75
+ 1 snapshots, 1 expect() calls
76
+ Ran 1 tests across 1 files. [101.00ms]
77
+ ```
78
+
79
+ ---
80
+
81
+ To update snapshots, use the `--update-snapshots` flag.
82
+
83
+ ```sh
84
+ $ bun test --update-snapshots
85
+ bun test v1.x (9c68abdb)
86
+
87
+ test/snap.test.ts:
88
+ ✓ snapshot [0.86ms]
89
+
90
+ 1 pass
91
+ 0 fail
92
+ snapshots: +1 added # the snapshot was regenerated
93
+ 1 expect() calls
94
+ Ran 1 tests across 1 files. [102.00ms]
95
+ ```
96
+
97
+ ---
98
+
99
+ See [Docs > Test Runner > Snapshots](https://bun.sh/docs/test/mocks) for complete documentation on mocking with the Bun test runner.
@@ -0,0 +1,46 @@
1
+ ---
2
+ name: Spy on methods in `bun test`
3
+ ---
4
+
5
+ Use the `spyOn` utility to track method calls with Bun's test runner.
6
+
7
+ ```ts
8
+ import { test, expect, spyOn } from "bun:test";
9
+
10
+ const leo = {
11
+ name: "Leonardo",
12
+ sayHi(thing: string) {
13
+ console.log(`Sup I'm ${this.name} and I like ${thing}`);
14
+ },
15
+ };
16
+
17
+ const spy = spyOn(leo, "sayHi");
18
+ ```
19
+
20
+ ---
21
+
22
+ Once the spy is created, it can be used to write `expect` assertions relating to method calls.
23
+
24
+ ```ts-diff
25
+ import { test, expect, spyOn } from "bun:test";
26
+
27
+ const leo = {
28
+ name: "Leonardo",
29
+ sayHi(thing: string) {
30
+ console.log(`Sup I'm ${this.name} and I like ${thing}`);
31
+ },
32
+ };
33
+
34
+ const spy = spyOn(leo, "sayHi");
35
+
36
+ + test("turtles", ()=>{
37
+ + expect(spy).toHaveBeenCalledTimes(0);
38
+ + leo.sayHi("pizza");
39
+ + expect(spy).toHaveBeenCalledTimes(1);
40
+ + expect(spy.mock.calls).toEqual([[ "pizza" ]]);
41
+ + })
42
+ ```
43
+
44
+ ---
45
+
46
+ See [Docs > Test Runner > Mocks](https://bun.sh/docs/test/mocks) for complete documentation on mocking with the Bun test runner.
@@ -0,0 +1,87 @@
1
+ ---
2
+ name: Using Testing Library with Bun
3
+ ---
4
+ You can use [Testing Library](https://testing-library.com/) with Bun's test runner.
5
+
6
+ ---
7
+ As a prerequisite to using Testing Library you will need to install [Happy Dom](https://github.com/capricorn86/happy-dom). ([see Bun's Happy DOM guide for more information](https://bun.sh/guides/test/happy-dom)).
8
+
9
+ ```sh
10
+ bun add -D @happy-dom/global-registrator
11
+ ```
12
+
13
+ ---
14
+
15
+ Next you should install the Testing Library packages you are planning on using. For example, if you are setting up testing for React your installs may look like this. You will also need to install `@testing-library/jest-dom` to get matchers working later.
16
+
17
+ ```sh
18
+ bun add -D @testing-library/react @testing-library/dom @testing-library/jest-dom
19
+ ```
20
+ ---
21
+
22
+ Next you will need to create a preload script for Happy DOM and for Testing Library. For more details about the Happy DOM setup script see [Bun's Happy DOM guide](https://bun.sh/guides/test/happy-dom).
23
+
24
+ ```ts#happydom.ts
25
+ import { GlobalRegistrator } from '@happy-dom/global-registrator';
26
+
27
+ GlobalRegistrator.register();
28
+ ```
29
+ ---
30
+
31
+ For Testing Library, you will want to extend Bun's `expect` function with Testing Library's matchers. Optionally, to better match the behavior of test-runners like Jest, you may want to run cleanup after each test.
32
+
33
+ ```ts#testing-library.ts
34
+ import { afterEach, expect } from 'bun:test';
35
+ import { cleanup } from '@testing-library/react';
36
+ import * as matchers from '@testing-library/jest-dom/matchers';
37
+
38
+ expect.extend(matchers);
39
+
40
+ // Optional: cleans up `render` after each test
41
+ afterEach(() => {
42
+ cleanup();
43
+ });
44
+ ```
45
+
46
+ ---
47
+
48
+ Next, add these preload scripts to your `bunfig.toml` (you can also have everything in a single `preload.ts` script if you prefer).
49
+
50
+ ```toml#bunfig.toml
51
+ [test]
52
+ preload = ["happydom.ts", "testing-library.ts"]
53
+ ```
54
+ ---
55
+
56
+ If you are using TypeScript you will also need to make use of declaration merging in order to get the new matcher types to show up in your editor. To do this, create a type declaration file that extends `Matchers` like this.
57
+
58
+ ```ts#matchers.d.ts
59
+ import { TestingLibraryMatchers } from '@testing-library/jest-dom/matchers';
60
+ import { Matchers, AsymmetricMatchers } from 'bun:test';
61
+
62
+ declare module 'bun:test' {
63
+ interface Matchers<T>
64
+ extends TestingLibraryMatchers<typeof expect.stringContaining, T> {}
65
+ interface AsymmetricMatchers extends TestingLibraryMatchers {}
66
+ }
67
+ ```
68
+
69
+ ---
70
+
71
+ You should now be able to use Testing Library in your tests
72
+
73
+ ```ts
74
+ import { test, expect } from 'bun:test';
75
+ import { screen, render } from '@testing-library/react';
76
+ import { MyComponent } from './myComponent';
77
+
78
+ test('Can use Testing Library', () => {
79
+ render(MyComponent);
80
+ const myComponent = screen.getByTestId('my-component');
81
+ expect(myComponent).toBeInTheDocument();
82
+ })
83
+ ```
84
+
85
+ ---
86
+
87
+ Refer to the [Testing Library docs](https://testing-library.com/), [Happy DOM repo](https://github.com/capricorn86/happy-dom) and [Docs > Test runner > DOM](https://bun.sh/docs/test/dom) for complete documentation on writing browser tests with Bun.
@@ -0,0 +1,15 @@
1
+ ---
2
+ name: Set a per-test timeout with the Bun test runner
3
+ ---
4
+
5
+ Use the `--timeout` flag to set a timeout for each test in milliseconds. If any test exceeds this timeout, it will be marked as failed.
6
+
7
+ The default timeout is `5000` (5 seconds).
8
+
9
+ ```sh
10
+ $ bun test --timeout 3000 # 3 seconds
11
+ ```
12
+
13
+ ---
14
+
15
+ See [Docs > Test runner](https://bun.sh/docs/cli/test) for complete documentation of `bun test`.
@@ -0,0 +1,67 @@
1
+ ---
2
+ name: Mark a test as a "todo" with the Bun test runner
3
+ ---
4
+
5
+ To remind yourself to write a test later, use the `test.todo` function. There's no need to provide a test implementation.
6
+
7
+ ```ts
8
+ import { test, expect } from "bun:test";
9
+
10
+ // write this later
11
+ test.todo("unimplemented feature");
12
+ ```
13
+
14
+ ---
15
+
16
+ The output of `bun test` indicates how many `todo` tests were encountered.
17
+
18
+ ```sh
19
+ $ bun test
20
+
21
+ test.test.ts:
22
+ ✓ add [0.03ms]
23
+ ✓ multiply [0.02ms]
24
+ ✎ unimplemented feature
25
+
26
+ 2 pass
27
+ 1 todo
28
+ 0 fail
29
+ 2 expect() calls
30
+ Ran 3 tests across 1 files. [74.00ms]
31
+ ```
32
+
33
+ ---
34
+
35
+ Optionally, you can provide a test implementation.
36
+
37
+ ```ts
38
+ import { test, expect } from "bun:test";
39
+
40
+ test.todo("unimplemented feature", () => {
41
+ expect(Bun.isAwesome()).toBe(true);
42
+ });
43
+ ```
44
+
45
+ ---
46
+
47
+ If an implementation is provided, it will not be run unless the `--todo` flag is passed. If the `--todo` flag is passed, the test will be executed and _expected to fail_ by test runner! If a todo test passes, the `bun test` run will return a non-zero exit code to signal the failure.
48
+
49
+ ```sh
50
+ $ bun test --todo
51
+ my.test.ts:
52
+ ✗ unimplemented feature
53
+ ^ this test is marked as todo but passes. Remove `.todo` or check that test is correct.
54
+
55
+ 0 pass
56
+ 1 fail
57
+ 1 expect() calls
58
+ $ echo $?
59
+ 1 # this is the exit code of the previous command
60
+ ```
61
+
62
+ ---
63
+
64
+ See also:
65
+
66
+ - [Skip a test](/guides/test/skip-tests)
67
+ - [Docs > Test runner > Writing tests](https://bun.sh/docs/test/writing)
@@ -0,0 +1,50 @@
1
+ ---
2
+ name: Update snapshots in `bun test`
3
+ ---
4
+
5
+ Bun's test runner supports Jest-style snapshot testing via `.toMatchSnapshot()`.
6
+
7
+ {% callout %}
8
+ The `.toMatchInlineSnapshot()` method is not yet supported.
9
+ {% /callout %}
10
+
11
+ ```ts#snap.test.ts
12
+ import { test, expect } from "bun:test";
13
+
14
+ test("snapshot", () => {
15
+ expect({ foo: "bar" }).toMatchSnapshot();
16
+ });
17
+ ```
18
+
19
+ ---
20
+
21
+ The first time this test is executed, Bun will write a snapshot file to disk in a directory called `__snapshots__` that lives alongside the test file.
22
+
23
+ ```txt
24
+ test
25
+ ├── __snapshots__
26
+ │   └── snap.test.ts.snap
27
+ └── snap.test.ts
28
+ ```
29
+
30
+ ---
31
+
32
+ To regenerate snapshots, use the `--update-snapshots` flag.
33
+
34
+ ```sh
35
+ $ bun test --update-snapshots
36
+ bun test v1.x (9c68abdb)
37
+
38
+ test/snap.test.ts:
39
+ ✓ snapshot [0.86ms]
40
+
41
+ 1 pass
42
+ 0 fail
43
+ snapshots: +1 added # the snapshot was regenerated
44
+ 1 expect() calls
45
+ Ran 1 tests across 1 files. [102.00ms]
46
+ ```
47
+
48
+ ---
49
+
50
+ See [Docs > Test Runner > Snapshots](https://bun.sh/docs/test/mocks) for complete documentation on mocking with the Bun test runner.
@@ -0,0 +1,19 @@
1
+ ---
2
+ name: Run tests in watch mode with Bun
3
+ ---
4
+
5
+ Use the `--watch` flag to run your tests in watch mode.
6
+
7
+ ```sh
8
+ $ bun test --watch
9
+ ```
10
+
11
+ ---
12
+
13
+ This will restart the running Bun process whenever a file change is detected. It's fast. In this example, the editor is configured to save the file on every keystroke.
14
+
15
+ {% image src="https://github.com/oven-sh/bun/assets/3084745/dc49a36e-ba82-416f-b960-1c883a924248" caption="Running tests in watch mode in Bun" /%}
16
+
17
+ ---
18
+
19
+ See [Docs > Test Runner](https://bun.sh/docs/cli/test) for complete documentation on the test runner.
@@ -0,0 +1,15 @@
1
+ ---
2
+ name: Encode and decode base64 strings
3
+ ---
4
+
5
+ Bun implements the Web-standard [`atob`](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/atob) and [`btoa`](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/btoa) functions for encoding and decoding base64 strings.
6
+
7
+ ```ts
8
+ const data = "hello world";
9
+ const encoded = btoa(data); // => "aGVsbG8gd29ybGQ="
10
+ const decoded = atob(encoded); // => "hello world"
11
+ ```
12
+
13
+ ---
14
+
15
+ See [Docs > Web APIs](https://bun.sh/docs/runtime/web-apis) for a complete breakdown of the Web APIs implemented in Bun.
@@ -0,0 +1,39 @@
1
+ ---
2
+ name: Check if two objects are deeply equal
3
+ ---
4
+
5
+ Check if two objects are deeply equal. This is used internally by `expect().toEqual()` in Bun's [test runner](https://bun.sh/docs/test/writing).
6
+
7
+ ```ts#index.ts
8
+ const a = { a: 1, b: 2, c: { d: 3 } };
9
+ const b = { a: 1, b: 2, c: { d: 3 } };
10
+
11
+ Bun.deepEquals(a, b); // true
12
+ ```
13
+
14
+ ---
15
+
16
+ Pass `true` as a third argument to enable strict mode. This is used internally by `expect().toStrictEqual()` in Bun's [test runner](https://bun.sh/docs/test/writing).
17
+
18
+ The following examples would return `true` in non-strict mode but `false` in strict mode.
19
+
20
+ ```ts
21
+ // undefined values
22
+ Bun.deepEquals({}, { a: undefined }, true); // false
23
+
24
+ // undefined in arrays
25
+ Bun.deepEquals(["asdf"], ["asdf", undefined], true); // false
26
+
27
+ // sparse arrays
28
+ Bun.deepEquals([, 1], [undefined, 1], true); // false
29
+
30
+ // object literals vs instances w/ same properties
31
+ class Foo {
32
+ a = 1;
33
+ }
34
+ Bun.deepEquals(new Foo(), { a: 1 }, true); // false
35
+ ```
36
+
37
+ ---
38
+
39
+ See [Docs > API > Utils](https://bun.sh/docs/api/utils) for more useful utilities.
@@ -0,0 +1,18 @@
1
+ ---
2
+ name: Compress and decompress data with DEFLATE
3
+ ---
4
+
5
+ Use `Bun.deflateSync()` to compress a `Uint8Array` with DEFLATE.
6
+
7
+ ```ts
8
+ const data = Buffer.from("Hello, world!");
9
+ const compressed = Bun.deflateSync("Hello, world!");
10
+ // => Uint8Array
11
+
12
+ const decompressed = Bun.inflateSync(compressed);
13
+ // => Uint8Array
14
+ ```
15
+
16
+ ---
17
+
18
+ See [Docs > API > Utils](https://bun.sh/docs/api/utils) for more useful utilities.