| name | bun-test-basics |
| description | Use for bun:test syntax, assertions, describe/it, test.skip/only/each, and basic patterns. |
| metadata | {"version":"1.0.0"} |
| license | MIT |
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
bun test
bun test ./test/math.test.ts
bun test --test-name-pattern "addition"
Writing Tests
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
test.skip("not ready", () => {
});
test.only("focus on this", () => {
});
test.todo("implement later");
test.failing("known bug", () => {
throw new Error("This is expected");
});
Parameterized Tests
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);
});
Concurrent Tests
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", () => {
});
Common Matchers
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();
(str).();
(arr).(item);
(arr).({ : });
(arr).();
(obj).();
(obj).(, value);
(obj).({ : });
( ()).();
( ()).();
( ()).();
(promise)..(value);
(promise)..();
(value)..();
CLI Options
bun test --timeout 20
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"
Output Reporters
bun test --dots
bun test --reporter=junit --reporter-outfile=./results.xml
Common Errors
| 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 | Check 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