AI accelerates testing and code review significantly — but only if you know how to prompt for them. This lesson covers generating tests, conducting AI reviews, and understanding where AI falls short.
AI excels at writing unit tests for pure functions. Give it the function, tell it which cases to cover, and specify your test framework.
# Prompt template:
"Write Vitest unit tests for the following function.
[paste function]
Test cases to cover:
- Normal case (happy path)
- Empty input / null / undefined
- Boundary values (0, negative, very large)
- Error cases (invalid types)
- [any domain-specific edge cases]
Use describe/it blocks. Do not mock anything unless necessary."
# Example result for formatCurrency():
describe('formatCurrency', () => {
it('formats a positive USD amount', () => {
expect(formatCurrency(1234.56, 'USD')).toBe('$1,234.56')
})
it('defaults to USD when currency omitted', () => {
expect(formatCurrency(100)).toBe('$100.00')
})
it('returns N/A for NaN', () => {
expect(formatCurrency(NaN, 'USD')).toBe('N/A')
})
it('handles zero', () => {
expect(formatCurrency(0, 'USD')).toBe('$0.00')
})
it('handles negative amounts', () => {
expect(formatCurrency(-50, 'USD')).toBe('-$50.00')
})
})
# Code review prompt: "Review the following code for: 1. Correctness bugs (logic errors, off-by-one, null issues) 2. Security vulnerabilities (injection, auth bypasses, data exposure) 3. Code quality (readability, naming, duplication) 4. Performance issues (unnecessary loops, missing indexes, N+1) 5. Missing error handling Be specific: for each issue, state the line/section, the problem, and how to fix it. [paste code]" # Example targeted review: "Review this Laravel controller method for security issues only. Pay attention to: authorization checks, input validation, SQL injection risks, and sensitive data in responses. [paste method]"
# When you encounter unfamiliar code:
"Explain what this code does step by step.
Assume I understand JavaScript but am not familiar with
this specific pattern.
[paste code]"
# For a complex regex:
"Explain what this regex matches:
/^(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/
Break down each part. Give 3 examples that match and 3 that don't."
# For a SQL query:
"Explain this SQL query in plain English.
Describe what each JOIN does and what the final result set contains.
[paste query]"
# For an algorithm:
"Explain this sorting algorithm. What is its time complexity?
Walk through it with the input [3, 1, 4, 1, 5] step by step."
# JSDoc generation:
"Add JSDoc comments to all exported functions in this file.
Include: @param (name, type, description), @returns, @throws,
@example with a realistic usage example.
[paste file]"
# README generation:
"Write a README section for the useCart composable below.
Include: what it does, how to use it, all exported properties
and methods with their types, and a complete usage example.
[paste composable]"
# API docs:
"Document this Laravel controller as an API reference.
For each method include: HTTP method, endpoint URL,
request parameters, response format, and error codes."
# Security-focused review: "Check this code for the following vulnerabilities: - SQL injection: any raw queries with user input? - XSS: any user content rendered as raw HTML? - IDOR: are resource IDs validated against the authenticated user? - Missing auth: any endpoints missing authentication middleware? - Sensitive data: are passwords, tokens, or PII logged or returned? - Mass assignment: are there unguarded fillable fields? [paste Laravel controller or Vue component]" # Input validation check: "Is this form validation sufficient? What inputs could an attacker send to bypass validation or cause unexpected behavior? [paste validation rules]"
| Test type | What AI helps with | Test framework |
|---|---|---|
| Unit | Pure functions, composables, store actions, utilities | Vitest, Jest, PHPUnit |
| Integration | API endpoints with DB, component + store interaction | Vitest + mocks, PHPUnit |
| E2E | Full user flows (login → cart → checkout) | Playwright, Cypress |
# E2E test prompt: "Write a Playwright test for the user login flow: 1. Navigate to /login 2. Fill in email: test@example.com, password: secret123 3. Click the Submit button 4. Assert URL changes to /dashboard 5. Assert the page contains 'Welcome back' 6. Also test the failure case: wrong password shows error message."
# When a test fails, paste the error to AI:
"This Vitest test is failing with:
AssertionError: expected 'USD 1,234.56' to equal '$1,234.56'
Here is the test:
[paste test]
Here is the function being tested:
[paste function]
What is causing the mismatch and how do I fix the function?"
# When AI-written tests themselves are wrong:
"The test AI generated expects the wrong value.
The function returns an object but the test checks a string.
Here's the actual function output: { amount: 100, currency: 'USD' }
Fix the test assertions to match the actual return shape."
AI is not a substitute for human review. It consistently misses or struggles with: