---
name: testing
source: botcore
description: >
  Write unit and E2E tests following best practices.
  Use when adding tests for new features, fixing bugs with regression tests, or improving coverage.
version: "2.1.0"
triggers:
  - test
  - coverage
  - unit test
  - E2E
  - TDD
  - regression
---

# Testing

Best practices for unit, integration, and E2E testing.

## When to Use

- Adding new components or features
- Fixing bugs (add regression test first)
- Improving test coverage
- Validating integration points

## Workflow

1. **Check coverage** — Run coverage report
2. **Identify gaps** — Find uncovered code paths
3. **Write tests** — Follow patterns in references
4. **Verify** — Ensure tests pass

## Test Structure (AAA Pattern)

```typescript
import { describe, it, expect, beforeEach } from 'vitest';

describe('MyComponent', () => {
  beforeEach(() => {
    // Setup - reset state
  });
  
  it('should render correctly', () => {
    // Arrange - set up test data
    const element = document.createElement('my-component');
    
    // Act - perform the action
    document.body.appendChild(element);
    
    // Assert - verify outcome
    expect(element.textContent).toContain('expected');
  });
});
```

## Coverage Targets

| Metric | Target | Minimum |
|--------|--------|---------|
| Statements | 85%+ | 80% |
| Branches | 80%+ | 75% |
| Functions | 90%+ | 85% |

## Best Practices

### Unit Tests
- Test one thing per test
- Use descriptive names: `it('should X when Y')`
- Mock external dependencies
- Keep tests fast (< 100ms each)

### E2E Tests
- Test critical user flows
- Use stable selectors (data-testid)
- Handle async properly
- Clean up test data

### Regression Tests
- Write test BEFORE fixing bug
- Reproduce failure first
- Include issue reference in test name

## Anti-Patterns

| Avoid | Instead |
|-------|---------|
| Testing implementation | Test behavior/outcomes |
| Flaky async tests | Use proper waits |
| Shared mutable state | Reset in beforeEach |
| Testing framework code | Test your code only |

## Detailed References

- `{baseDir}/references/vitest-patterns.md` — Unit test patterns
- `{baseDir}/references/playwright-e2e.md` — E2E test patterns
- `{baseDir}/references/mocking.md` — Mock and stub patterns
