Skip to content

Commit df0e075

Browse files
committed
🏁 Add bun tests
1 parent da968b2 commit df0e075

File tree

8 files changed

+108
-4
lines changed

8 files changed

+108
-4
lines changed

.github/workflows/bun.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: Bun compatibility test
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches:
7+
- master
8+
- dev
9+
workflow_dispatch:
10+
11+
jobs:
12+
bun-test:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- uses: actions/checkout@v3
16+
- uses: moonrepo/setup-toolchain@v0
17+
with:
18+
auto-install: true
19+
- name: Install dependencies
20+
run: npm ci
21+
- name: Build
22+
run: npm run build
23+
- name: Test Bun compatibility
24+
run: npm run test:bun

.prototools

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node = "lts"
22
npm = "latest"
33
deno = "latest"
4+
bun = "latest"

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ cd wretch
1515

1616
### 2 - Setup toolchain
1717

18-
This project uses [proto](https://moonrepo.dev/proto) to manage tool versions (Node.js, npm, Deno). Install proto:
18+
This project uses [proto](https://moonrepo.dev/proto) to manage tool versions (Node.js, npm, Deno, Bun). Install proto:
1919

2020
```bash
2121
# macOS/Linux

README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
- 💡 **Intuitive** - lean API, handles errors, headers and (de)serialization
3232
- 🧊 **Immutable** - every call creates a cloned instance that can then be reused safely
3333
- 🔌 **Modular** - plug addons to add new features, and middlewares to intercept requests
34-
- 🧩 **Isomorphic** - compatible with modern browsers, Node.js 22+ and Deno
34+
- 🧩 **Isomorphic** - compatible with modern browsers, Node.js 22+, Deno and Bun
3535
- 🦺 **Type safe** - strongly typed, written in TypeScript
3636
-**Proven** - fully covered by unit tests and widely used
3737
- 💓 **Maintained** - alive and well for many years
@@ -236,6 +236,21 @@ const text = await wretch("https://httpbun.org").get("/status/200").text();
236236
console.log(text); // -> { "code": 200, "description": "OK" }
237237
```
238238

239+
## Bun
240+
241+
Works with [Bun](https://bun.sh/) out of the box.
242+
243+
```bash
244+
bun add wretch
245+
```
246+
247+
```ts
248+
import wretch from "wretch";
249+
250+
const text = await wretch("https://httpbun.org").get("/status/200").text();
251+
console.log(text); // -> { "code": 200, "description": "OK" }
252+
```
253+
239254
# Usage
240255

241256
## Import

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@
121121
"test:browser": "concurrently --success first -k 'npm run mock:wait && web-test-runner' 'npm run mock'",
122122
"test:browser:watch": "concurrently --success first -k 'npm run mock:wait && web-test-runner --watch' 'npm run mock'",
123123
"test:deno": "concurrently --success first -k 'npm run mock:wait && deno test --allow-net --allow-read --no-check --sloppy-imports --config test/deno/deno.json test/deno/wretch.spec.ts' 'npm run mock'",
124+
"test:bun": "concurrently --success first -k 'npm run mock:wait && bun test test/bun/wretch.spec.ts' 'npm run mock'",
124125
"test:snippets:generate": "node --import tsx scripts/generateSnippetTests.ts",
125126
"test:snippets": "npm run test:snippets:generate && node --experimental-vm-modules --import tsx --test test/generated/snippets.spec.ts",
126127
"test:snippets:skip-failing": "node --import tsx scripts/addSkipComments.ts",

test/bun/helpers.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { expect as bunExpect } from "bun:test"
2+
3+
export const expect = (actual: any) => ({
4+
toBe: (expected: unknown) => bunExpect(actual).toBe(expected),
5+
toEqual: (expected: unknown) => bunExpect(actual).toEqual(expected),
6+
toBeNull: () => bunExpect(actual).toBeNull(),
7+
toBeTruthy: () => bunExpect(actual).toBeTruthy(),
8+
toBeUndefined: () => bunExpect(actual).toBeUndefined(),
9+
toBeGreaterThanOrEqual: (expected: any) => bunExpect(actual).toBeGreaterThanOrEqual(expected),
10+
toStrictEqual: (expected: unknown) => bunExpect(actual).toStrictEqual(expected),
11+
toMatchObject: (expected: any) => bunExpect(actual).toMatchObject(expected),
12+
})
13+
14+
export const assert = {
15+
rejects: async (
16+
fn: () => Promise<unknown>,
17+
validator?: (error: unknown) => boolean
18+
) => {
19+
try {
20+
await fn()
21+
throw new Error("Expected promise to reject")
22+
} catch (error) {
23+
if (validator && !validator(error)) {
24+
throw new Error("Validator returned false")
25+
}
26+
}
27+
}
28+
}
29+
30+
export const fs = {
31+
openAsBlob: async (path: string) => {
32+
const file = Bun.file(path)
33+
return file
34+
}
35+
}

test/bun/wretch.spec.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { describe, it, beforeEach } from "bun:test"
2+
import { expect, assert, fs } from "./helpers"
3+
import { createWretchTests, createMixTests } from "../shared/wretch.spec"
4+
5+
const duckImagePath = new URL("../assets/duck.jpg", import.meta.url).pathname
6+
const duckImage = await Bun.file(duckImagePath).arrayBuffer().then(buf => new Uint8Array(buf))
7+
8+
createWretchTests({
9+
describe,
10+
it,
11+
beforeEach,
12+
assert,
13+
expect,
14+
fs,
15+
duckImage,
16+
duckImagePath,
17+
})
18+
19+
createMixTests({
20+
describe,
21+
it,
22+
assert,
23+
expect,
24+
fs,
25+
duckImage,
26+
duckImagePath,
27+
})

test/shared/wretch.spec.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const isSafari =
2121
navigator.userAgent.indexOf("Chrome") < 0
2222
const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"
2323
const isDeno = typeof globalThis.Deno !== "undefined"
24+
const isBun = typeof globalThis.Bun !== "undefined"
2425

2526
const allRoutes = <T>(
2627
obj: Wretch,
@@ -562,7 +563,7 @@ export function createWretchTests(ctx: TestContext): void {
562563
})
563564

564565
it("should retrieve performance timings associated with a fetch request", async function () {
565-
if (isSafari || isDeno)
566+
if (isSafari || isDeno || isBun)
566567
return
567568

568569
const w = wretch()
@@ -799,7 +800,7 @@ export function createWretchTests(ctx: TestContext): void {
799800
})
800801

801802
it("should program resolvers", async function () {
802-
if(isSafari || isDeno)
803+
if(isSafari || isDeno || isBun)
803804
return
804805

805806
let check = 0

0 commit comments

Comments
 (0)