# AI-Assisted Test Generation — Using Claude for Coverage

<!-- hint:slides topic="AI test generation: AI strengths and weaknesses, prompting for tests, review pitfalls, and human+AI workflow" slides="5" -->

## What AI Is Good At (and Bad At)

**Good at:**
- Generating boilerplate (describe, it, expect)
- Suggesting edge cases (empty, null, boundary)
- Creating test data (fixtures, mocks)
- Drafting test doubles (stubs, mocks)
- Writing tests for well-structured, documented code

**Bad at:**
- Understanding *intent* without context
- Knowing what *matters* to your business
- Judging test quality (shallow vs deep assertions)
- Choosing what to test when time is limited

AI amplifies your judgment. You direct; AI drafts.

## Prompting for Tests

### Provide Context

- **Function under test**: Paste the code.
- **Framework**: Jest, Vitest, Playwright, etc.
- **Examples**: One good test as a style reference.
- **Constraints**: "No mocks for X", "Use testing-library for React".

```javascript
// Example prompt context:
// "Here's my function. Generate unit tests with Vitest.
// I want: happy path, empty input, invalid input, and one edge case.
// Style: use describe/it, expect().toBe() for primitives."
```

### Example Prompts

- "Generate unit tests for this function. Include happy path, null/empty, and error cases."
- "Write 3 integration tests for this API endpoint. Use the existing test setup."
- "Suggest edge cases I might have missed for this validation function."
- "Generate a Jest mock for this service. I need to verify it's called with the right arguments."

## Reviewing AI-Generated Tests

### Common Pitfalls

| Pitfall | What to check |
|---------|----------------|
| **Shallow assertions** | `expect(result).toBeDefined()` — asserts little |
| **False confidence** | Tests pass but don't verify real behavior |
| **Over-mocking** | Mocks everything; integration value lost |
| **Missing edge cases** | AI often hits obvious cases, misses subtle ones |
| **Wrong framework** | Jest vs Vitest, RTL vs Enzyme — verify APIs |

### Review Checklist

- [ ] Assertions verify *behavior*, not just "it didn't throw"
- [ ] Edge cases and error paths are covered
- [ ] Mocks are justified (isolate unit vs test integration)
- [ ] Test names describe what is being tested
- [ ] Tests are independent (no shared mutable state)

## AI Across Test Layers

| Layer | AI Strength | Human Must |
|-------|-------------|------------|
| **Unit** | Boilerplate, edge cases | Choose what to test, verify assertions |
| **Integration** | Setup, fixtures, API contracts | Verify real flows, DB state |
| **E2E** | Selectors, steps | Scenarios that matter, flakiness |

## Iterating on Generated Tests

1. **Generate** — Get a first draft from AI.
2. **Run** — Do they pass? Fix setup/import errors.
3. **Review** — Strengthen weak assertions, add missing cases.
4. **Refine prompt** — "Add tests for when the API returns 500" or "Use arrayContaining for the response."
5. **Repeat** — Use feedback to improve future prompts.

## AI for Test Data Generation

AI can generate:
- Realistic fake data (names, emails, dates)
- Boundary values (min, max, zero)
- Invalid inputs for negative tests
- Complex nested structures

```javascript
// Prompt: "Generate 5 valid and 3 invalid sample inputs for
// this email validation function"
```

## AI for Accessibility Testing

AI can suggest:
- ARIA attribute checks
- Keyboard navigation tests
- Color contrast assertions
- Screen reader flow tests

Still verify with real assistive tech; AI can miss nuance.

## Workflow: Human + AI

```mermaid
flowchart LR
    A[You: function + context] --> B[AI: draft tests]
    B --> C[You: run + review]
    C --> D{Good enough?}
    D -->|No| E[You: refine prompt]
    E --> B
    D -->|Yes| F[You: commit]
```

---

## Key Takeaways

1. **AI drafts, you direct** — Provide context, examples, constraints.
2. **Review critically** — Shallow assertions and over-mocking are common.
3. **Use AI for boilerplate and suggestions** — Not for deciding what matters.
4. **Iterate** — Refine prompts based on output quality.
5. **Combine layers** — AI for unit draft; human for integration/e2e strategy.
6. **Test data and accessibility** — AI can help; human verification still essential.
