bun-testing
Bun Testing Skill
Quick Reference
- Framework: bun:test
- File pattern:
*.test.tsinside__tests__directories - Module mocking: Use
ModuleMockerfrom@/__tests__(see patterns) - Coverage target: 60–80% (focus on important logic, not 100%)
- Config:
.env.testfor test environment variables
Test Utilities (src/tests/)
Before writing custom test helpers, check existing utilities:
createTestApp(basePath, route, middleware[])- Creates test Hono app with error handler, logger, and optional middlewareModuleMocker(import.meta.url)- Module mocking utility (see mocking patterns)post(app, url, body, headers)- POST request helperget(app, url, headers)- GET request helperdoRequest(app, url, method, body, headers)- Generic request helper
Example:
import { createTestApp, post } from '@/__tests__'
const app = createTestApp('/api/v1/auth', signupRoute, [captchaMiddleware()])
const response = await post(app, '/api/v1/auth/signup', {
email: 'test@example.com',
password: 'SecurePass123!'
})
Test Types
- Unit tests: Mock all dependencies (repositories, services, APIs)
- Integration tests: Use real database, mock external APIs only
- Endpoint tests: Use
createTestApp()with mocked services
Test Priorities
- Correct scenario(s)
- Error handling
- Boundary inputs
- Failure scenarios
Environment Setup
- Use
.env.testfor test-specific variables - Bun handles env loading natively — no manual dotenv needed
- Minimize mocking
@/env— only mock for special/invalid configs
Mocking Strategy
- Mock only business logic dependencies (repositories, external APIs)
- Use global mocks for shared services (CAPTCHA, email) — don't redefine per test
- No real database or network calls — all I/O must be mocked
- Don't mock encapsulated dependencies — mock the public API/wrapper only
Type Safety
- Minimize
any— prefer proper TypeScript types - Use type inference when possible
- Use
Partial<T>for mock objects - Exception: Use
anyonly for complex mocks where full typing adds unnecessary complexity
Test Structure
Use arrange → act → assert pattern with descriptive test names:
describe('UserService', () => {
it('should return user when found by email', async () => {
// Arrange
const mockUser = { id: 1, email: 'test@example.com' }
mockUserRepo.findByEmail.mockResolvedValue(mockUser)
// Act
const result = await userService.findByEmail('test@example.com')
// Assert
expect(result).toEqual(mockUser)
})
})
Mocking Patterns
For detailed mocking patterns including variable ordering, beforeEach setup, and ModuleMocker usage, see references/mocking-patterns.md.
Cleanup
Always clean up side effects after each test:
afterEach(async () => {
await moduleMocker.clear() // restore mocked modules
vi.clearAllMocks() // or mock.mockClear() for individual mocks
})
More from diegosouzapw/awesome-omni-skill
music-assistant
Control Home Assistant Music Assistant - browse library, search, play, manage preferences and moods.
12agent-code-generator
Generates Agent definitions (.md files) based on user intent and standard templates.
6terragrunt-generator
Comprehensive toolkit for generating best practice Terragrunt configurations (HCL files) following current standards and conventions. Use this skill when creating new Terragrunt resources (root configs, child modules, stacks, environment setups), or building multi-environment Terragrunt projects.
6api contract sync manager
Validate OpenAPI, Swagger, and GraphQL schemas match backend implementation. Detect breaking changes, generate TypeScript clients, and ensure API documentation stays synchronized. Use when working with API spec files (.yaml, .json, .graphql), reviewing API changes, generating frontend types, or validating endpoint implementations.
5upstash/workflow typescript sdk skill
Lightweight guidance for using the Upstash Workflow SDK to define, trigger, and manage workflows. Use this Skill whenever a user wants to create workflow endpoints, run steps, or interact with the Upstash Workflow client.
5upstash/search typescript sdk
Entry point for documentation skills covering Upstash Search quick starts, core concepts, and TypeScript SDK usage. Use when a user asks how to get started, how indexing works, or how to use the TS client.
5