Skip to content

Commit 2011ea6

Browse files
committed
Bun tests
1 parent 768f523 commit 2011ea6

6 files changed

Lines changed: 135 additions & 23 deletions

File tree

.github/workflows/build.yaml

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,29 @@ jobs:
6767
- run: deno task test:node
6868
working-directory: ${{ github.workspace }}/fedify/
6969

70+
test-bun:
71+
strategy:
72+
matrix:
73+
os: [macos-latest, ubuntu-latest, windows-latest]
74+
fail-fast: false
75+
runs-on: ${{ matrix.os }}
76+
steps:
77+
- uses: actions/checkout@v4
78+
- uses: denoland/setup-deno@v2
79+
with:
80+
deno-version: v2.x
81+
- uses: actions/setup-node@v4
82+
with:
83+
node-version: lts/*
84+
- uses: oven-sh/setup-bun@v1
85+
with:
86+
bun-version: latest
87+
- uses: pnpm/action-setup@v4
88+
with:
89+
version: 10
90+
- run: deno task test:bun
91+
working-directory: ${{ github.workspace }}/fedify/
92+
7093
lint:
7194
runs-on: ubuntu-latest
7295
steps:
@@ -106,7 +129,7 @@ jobs:
106129
working-directory: ${{ github.workspace }}/cli/
107130

108131
publish:
109-
needs: [test, test-node, lint, release-test]
132+
needs: [test, test-node, test-bun, lint, release-test]
110133
runs-on: ubuntu-latest
111134
permissions:
112135
id-token: write

fedify/deno.json

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,38 @@
9797
"codegen"
9898
]
9999
},
100-
"npm": "pnpm install && pnpm pack",
101-
"test:node": "pnpm install && pnpm test",
100+
"pnpm:install": "pnpm install",
101+
"pnpm:build": {
102+
"command": "node_modules/.bin/tsdown",
103+
"dependencies": [
104+
"codegen",
105+
"pnpm:install"
106+
]
107+
},
108+
"npm": {
109+
"command": "pnpm pack",
110+
"dependencies": [
111+
"pnpm:install"
112+
]
113+
},
114+
"test:node": {
115+
"command": "cd dist/ && node --test",
116+
"dependencies": [
117+
"pnpm:build"
118+
]
119+
},
120+
"test:bun": {
121+
"command": "cd dist/ && bun test",
122+
"dependencies": [
123+
"pnpm:build"
124+
]
125+
},
102126
"test-all": {
103127
"dependencies": [
104128
"check",
105129
"test",
106-
"test:node"
130+
"test:node",
131+
"test:bun"
107132
]
108133
}
109134
}

fedify/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@
8787
"@opentelemetry/api": "^1.9.0",
8888
"@opentelemetry/semantic-conventions": "^1.27.0",
8989
"@phensley/language-tag": "^1.9.0",
90-
"@types/bun": "^1.2.14",
9190
"asn1js": "^3.0.5",
9291
"byte-encodings": "^1.0.11",
9392
"json-canon": "^1.0.1",
@@ -114,6 +113,7 @@
114113
"build": "deno task codegen && tsdown",
115114
"prepack": "deno task codegen && tsdown",
116115
"prepublish": "deno task codegen && tsdown",
117-
"test": "deno task codegen && tsdown && cd dist/ && node --test"
116+
"test": "deno task codegen && tsdown && cd dist/ && node --test",
117+
"test:bun": "deno task codegen && tsdown && cd dist/ && bun test"
118118
}
119119
}

fedify/pnpm-lock.yaml

Lines changed: 0 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

fedify/testing/mod.ts

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,63 @@ export function test(
6868
}
6969
},
7070
});
71+
} else if ("Bun" in globalThis) {
72+
let failed: unknown = undefined;
73+
// deno-lint-ignore no-inner-declarations
74+
function step(def: Deno.TestStepDefinition): Promise<boolean>;
75+
// deno-lint-ignore no-inner-declarations
76+
function step(
77+
name: string,
78+
fn: (ctx: Deno.TestContext) => void | Promise<void>,
79+
): Promise<boolean>;
80+
// deno-lint-ignore no-inner-declarations
81+
function step(
82+
fn: (ctx: Deno.TestContext) => void | Promise<void>,
83+
): Promise<boolean>;
84+
// deno-lint-ignore no-inner-declarations
85+
async function step(
86+
defOrNameOrFn:
87+
| Deno.TestStepDefinition
88+
| string
89+
| ((ctx: Deno.TestContext) => void | Promise<void>),
90+
fn?: (ctx: Deno.TestContext) => void | Promise<void>,
91+
): Promise<boolean> {
92+
let def: Deno.TestStepDefinition;
93+
if (typeof defOrNameOrFn === "string") {
94+
def = { name: defOrNameOrFn, fn: fn! };
95+
} else if (typeof defOrNameOrFn === "function") {
96+
def = { name: defOrNameOrFn.name, fn: defOrNameOrFn };
97+
} else {
98+
def = defOrNameOrFn;
99+
}
100+
if (def.ignore) return true;
101+
try {
102+
await def.fn({
103+
name: def.name,
104+
origin: "",
105+
step,
106+
});
107+
} catch (e) {
108+
failed ??= e;
109+
return false;
110+
}
111+
return true;
112+
}
113+
const ctx: Deno.TestContext = {
114+
name: def.name,
115+
origin: "",
116+
step,
117+
};
118+
// deno-lint-ignore no-inner-declarations
119+
async function fn() {
120+
await def.fn(ctx);
121+
if (failed) throw failed;
122+
}
123+
// @ts-ignore: Bun exists in the global scope in Bun
124+
const bunTest = Bun.jest(caller()).test;
125+
if (def.ignore) bunTest.skip(def.name, fn);
126+
else if (def.only) bunTest.only(def.name, fn);
127+
else bunTest(def.name, fn);
71128
} else {
72129
nodeTest(def.name, { only: def.only, skip: def.ignore }, async (t) => {
73130
await def.fn(intoDenoTestContext(def.name, t));
@@ -120,3 +177,21 @@ function intoDenoTestContext(
120177
};
121178
return denoCtx;
122179
}
180+
181+
// Below code is borrowed from https://github.com/oven-sh/bun/issues/11660#issuecomment-2506832106
182+
183+
/** Retrieve caller test file. */
184+
function caller() {
185+
const Trace = Error as unknown as {
186+
prepareStackTrace: (error: Error, stack: CallSite[]) => unknown;
187+
};
188+
const _ = Trace.prepareStackTrace;
189+
Trace.prepareStackTrace = (_, stack) => stack;
190+
const { stack } = new Error();
191+
Trace.prepareStackTrace = _;
192+
const caller = (stack as unknown as CallSite[])[2];
193+
return caller.getFileName().replaceAll("\\", "/");
194+
}
195+
196+
/** V8 CallSite (subset). */
197+
type CallSite = { getFileName: () => string };

fedify/tsdown.config.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ export default defineConfig({
2929
warning.exporter === "@std/testing/snapshot"
3030
) {
3131
return;
32+
} else if (
33+
warning.code === "UNRESOLVED_IMPORT" &&
34+
warning.id?.endsWith("testing/mod.ts") &&
35+
warning.exporter === "bun:test"
36+
) {
37+
return;
3238
}
3339
defaultHandler(warning);
3440
},

0 commit comments

Comments
 (0)