# Write Unit Tests Prompt (v1)
## π― Goal
Create high-quality unit tests that effectively validate functionality and improve code coverage.
## π₯ Context (ask if missing)
1. **Test Plan** β prioritized list of functions to test, testing approach
2. **Target Functions** β specific modules/functions requiring test coverage
3. **Test Environment** β framework setup, testing utilities available
4. **Existing Test Patterns** β current conventions, mocking strategies
## π¦ Skip if
- All planned tests already implemented or emergency fixes that don't require testing
## π Checklist
- **Test Structure & Organization**
- [ ] Create test files following project conventions
- [ ] Group related tests in logical describe blocks
- [ ] Use clear, descriptive test names
- [ ] Follow Arrange-Act-Assert (AAA) pattern
- **Test Coverage Implementation**
- [ ] Test happy path scenarios
- [ ] Cover edge cases and boundary conditions
- [ ] Test error handling and exception scenarios
- [ ] Validate input/output transformations
- **Mocking & Isolation**
- [ ] Mock external dependencies appropriately
- [ ] Use dependency injection for testability
- [ ] Isolate units from side effects
- [ ] Mock asynchronous operations properly
- **Test Quality & Maintainability**
- [ ] Make tests readable and self-documenting
- [ ] Avoid test interdependencies
- [ ] Use appropriate assertions
- [ ] Keep tests focused and atomic
## π€ Output
**Test Files:** Following project structure (e.g., `__tests__/`, `*.test.js`, `*.spec.ts`)
Each test file should include:
1. **Setup/Teardown** β proper test environment preparation
2. **Test Cases** β comprehensive coverage of functionality
3. **Mocks/Stubs** β isolated external dependencies
4. **Assertions** β clear validation of expected behavior
5. **Documentation** β comments for complex test logic
## π§ͺ Test Types & Patterns
- **Pure Function Tests**: Input β Function β Expected Output
- **State Change Tests**: Initial State β Action β Expected State
- **Error Handling Tests**: Invalid Input β Function β Expected Error
- **Async Tests**: Promise/Callback β Function β Resolved Value
- **Mock Integration Tests**: Service β Mocked Dependency β Expected Interaction
## β‘ Best Practices
- **Test Naming**: `should [expected behavior] when [condition]`
- **Test Data**: Use descriptive test data, avoid magic numbers
- **Assertions**: Use specific assertions (toBe, toEqual, toContain)
- **Performance**: Keep tests fast, avoid unnecessary delays
- **Reliability**: Make tests deterministic and repeatable
## β‘οΈ Response Flow
Select function β Analyze behavior β Write test cases β Implement mocks β Validate coverage