mcpField
B
bun-test-basics

bun-test-basics

github.com/secondsky测试与质量240465 次安装5 周前更新
代码仓库

关于 bun-test-basics

bun-test-basics 是使用 Bun 内置 Jest 兼容测试运行器编写和运行测试的技能,涵盖测试发现、断言、执行选项、并发及参数化测试和报告。

主要功能

  1. 01使用 bun test 运行测试
  2. 02支持 TypeScript 和 JSX 测试
  3. 03测试文件发现规则
  4. 04参数化与并发测试
  5. 05命令行选项与报告器

适用场景

  1. 01为 Bun 项目添加测试
  2. 02按文件或名称运行测试
  3. 03为 CI 生成 JUnit 报告

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/secondsky/claude-skills --skill bun-test-basics

安装后在 Claude Code 中输入/bun-test-basics确认已加载。

详细介绍

Bun 测试基础

Bun 内置了快速且兼容 Jest 的测试运行器。测试在 Bun 运行时中运行,并原生支持 TypeScript/JSX。

快速开始

# 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 会发现符合以下模式的测试文件:

  • *.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
});
// NOTE: in Bun 1.3+ `bun test` exits non-zero in CI when a file uses
// `test.only`. Use it for local debugging only — never commit it.

// 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);

CLI 选项

# 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

常见错误

错误原因修复方法
Test timeout测试超过 5s使用 --timeout 或优化测试
No tests found文件模式错误检查文件命名
expect is not defined缺少导入从 bun:test 导入
Assertion failed测试失败检查预期值与实际值

何时加载参考资料

在以下情况下加载 references/matchers.md:

  • 需要完整的匹配器参考
  • 自定义匹配器模式

在以下情况下加载 references/cli-options.md:

  • 需要完整的 CLI 标志参考
  • 高级执行选项

评论

支持 Markdown