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,106 @@
1
+ `bun:test` lets you change what time it is in your tests.
2
+
3
+ This works with any of the following:
4
+
5
+ - `Date.now`
6
+ - `new Date()`
7
+ - `new Intl.DateTimeFormat().format()`
8
+
9
+ Timers are not impacted yet, but may be in a future release of Bun.
10
+
11
+ ## `setSystemTime`
12
+
13
+ To change the system time, use `setSystemTime`:
14
+
15
+ ```ts
16
+ import { setSystemTime, beforeAll, test, expect } from "bun:test";
17
+
18
+ beforeAll(() => {
19
+ setSystemTime(new Date("2020-01-01T00:00:00.000Z"));
20
+ });
21
+
22
+ test("it is 2020", () => {
23
+ expect(new Date().getFullYear()).toBe(2020);
24
+ });
25
+ ```
26
+
27
+ To support existing tests that use Jest's `useFakeTimers` and `useRealTimers`, you can use `useFakeTimers` and `useRealTimers`:
28
+
29
+ ```ts
30
+ test("just like in jest", () => {
31
+ jest.useFakeTimers();
32
+ jest.setSystemTime(new Date("2020-01-01T00:00:00.000Z"));
33
+ expect(new Date().getFullYear()).toBe(2020);
34
+ jest.useRealTimers();
35
+ expect(new Date().getFullYear()).toBeGreaterThan(2020);
36
+ });
37
+
38
+ test("unlike in jest", () => {
39
+ const OriginalDate = Date;
40
+ jest.useFakeTimers();
41
+ if (typeof Bun === "undefined") {
42
+ // In Jest, the Date constructor changes
43
+ // That can cause all sorts of bugs because suddenly Date !== Date before the test.
44
+ expect(Date).not.toBe(OriginalDate);
45
+ expect(Date.now).not.toBe(OriginalDate.now);
46
+ } else {
47
+ // In bun:test, Date constructor does not change when you useFakeTimers
48
+ expect(Date).toBe(OriginalDate);
49
+ expect(Date.now).toBe(OriginalDate.now);
50
+ }
51
+ });
52
+ ```
53
+
54
+ {% callout %}
55
+ **Timers** — Note that we have not implemented builtin support for mocking timers yet, but this is on the roadmap.
56
+ {% /callout %}
57
+
58
+ ### Reset the system time
59
+
60
+ To reset the system time, pass no arguments to `setSystemTime`:
61
+
62
+ ```ts
63
+ import { setSystemTime, expect, test } from "bun:test";
64
+
65
+ test("it was 2020, for a moment.", () => {
66
+ // Set it to something!
67
+ setSystemTime(new Date("2020-01-01T00:00:00.000Z"));
68
+ expect(new Date().getFullYear()).toBe(2020);
69
+
70
+ // reset it!
71
+ setSystemTime();
72
+
73
+ expect(new Date().getFullYear()).toBeGreaterThan(2020);
74
+ });
75
+ ```
76
+
77
+ ## Set the time zone
78
+
79
+ To change the time zone, either pass the `$TZ` environment variable to `bun test`.
80
+
81
+ ```sh
82
+ TZ=America/Los_Angeles bun test
83
+ ```
84
+
85
+ Or set `process.env.TZ` at runtime:
86
+
87
+ ```ts
88
+ import { test, expect } from "bun:test";
89
+
90
+ test("Welcome to California!", () => {
91
+ process.env.TZ = "America/Los_Angeles";
92
+ expect(new Date().getTimezoneOffset()).toBe(420);
93
+ expect(new Intl.DateTimeFormat().resolvedOptions().timeZone).toBe(
94
+ "America/Los_Angeles",
95
+ );
96
+ });
97
+
98
+ test("Welcome to New York!", () => {
99
+ // Unlike in Jest, you can set the timezone multiple times at runtime and it will work.
100
+ process.env.TZ = "America/New_York";
101
+ expect(new Date().getTimezoneOffset()).toBe(240);
102
+ expect(new Intl.DateTimeFormat().resolvedOptions().timeZone).toBe(
103
+ "America/New_York",
104
+ );
105
+ });
106
+ ```
@@ -0,0 +1,547 @@
1
+ Define tests with a Jest-like API imported from the built-in `bun:test` module. Long term, Bun aims for complete Jest compatibility; at the moment, a [limited set](#matchers) of `expect` matchers are supported.
2
+
3
+ ## Basic usage
4
+
5
+ To define a simple test:
6
+
7
+ ```ts#math.test.ts
8
+ import { expect, test } from "bun:test";
9
+
10
+ test("2 + 2", () => {
11
+ expect(2 + 2).toBe(4);
12
+ });
13
+ ```
14
+
15
+ {% details summary="Jest-style globals" %}
16
+ As in Jest, you can use `describe`, `test`, `expect`, and other functions without importing them. Unlike Jest, they are not injected into the global scope. Instead, the Bun transpiler will automatically inject an import from `bun:test` internally.
17
+
18
+ ```ts
19
+ typeof globalThis.describe; // "undefined"
20
+ typeof describe; // "function"
21
+ ```
22
+
23
+ This transpiler integration only occurs during `bun test`, and only for test files & preloaded scripts. In practice there's no significant difference to the end user.
24
+ {% /details %}
25
+
26
+ Tests can be grouped into suites with `describe`.
27
+
28
+ ```ts#math.test.ts
29
+ import { expect, test, describe } from "bun:test";
30
+
31
+ describe("arithmetic", () => {
32
+ test("2 + 2", () => {
33
+ expect(2 + 2).toBe(4);
34
+ });
35
+
36
+ test("2 * 2", () => {
37
+ expect(2 * 2).toBe(4);
38
+ });
39
+ });
40
+ ```
41
+
42
+ Tests can be `async`.
43
+
44
+ ```ts
45
+ import { expect, test } from "bun:test";
46
+
47
+ test("2 * 2", async () => {
48
+ const result = await Promise.resolve(2 * 2);
49
+ expect(result).toEqual(4);
50
+ });
51
+ ```
52
+
53
+ Alternatively, use the `done` callback to signal completion. If you include the `done` callback as a parameter in your test definition, you _must_ call it or the test will hang.
54
+
55
+ ```ts
56
+ import { expect, test } from "bun:test";
57
+
58
+ test("2 * 2", done => {
59
+ Promise.resolve(2 * 2).then(result => {
60
+ expect(result).toEqual(4);
61
+ done();
62
+ });
63
+ });
64
+ ```
65
+
66
+ ## Timeouts
67
+
68
+ Optionally specify a per-test timeout in milliseconds by passing a number as the third argument to `test`.
69
+
70
+ ```ts
71
+ import { test } from "bun:test";
72
+
73
+ test("wat", async () => {
74
+ const data = await slowOperation();
75
+ expect(data).toBe(42);
76
+ }, 500); // test must run in <500ms
77
+ ```
78
+
79
+ In `bun:test`, test timeouts throw an uncatchable exception to force the test to stop running and fail. We also kill any child processes that were spawned in the test to avoid leaving behind zombie processes lurking in the background.
80
+
81
+ ### 🧟 Zombie process killer
82
+
83
+ When a test times out and processes spawned in the test via `Bun.spawn`, `Bun.spawnSync`, or `node:child_process` are not killed, they will be automatically killed and a message will be logged to the console.
84
+
85
+ ## `test.skip`
86
+
87
+ Skip individual tests with `test.skip`. These tests will not be run.
88
+
89
+ ```ts
90
+ import { expect, test } from "bun:test";
91
+
92
+ test.skip("wat", () => {
93
+ // TODO: fix this
94
+ expect(0.1 + 0.2).toEqual(0.3);
95
+ });
96
+ ```
97
+
98
+ ## `test.todo`
99
+
100
+ Mark a test as a todo with `test.todo`. These tests will not be run.
101
+
102
+ ```ts
103
+ import { expect, test } from "bun:test";
104
+
105
+ test.todo("fix this", () => {
106
+ myTestFunction();
107
+ });
108
+ ```
109
+
110
+ To run todo tests and find any which are passing, use `bun test --todo`.
111
+
112
+ ```sh
113
+ $ bun test --todo
114
+ my.test.ts:
115
+ ✗ unimplemented feature
116
+ ^ this test is marked as todo but passes. Remove `.todo` or check that test is correct.
117
+
118
+ 0 pass
119
+ 1 fail
120
+ 1 expect() calls
121
+ ```
122
+
123
+ With this flag, failing todo tests will not cause an error, but todo tests which pass will be marked as failing so you can remove the todo mark or
124
+ fix the test.
125
+
126
+ ## `test.only`
127
+
128
+ To run a particular test or suite of tests use `test.only()` or `describe.only()`. Once declared, running `bun test --only` will only execute tests/suites that have been marked with `.only()`. Running `bun test` without the `--only` option with `test.only()` declared will result in all tests in the given suite being executed _up to_ the test with `.only()`. `describe.only()` functions the same in both execution scenarios.
129
+
130
+ ```ts
131
+ import { test, describe } from "bun:test";
132
+
133
+ test("test #1", () => {
134
+ // does not run
135
+ });
136
+
137
+ test.only("test #2", () => {
138
+ // runs
139
+ });
140
+
141
+ describe.only("only", () => {
142
+ test("test #3", () => {
143
+ // runs
144
+ });
145
+ });
146
+ ```
147
+
148
+ The following command will only execute tests #2 and #3.
149
+
150
+ ```sh
151
+ $ bun test --only
152
+ ```
153
+
154
+ The following command will only execute tests #1, #2 and #3.
155
+
156
+ ```sh
157
+ $ bun test
158
+ ```
159
+
160
+ ## `test.if`
161
+
162
+ To run a test conditionally, use `test.if()`. The test will run if the condition is truthy. This is particularly useful for tests that should only run on specific architectures or operating systems.
163
+
164
+ ```ts
165
+ test.if(Math.random() > 0.5)("runs half the time", () => {
166
+ // ...
167
+ });
168
+
169
+ const macOS = process.arch === "darwin";
170
+ test.if(macOS)("runs on macOS", () => {
171
+ // runs if macOS
172
+ });
173
+ ```
174
+
175
+ ## `test.skipIf`
176
+
177
+ To instead skip a test based on some condition, use `test.skipIf()` or `describe.skipIf()`.
178
+
179
+ ```ts
180
+ const macOS = process.arch === "darwin";
181
+
182
+ test.skipIf(macOS)("runs on non-macOS", () => {
183
+ // runs if *not* macOS
184
+ });
185
+ ```
186
+
187
+ ## `test.todoIf`
188
+
189
+ If instead you want to mark the test as TODO, use `test.todoIf()` or `describe.todoIf()`. Carefully choosing `skipIf` or `todoIf` can show a difference between, for example, intent of "invalid for this target" and "planned but not implemented yet."
190
+
191
+ ```ts
192
+ const macOS = process.arch === "darwin";
193
+
194
+ // TODO: we've only implemented this for Linux so far.
195
+ test.todoIf(macOS)("runs on posix", () => {
196
+ // runs if *not* macOS
197
+ });
198
+ ```
199
+
200
+ ## `test.each`
201
+
202
+ To return a function for multiple cases in a table of tests, use `test.each`.
203
+
204
+ ```ts
205
+ const cases = [
206
+ [1, 2, 3],
207
+ [3, 4, 5],
208
+ ];
209
+
210
+ test.each(cases)("%p + %p should be %p", (a, b, expected) => {
211
+ // runs once for each test case provided
212
+ });
213
+ ```
214
+
215
+ There are a number of options available for formatting the case label depending on its type.
216
+
217
+ {% table %}
218
+
219
+ ---
220
+
221
+ - `%p`
222
+ - [`pretty-format`](https://www.npmjs.com/package/pretty-format)
223
+
224
+ ---
225
+
226
+ - `%s`
227
+ - String
228
+
229
+ ---
230
+
231
+ - `%d`
232
+ - Number
233
+
234
+ ---
235
+
236
+ - `%i`
237
+ - Integer
238
+
239
+ ---
240
+
241
+ - `%f`
242
+ - Floating point
243
+
244
+ ---
245
+
246
+ - `%j`
247
+ - JSON
248
+
249
+ ---
250
+
251
+ - `%o`
252
+ - Object
253
+
254
+ ---
255
+
256
+ - `%#`
257
+ - Index of the test case
258
+
259
+ ---
260
+
261
+ - `%%`
262
+ - Single percent sign (`%`)
263
+
264
+ {% /table %}
265
+
266
+ ## Matchers
267
+
268
+ Bun implements the following matchers. Full Jest compatibility is on the roadmap; track progress [here](https://github.com/oven-sh/bun/issues/1825).
269
+
270
+ {% table %}
271
+
272
+ ---
273
+
274
+ - ✅
275
+ - [`.not`](https://jestjs.io/docs/expect#not)
276
+
277
+ ---
278
+
279
+ - ✅
280
+ - [`.toBe()`](https://jestjs.io/docs/expect#tobevalue)
281
+
282
+ ---
283
+
284
+ - ✅
285
+ - [`.toEqual()`](https://jestjs.io/docs/expect#toequalvalue)
286
+
287
+ ---
288
+
289
+ - ✅
290
+ - [`.toBeNull()`](https://jestjs.io/docs/expect#tobenull)
291
+
292
+ ---
293
+
294
+ - ✅
295
+ - [`.toBeUndefined()`](https://jestjs.io/docs/expect#tobeundefined)
296
+
297
+ ---
298
+
299
+ - ✅
300
+ - [`.toBeNaN()`](https://jestjs.io/docs/expect#tobenan)
301
+
302
+ ---
303
+
304
+ - ✅
305
+ - [`.toBeDefined()`](https://jestjs.io/docs/expect#tobedefined)
306
+
307
+ ---
308
+
309
+ - ✅
310
+ - [`.toBeFalsy()`](https://jestjs.io/docs/expect#tobefalsy)
311
+
312
+ ---
313
+
314
+ - ✅
315
+ - [`.toBeTruthy()`](https://jestjs.io/docs/expect#tobetruthy)
316
+
317
+ ---
318
+
319
+ - ✅
320
+ - [`.toContain()`](https://jestjs.io/docs/expect#tocontainitem)
321
+
322
+ ---
323
+
324
+ - ✅
325
+ - [`.toContainAllKeys()`](https://jest-extended.jestcommunity.dev/docs/matchers/Object#tocontainallkeyskeys)
326
+
327
+ ---
328
+
329
+ - ✅
330
+ - [`.toContainValue()`](https://jest-extended.jestcommunity.dev/docs/matchers/Object#tocontainvaluevalue)
331
+
332
+ ---
333
+
334
+ - ✅
335
+ - [`.toContainValues()`](https://jest-extended.jestcommunity.dev/docs/matchers/Object#tocontainvaluesvalues)
336
+
337
+ ---
338
+
339
+ - ✅
340
+ - [`.toContainAllValues()`](https://jest-extended.jestcommunity.dev/docs/matchers/Object#tocontainallvaluesvalues)
341
+
342
+ ---
343
+
344
+ - ✅
345
+ - [`.toContainAnyValues()`](https://jest-extended.jestcommunity.dev/docs/matchers/Object#tocontainanyvaluesvalues)
346
+
347
+ ---
348
+
349
+ - ✅
350
+ - [`.toStrictEqual()`](https://jestjs.io/docs/expect#tostrictequalvalue)
351
+
352
+ ---
353
+
354
+ - ✅
355
+ - [`.toThrow()`](https://jestjs.io/docs/expect#tothrowerror)
356
+
357
+ ---
358
+
359
+ - ✅
360
+ - [`.toHaveLength()`](https://jestjs.io/docs/expect#tohavelengthnumber)
361
+
362
+ ---
363
+
364
+ - ✅
365
+ - [`.toHaveProperty()`](https://jestjs.io/docs/expect#tohavepropertykeypath-value)
366
+
367
+ ---
368
+
369
+ - ✅
370
+ - [`.extend`](https://jestjs.io/docs/expect#expectextendmatchers)
371
+
372
+ ---
373
+
374
+ - ✅
375
+ - [`.anything()`](https://jestjs.io/docs/expect#expectanything)
376
+
377
+ ---
378
+
379
+ - ✅
380
+ - [`.any()`](https://jestjs.io/docs/expect#expectanyconstructor)
381
+
382
+ ---
383
+
384
+ - ✅
385
+ - [`.arrayContaining()`](https://jestjs.io/docs/expect#expectarraycontainingarray)
386
+
387
+ ---
388
+
389
+ - ✅
390
+ - [`.assertions()`](https://jestjs.io/docs/expect#expectassertionsnumber)
391
+
392
+ ---
393
+
394
+ - ✅
395
+ - [`.closeTo()`](https://jestjs.io/docs/expect#expectclosetonumber-numdigits)
396
+
397
+ ---
398
+
399
+ - ✅
400
+ - [`.hasAssertions()`](https://jestjs.io/docs/expect#expecthasassertions)
401
+
402
+ ---
403
+
404
+ - ✅
405
+ - [`.objectContaining()`](https://jestjs.io/docs/expect#expectobjectcontainingobject)
406
+
407
+ ---
408
+
409
+ - ✅
410
+ - [`.stringContaining()`](https://jestjs.io/docs/expect#expectstringcontainingstring)
411
+
412
+ ---
413
+
414
+ - ✅
415
+ - [`.stringMatching()`](https://jestjs.io/docs/expect#expectstringmatchingstring--regexp)
416
+
417
+ ---
418
+
419
+ - ❌
420
+ - [`.addSnapshotSerializer()`](https://jestjs.io/docs/expect#expectaddsnapshotserializerserializer)
421
+
422
+ ---
423
+
424
+ - ✅
425
+ - [`.resolves()`](https://jestjs.io/docs/expect#resolves)
426
+
427
+ ---
428
+
429
+ - ✅
430
+ - [`.rejects()`](https://jestjs.io/docs/expect#rejects)
431
+
432
+ ---
433
+
434
+ - ✅
435
+ - [`.toHaveBeenCalled()`](https://jestjs.io/docs/expect#tohavebeencalled)
436
+
437
+ ---
438
+
439
+ - ✅
440
+ - [`.toHaveBeenCalledTimes()`](https://jestjs.io/docs/expect#tohavebeencalledtimesnumber)
441
+
442
+ ---
443
+
444
+ - ✅
445
+ - [`.toHaveBeenCalledWith()`](https://jestjs.io/docs/expect#tohavebeencalledwitharg1-arg2-)
446
+
447
+ ---
448
+
449
+ - ✅
450
+ - [`.toHaveBeenLastCalledWith()`](https://jestjs.io/docs/expect#tohavebeenlastcalledwitharg1-arg2-)
451
+
452
+ ---
453
+
454
+ - ✅
455
+ - [`.toHaveBeenNthCalledWith()`](https://jestjs.io/docs/expect#tohavebeennthcalledwithnthcall-arg1-arg2-)
456
+
457
+ ---
458
+
459
+ - ✅
460
+ - [`.toHaveReturned()`](https://jestjs.io/docs/expect#tohavereturned)
461
+
462
+ ---
463
+
464
+ - ✅
465
+ - [`.toHaveReturnedTimes()`](https://jestjs.io/docs/expect#tohavereturnedtimesnumber)
466
+
467
+ ---
468
+
469
+ - ❌
470
+ - [`.toHaveReturnedWith()`](https://jestjs.io/docs/expect#tohavereturnedwithvalue)
471
+
472
+ ---
473
+
474
+ - ❌
475
+ - [`.toHaveLastReturnedWith()`](https://jestjs.io/docs/expect#tohavelastreturnedwithvalue)
476
+
477
+ ---
478
+
479
+ - ❌
480
+ - [`.toHaveNthReturnedWith()`](https://jestjs.io/docs/expect#tohaventhreturnedwithnthcall-value)
481
+
482
+ ---
483
+
484
+ - ✅
485
+ - [`.toBeCloseTo()`](https://jestjs.io/docs/expect#tobeclosetonumber-numdigits)
486
+
487
+ ---
488
+
489
+ - ✅
490
+ - [`.toBeGreaterThan()`](https://jestjs.io/docs/expect#tobegreaterthannumber--bigint)
491
+
492
+ ---
493
+
494
+ - ✅
495
+ - [`.toBeGreaterThanOrEqual()`](https://jestjs.io/docs/expect#tobegreaterthanorequalnumber--bigint)
496
+
497
+ ---
498
+
499
+ - ✅
500
+ - [`.toBeLessThan()`](https://jestjs.io/docs/expect#tobelessthannumber--bigint)
501
+
502
+ ---
503
+
504
+ - ✅
505
+ - [`.toBeLessThanOrEqual()`](https://jestjs.io/docs/expect#tobelessthanorequalnumber--bigint)
506
+
507
+ ---
508
+
509
+ - ✅
510
+ - [`.toBeInstanceOf()`](https://jestjs.io/docs/expect#tobeinstanceofclass)
511
+
512
+ ---
513
+
514
+ - ✅
515
+ - [`.toContainEqual()`](https://jestjs.io/docs/expect#tocontainequalitem)
516
+
517
+ ---
518
+
519
+ - ✅
520
+ - [`.toMatch()`](https://jestjs.io/docs/expect#tomatchregexp--string)
521
+
522
+ ---
523
+
524
+ - ✅
525
+ - [`.toMatchObject()`](https://jestjs.io/docs/expect#tomatchobjectobject)
526
+
527
+ ---
528
+
529
+ - ✅
530
+ - [`.toMatchSnapshot()`](https://jestjs.io/docs/expect#tomatchsnapshotpropertymatchers-hint)
531
+
532
+ ---
533
+
534
+ - ❌
535
+ - [`.toMatchInlineSnapshot()`](https://jestjs.io/docs/expect#tomatchinlinesnapshotpropertymatchers-inlinesnapshot)
536
+
537
+ ---
538
+
539
+ - ❌
540
+ - [`.toThrowErrorMatchingSnapshot()`](https://jestjs.io/docs/expect#tothrowerrormatchingsnapshothint)
541
+
542
+ ---
543
+
544
+ - ❌
545
+ - [`.toThrowErrorMatchingInlineSnapshot()`](https://jestjs.io/docs/expect#tothrowerrormatchinginlinesnapshotinlinesnapshot)
546
+
547
+ {% /table %}
@@ -0,0 +1,51 @@
1
+ To install the TypeScript definitions for Bun's built-in APIs, install `@types/bun`.
2
+
3
+ ```sh
4
+ $ bun add -d @types/bun # dev dependency
5
+ ```
6
+
7
+ At this point, you should be able to reference the `Bun` global in your TypeScript files without seeing errors in your editor.
8
+
9
+ ```ts
10
+ console.log(Bun.version);
11
+ ```
12
+
13
+ ## Suggested `compilerOptions`
14
+
15
+ Bun supports things like top-level await, JSX, and extensioned `.ts` imports, which TypeScript doesn't allow by default. Below is a set of recommended `compilerOptions` for a Bun project, so you can use these features without seeing compiler warnings from TypeScript.
16
+
17
+ ```jsonc
18
+ {
19
+ "compilerOptions": {
20
+ // Enable latest features
21
+ "lib": ["ESNext"],
22
+ "target": "ESNext",
23
+ "module": "ESNext",
24
+ "moduleDetection": "force",
25
+ "jsx": "react-jsx",
26
+ "allowJs": true,
27
+
28
+ // Bundler mode
29
+ "moduleResolution": "bundler",
30
+ "allowImportingTsExtensions": true,
31
+ "verbatimModuleSyntax": true,
32
+ "noEmit": true,
33
+
34
+ // Best practices
35
+ "strict": true,
36
+ "skipLibCheck": true,
37
+ "noFallthroughCasesInSwitch": true,
38
+
39
+ // Some stricter flags
40
+ "noUnusedLocals": true,
41
+ "noUnusedParameters": true,
42
+ "noPropertyAccessFromIndexSignature": true
43
+ }
44
+ }
45
+ ```
46
+
47
+ If you run `bun init` in a new directory, this `tsconfig.json` will be generated for you. (The stricter flags are disabled by default.)
48
+
49
+ ```sh
50
+ $ bun init
51
+ ```