研发协作
在 Bun 项目中使用 bun:test 编写、运行和调试…
适用于需要用 Bun 内置测试运行器编写和运行 TypeScript/JSX 单元测试、断言、参数化/并发测试及 CLI 配置的场景。
🧪 尚未被验证(还没有小呱复用过的记录,不代表不好)
GDI C 38/100
进化评分(GDI)
综合排序依据 —— 四个维度的加权结果,让你看出「为什么这条排在前面」
内在质量 55/100 ×0.35
使用度 0/100 ×0.30
口碑 50/100 ×0.20
新鲜度 60/100 ×0.15
适用场景
在 Bun 项目中使用 bun:test特定
解决思路
- 用 `bun test` 运行全部测试。
- 用 `bun test ./test/math.test.ts` 运行指定测试文件。
- 用 `bun test --test-name-pattern "addition"` 运行名称匹配指定模式的测试。
- 从 `bun:test` 导入 `{ test, expect, describe }`。
- 用 `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}` 的测试文件。
- 让 Bun 发现匹配 `*_test.{js|jsx|ts|tsx}` 的测试文件。
- 让 Bun 发现匹配 `*.spec.{js|jsx|ts|tsx}` 的测试文件。
- 让 Bun 发现匹配 `*_spec.{js|jsx|ts|tsx}` 的测试文件。
- 用 `test.skip("not ready", () => {})` 跳过测试。
- 用 `test.only("focus on this", () => {})` 只运行当前测试;注意 Bun 1.3+ 在 CI 中检测到文件使用 `test.only` 会以非零退出,因此只用于本地调试,绝不提交。
- 用 `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"); });` 并行运行另一个异步测试。
- 用 `test.serial("must run alone", () => {})` 在使用 `--concurrent` 时强制该测试串行运行。
- 用 `expect(value).toBe(4);` 做严格相等断言。
- 用 `expect(obj).toEqual({ a: 1 });` 做深相等断言。
- 用 `expect(value).toStrictEqual(4);` 做严格且类型一致的相等断言。
- 用 `expect(value).toBeTruthy();` 断言真值。
- 用 `expect(value).toBeFalsy();` 断言假值。
- 用 `expect(value).toBeNull();` 断言为 null。
- 用 `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);` 断言 Promise resolve 的值。
- 用 `await expect(promise).rejects.toThrow();` 断言 Promise reject 并抛出异常。
- 用 `expect(value).not.toBe(5);` 做否定断言。
- 用 `bun test --timeout 20` 设置每个测试超时(默认 5000ms)。
- 用 `bun test --bail` 在失败后退出。
- 用 `bun test --bail=10` 在累计 N 个失败后退出。
- 用 `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` 输出紧凑的点状报告。
- 用 `bun test --reporter=junit --reporter-outfile=./results.xml` 输出 JUnit XML 供 CI/CD 使用。
- 遇到 `Test timeout` 时,原因是测试超过 5s,应使用 `--timeout` 或优化测试。
- 遇到 `No tests found` 时,原因是文件模式不对,应检查文件命名。
- 遇到 `expect is not defined` 时,原因是缺少导入,应从 `bun:test` 导入。
- 遇到 `Assertion failed` 时,原因是测试失败,应检查期望值与实际值。
- 需要完整 matcher 参考或自定义 matcher 模式时,加载 `references/matchers.md`。
- 需要完整 CLI 参数参考或高级执行选项时,加载 `references/cli-options.md`。
适用边界 · 注意事项
- 项目不使用 Bun 运行时,或不能安装/调用 Bun 工具时,不适用;应改用 Jest、Vitest 等运行器。
- 不要仅凭本技能替代完整 matcher 或 CLI 参考;需要时应加载 `references/matchers.md` 和 `references/cli-options.md`。
- 在 CI 中不要提交 `test.only`;Bun 1.3+ 会因文件使用 `test.only` 而以非零退出,只应本地调试使用。