Bun Test Basics

secondsky/claude-skills/plugins/bun/skills/bun-test-basics

作者 secondsky88378361314fMIT227 個星標收錄於 2026年10月8日更新於 2026年10月8日儲存庫10 天前更新

Use for bun:test syntax, assertions, describe/it, test.skip/only/each, and basic patterns.

AI 產生的概覽

Bun 內建測試執行器的參考指南:語法、斷言、修飾詞與 CLI 選項。

功能
此技能提供使用 Bun 內建、相容於 Jest 的測試執行器來撰寫與執行測試的參考文件。內容涵蓋測試檔案命名模式、describe/it 結構、skip、only、todo、failing 等測試修飾詞、參數化與並行測試、常用匹配器清單、CLI 選項、輸出報告器,以及常見錯誤的修正方式。它產出的是指引說明與程式碼範例,而非可執行的成品。
適用情境
在撰寫或審查由 Bun 執行的測試,且需要正確的語法、匹配器或 CLI 參數時使用。也適合用來排查 bun test 的常見錯誤,例如逾時或缺少匯入。
執行需求
執行所描述的測試需要 Bun 執行環境。此技能未附帶任何指令碼或資源,僅有說明與範例。

Bun Test Basics

Bun ships with a fast, built-in, Jest-compatible test runner. Tests run with the Bun runtime and support TypeScript/JSX natively.

Quick Start

bash
# Run all testsbun test
# Run specific filebun test ./test/math.test.ts
# Run tests matching patternbun test --test-name-pattern "addition"

Writing Tests

typescript
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);  });});

Test File Patterns

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}

Test Modifiers

typescript
// Skip a testtest.skip("not ready", () => {  // won't run});
// Only run this testtest.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 testtest.todo("implement later");
// Expected to failtest.failing("known bug", () => {  throw new Error("This is expected");});

Parameterized Tests

typescript
test.each([  [1, 1, 2],  [2, 2, 4],  [3, 3, 6],])("add(%i, %i) = %i", (a, b, expected) => {  expect(a + b).toBe(expected);});
// With objectstest.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);});

Concurrent Tests

typescript
// Run tests in paralleltest.concurrent("async test 1", async () => {  await fetch("/api/1");});
test.concurrent("async test 2", async () => {  await fetch("/api/2");});
// Force sequential when using --concurrenttest.serial("must run alone", () => {  // runs sequentially});

Common Matchers

typescript
// Equalityexpect(value).toBe(4);           // Strict equalityexpect(obj).toEqual({ a: 1 });   // Deep equalityexpect(value).toStrictEqual(4);  // Strict + type
// Truthinessexpect(value).toBeTruthy();expect(value).toBeFalsy();expect(value).toBeNull();expect(value).toBeDefined();expect(value).toBeUndefined();
// Numbersexpect(value).toBeGreaterThan(3);expect(value).toBeGreaterThanOrEqual(3);expect(value).toBeLessThan(5);expect(value).toBeCloseTo(0.3, 5);  // Floating point
// Stringsexpect(str).toMatch(/pattern/);expect(str).toContain("substring");expect(str).toStartWith("Hello");expect(str).toEndWith("world");
// Arraysexpect(arr).toContain(item);expect(arr).toContainEqual({ a: 1 });expect(arr).toHaveLength(3);
// Objectsexpect(obj).toHaveProperty("key");expect(obj).toHaveProperty("key", value);expect(obj).toMatchObject({ a: 1 });
// Exceptionsexpect(() => fn()).toThrow();expect(() => fn()).toThrow("message");expect(() => fn()).toThrow(CustomError);
// Asyncawait expect(promise).resolves.toBe(value);await expect(promise).rejects.toThrow();
// Negationexpect(value).not.toBe(5);

CLI Options

bash
# Timeout per test (default 5000ms)bun test --timeout 20
# Bail after N failuresbun test --bailbun test --bail=10
# Watch modebun test --watch
# Random orderbun test --randomizebun test --seed 12345
# Concurrent executionbun test --concurrentbun test --concurrent --max-concurrency 4
# Filter by namebun test -t "pattern"

Output Reporters

bash
# Dots (compact)bun test --dots
# JUnit XML (CI/CD)bun test --reporter=junit --reporter-outfile=./results.xml

Common Errors

ErrorCauseFix
Test timeoutTest exceeds 5sUse --timeout or optimize
No tests foundWrong file patternCheck file naming
expect is not definedMissing importImport from bun:test
Assertion failedTest failureCheck expected vs actual

When to Load References

Load references/matchers.md when:

  • Need complete matcher reference
  • Custom matcher patterns

Load references/cli-options.md when:

  • Full CLI flag reference
  • Advanced execution options

來源與署名

來源:secondsky/claude-skills位於plugins/bun/skills/bun-test-basics提交8837836

授權條款: MIT

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架