Unit Testing Skill
When to use
- •When the user asks to "write tests", "add unit tests", "test this function".
- •When creating new modules that require verification.
Instructions
1. Test Runner
- •Use Bun's built-in test runner (
bun test). - •Do NOT install Jest or Mocha unless explicitly requested.
2. File Conventions
- •Place tests in the
test/directory or alongside source files (e.g.,src/foo.test.ts) depending on project structure. - •Naming convention:
*.test.tsor*.spec.ts.
3. Syntax & Imports
- •Import test utilities from
bun:test:typescriptimport { describe, it, expect, beforeEach, afterEach } from 'bun:test'; - •Use
describeblocks to group tests by function or module. - •Use
itortestfor individual test cases.
4. Mocking
- •Use
mockfrombun:testfor function mocking.typescriptimport { mock } from 'bun:test'; const myMock = mock(() => 'value'); - •Reset mocks in
afterEachif necessary.
5. Execution
- •Run all tests:
bun test - •Run specific file:
bun test path/to/file.test.ts - •Run/Watch mode:
bun test --watch
6. Best Practices
- •Isolation: Tests should not depend on each other.
- •Clarity: Test names should describe the expected behavior (e.g.,
it('should return 404 when user not found')). - •Coverage: Aim to test both success and failure paths.