Use for test lifecycle hooks: beforeAll, afterAll, beforeEach, afterEach, fixtures, preload.
Provides Bun test lifecycle hooks (beforeAll, afterAll, beforeEach, afterEach) for setup and teardown. Use when writing tests needing resource management, database connections, or server setup between tests.
/plugin marketplace add secondsky/claude-skills/plugin install bun@claude-skillsThis skill inherits all available tools. When active, it can use any tool Claude has access to.
Bun supports Jest-compatible lifecycle hooks for test setup and teardown.
import { test, expect, beforeAll, afterAll, beforeEach, afterEach } from "bun:test";
let db: Database;
let testData: any;
// Runs once before all tests in file
beforeAll(async () => {
db = await Database.connect();
});
// Runs once after all tests in file
afterAll(async () => {
await db.close();
});
// Runs before each test
beforeEach(() => {
testData = { id: 1, name: "test" };
});
// Runs after each test
afterEach(() => {
testData = null;
});
test("example", () => {
expect(testData.name).toBe("test");
});
beforeAll
├── beforeEach
│ └── test 1
│ └── afterEach
├── beforeEach
│ └── test 2
│ └── afterEach
afterAll
import { describe, test, beforeAll, beforeEach, afterEach, afterAll } from "bun:test";
describe("outer", () => {
beforeAll(() => console.log("1. outer beforeAll"));
afterAll(() => console.log("6. outer afterAll"));
beforeEach(() => console.log("2. outer beforeEach"));
afterEach(() => console.log("5. outer afterEach"));
describe("inner", () => {
beforeEach(() => console.log("3. inner beforeEach"));
afterEach(() => console.log("4. inner afterEach"));
test("test", () => {
console.log("test runs here");
});
});
});
// Output:
// 1. outer beforeAll
// 2. outer beforeEach
// 3. inner beforeEach
// test runs here
// 4. inner afterEach
// 5. outer afterEach
// 6. outer afterAll
beforeAll(async () => {
await setupDatabase();
});
beforeEach(async () => {
await seedTestData();
});
afterEach(async () => {
await clearTestData();
});
afterAll(async () => {
await teardownDatabase();
});
// Set timeout for slow setup
beforeAll(async () => {
await slowSetup();
}, 30000); // 30 seconds
Use --preload for global setup across all test files:
bun test --preload ./setup.ts
import { beforeAll, afterAll } from "bun:test";
// Global setup
beforeAll(() => {
console.log("Global setup runs before all test files");
});
// Global teardown
afterAll(() => {
console.log("Global teardown runs after all test files");
});
// Set global variables
globalThis.testConfig = {
apiUrl: "http://localhost:3000",
};
[test]
preload = ["./test/setup.ts"]
import { beforeAll, afterAll, beforeEach, afterEach } from "bun:test";
let db: Database;
beforeAll(async () => {
db = await Database.connect(process.env.TEST_DB_URL);
await db.migrate();
});
afterAll(async () => {
await db.close();
});
beforeEach(async () => {
await db.beginTransaction();
});
afterEach(async () => {
await db.rollback(); // Reset state
});
import { beforeAll, afterAll } from "bun:test";
let server: Server;
let baseUrl: string;
beforeAll(async () => {
server = Bun.serve({
port: 0, // Random available port
fetch: app.fetch,
});
baseUrl = `http://localhost:${server.port}`;
});
afterAll(() => {
server.stop();
});
test("api works", async () => {
const res = await fetch(`${baseUrl}/api/health`);
expect(res.ok).toBe(true);
});
import { beforeEach, afterEach, spyOn } from "bun:test";
let fetchSpy: ReturnType<typeof spyOn>;
beforeEach(() => {
fetchSpy = spyOn(global, "fetch").mockResolvedValue(
new Response(JSON.stringify({ ok: true }))
);
});
afterEach(() => {
fetchSpy.mockRestore();
});
import { beforeAll, afterAll } from "bun:test";
const originalEnv = process.env;
beforeAll(() => {
process.env = {
...originalEnv,
NODE_ENV: "test",
API_KEY: "test-key",
};
});
afterAll(() => {
process.env = originalEnv;
});
// fixtures.ts
export async function createTestUser() {
return { id: 1, name: "Test User" };
}
export async function cleanupTestUser(user: any) {
// cleanup logic
}
// test file
import { createTestUser, cleanupTestUser } from "./fixtures";
let user: any;
beforeEach(async () => {
user = await createTestUser();
});
afterEach(async () => {
await cleanupTestUser(user);
});
If a hook throws, all tests in that describe block fail:
beforeAll(() => {
throw new Error("Setup failed");
});
// All tests in this file will fail
test("will fail", () => {
expect(true).toBe(true);
});
| Error | Cause | Fix |
|---|---|---|
beforeAll timeout | Slow async setup | Increase timeout |
Hook not called | Wrong scope | Check hook placement |
Cleanup not run | afterAll skipped | Ensure no throws in tests |
State leak | Missing cleanup | Add proper afterEach |
Load references/preload-scripts.md when:
Load references/fixtures.md when:
Use when working with Payload CMS projects (payload.config.ts, collections, fields, hooks, access control, Payload API). Use when debugging validation errors, security issues, relationship queries, transactions, or hook behavior.
Applies Anthropic's official brand colors and typography to any sort of artifact that may benefit from having Anthropic's look-and-feel. Use it when brand colors or style guidelines, visual formatting, or company design standards apply.
Creating algorithmic art using p5.js with seeded randomness and interactive parameter exploration. Use this when users request creating art using code, generative art, algorithmic art, flow fields, or particle systems. Create original algorithmic art rather than copying existing artists' work to avoid copyright violations.