npx skills add https://github.com/secondsky/claude-skills --skill 'Bun Test Basics'Bun 内置了一个快速、兼容 Jest 的测试运行器。测试使用 Bun 运行时执行,并原生支持 TypeScript/JSX。
# 运行所有测试
bun test
# 运行特定文件
bun test ./test/math.test.ts
# 运行匹配模式的测试
bun test --test-name-pattern "addition"
import { test, expect, describe } from "bun:test";
test("2 + 2", () => {
expect(2 + 2).toBe(4);
});
describe("math", () => {
test("addition", () => {
expect(1 + 1).toBe(2);
});
test("subtraction", () => {
expect(5 - 3).toBe(2);
});
});
Bun 会查找匹配以下模式的测试文件:
*.test.{js|jsx|ts|tsx}*_test.{js|jsx|ts|tsx}*.spec.{js|jsx|ts|tsx}*_spec.{js|jsx|ts|tsx}广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
// 跳过测试
test.skip("not ready", () => {
// 不会运行
});
// 仅运行此测试
test.only("focus on this", () => {
// 其他测试不会运行
});
// 未来测试的占位符
test.todo("implement later");
// 预期会失败
test.failing("known bug", () => {
throw new Error("This is expected");
});
test.each([
[1, 1, 2],
[2, 2, 4],
[3, 3, 6],
])("add(%i, %i) = %i", (a, b, expected) => {
expect(a + b).toBe(expected);
});
// 使用对象
test.each([
{ a: 1, b: 2, expected: 3 },
{ a: 5, b: 5, expected: 10 },
])("add($a, $b) = $expected", ({ a, b, expected }) => {
expect(a + b).toBe(expected);
});
// 并行运行测试
test.concurrent("async test 1", async () => {
await fetch("/api/1");
});
test.concurrent("async test 2", async () => {
await fetch("/api/2");
});
// 使用 --concurrent 时强制顺序执行
test.serial("must run alone", () => {
// 顺序运行
});
// 相等性
expect(value).toBe(4); // 严格相等
expect(obj).toEqual({ a: 1 }); // 深度相等
expect(value).toStrictEqual(4); // 严格 + 类型
// 真值性
expect(value).toBeTruthy();
expect(value).toBeFalsy();
expect(value).toBeNull();
expect(value).toBeDefined();
expect(value).toBeUndefined();
// 数字
expect(value).toBeGreaterThan(3);
expect(value).toBeGreaterThanOrEqual(3);
expect(value).toBeLessThan(5);
expect(value).toBeCloseTo(0.3, 5); // 浮点数
// 字符串
expect(str).toMatch(/pattern/);
expect(str).toContain("substring");
expect(str).toStartWith("Hello");
expect(str).toEndWith("world");
// 数组
expect(arr).toContain(item);
expect(arr).toContainEqual({ a: 1 });
expect(arr).toHaveLength(3);
// 对象
expect(obj).toHaveProperty("key");
expect(obj).toHaveProperty("key", value);
expect(obj).toMatchObject({ a: 1 });
// 异常
expect(() => fn()).toThrow();
expect(() => fn()).toThrow("message");
expect(() => fn()).toThrow(CustomError);
// 异步
await expect(promise).resolves.toBe(value);
await expect(promise).rejects.toThrow();
// 否定
expect(value).not.toBe(5);
# 每个测试的超时时间(默认 5000 毫秒)
bun test --timeout 20
# 在 N 次失败后停止
bun test --bail
bun test --bail=10
# 监视模式
bun test --watch
# 随机顺序
bun test --randomize
bun test --seed 12345
# 并发执行
bun test --concurrent
bun test --concurrent --max-concurrency 4
# 按名称过滤
bun test -t "pattern"
# 点状(紧凑)
bun test --dots
# JUnit XML(CI/CD)
bun test --reporter=junit --reporter-outfile=./results.xml
| 错误 | 原因 | 解决方法 |
|---|---|---|
测试超时 | 测试超过 5 秒 | 使用 --timeout 或优化代码 |
未找到测试 | 文件模式错误 | 检查文件命名 |
expect 未定义 | 缺少导入 | 从 bun:test 导入 |
断言失败 | 测试失败 | 检查预期值与实际值 |
在以下情况下加载 references/matchers.md:
在以下情况下加载 references/cli-options.md:
每周安装次数
–
代码仓库
GitHub 星标数
90
首次出现时间
–
安全审计
Bun ships with a fast, built-in, Jest-compatible test runner. Tests run with the Bun runtime and support TypeScript/JSX natively.
# Run all tests
bun test
# Run specific file
bun test ./test/math.test.ts
# Run tests matching pattern
bun test --test-name-pattern "addition"
import { test, expect, describe } from "bun:test";
test("2 + 2", () => {
expect(2 + 2).toBe(4);
});
describe("math", () => {
test("addition", () => {
expect(1 + 1).toBe(2);
});
test("subtraction", () => {
expect(5 - 3).toBe(2);
});
});
Bun discovers test files matching:
*.test.{js|jsx|ts|tsx}*_test.{js|jsx|ts|tsx}*.spec.{js|jsx|ts|tsx}*_spec.{js|jsx|ts|tsx}// Skip a test
test.skip("not ready", () => {
// won't run
});
// Only run this test
test.only("focus on this", () => {
// other tests won't run
});
// Placeholder for future test
test.todo("implement later");
// Expected to fail
test.failing("known bug", () => {
throw new Error("This is expected");
});
test.each([
[1, 1, 2],
[2, 2, 4],
[3, 3, 6],
])("add(%i, %i) = %i", (a, b, expected) => {
expect(a + b).toBe(expected);
});
// With objects
test.each([
{ a: 1, b: 2, expected: 3 },
{ a: 5, b: 5, expected: 10 },
])("add($a, $b) = $expected", ({ a, b, expected }) => {
expect(a + b).toBe(expected);
});
// Run tests in parallel
test.concurrent("async test 1", async () => {
await fetch("/api/1");
});
test.concurrent("async test 2", async () => {
await fetch("/api/2");
});
// Force sequential when using --concurrent
test.serial("must run alone", () => {
// runs sequentially
});
// Equality
expect(value).toBe(4); // Strict equality
expect(obj).toEqual({ a: 1 }); // Deep equality
expect(value).toStrictEqual(4); // Strict + type
// Truthiness
expect(value).toBeTruthy();
expect(value).toBeFalsy();
expect(value).toBeNull();
expect(value).toBeDefined();
expect(value).toBeUndefined();
// Numbers
expect(value).toBeGreaterThan(3);
expect(value).toBeGreaterThanOrEqual(3);
expect(value).toBeLessThan(5);
expect(value).toBeCloseTo(0.3, 5); // Floating point
// Strings
expect(str).toMatch(/pattern/);
expect(str).toContain("substring");
expect(str).toStartWith("Hello");
expect(str).toEndWith("world");
// Arrays
expect(arr).toContain(item);
expect(arr).toContainEqual({ a: 1 });
expect(arr).toHaveLength(3);
// Objects
expect(obj).toHaveProperty("key");
expect(obj).toHaveProperty("key", value);
expect(obj).toMatchObject({ a: 1 });
// Exceptions
expect(() => fn()).toThrow();
expect(() => fn()).toThrow("message");
expect(() => fn()).toThrow(CustomError);
// Async
await expect(promise).resolves.toBe(value);
await expect(promise).rejects.toThrow();
// Negation
expect(value).not.toBe(5);
# Timeout per test (default 5000ms)
bun test --timeout 20
# Bail after N failures
bun test --bail
bun test --bail=10
# Watch mode
bun test --watch
# Random order
bun test --randomize
bun test --seed 12345
# Concurrent execution
bun test --concurrent
bun test --concurrent --max-concurrency 4
# Filter by name
bun test -t "pattern"
# Dots (compact)
bun test --dots
# JUnit XML (CI/CD)
bun test --reporter=junit --reporter-outfile=./results.xml
| Error | Cause | Fix |
|---|---|---|
Test timeout | Test exceeds 5s | Use --timeout or optimize |
No tests found | Wrong file pattern | Check file naming |
expect is not defined | Missing import | Import from bun:test |
Assertion failed | Test failure |
Load references/matchers.md when:
Load references/cli-options.md when:
Weekly Installs
–
Repository
GitHub Stars
90
First Seen
–
Security Audits
Vue 3 调试指南:解决响应式、计算属性与监听器常见错误
10,500 周安装
| Check expected vs actual |