Working Code
Start Claude Code in a project with functions to test:
> Write unit tests for all functions in src/utils/math.ts.
> Use Jest and include edge cases.
Claude generates a test file. Run it right away:
> Run npm test
Any failing tests? Let Claude fix them:
> Check the failing tests and fix the code
Try It Yourself
Now let's try the test-first TDD approach:
> Write tests for an email validation function first.
> Test cases: valid email, email without @, email without domain, empty string.
> Don't create the function itself yet.
Run the tests — they'll obviously fail:
> Run npm test
Now request the implementation:
> Implement a validateEmail function that passes these tests
> Run npm test again
Confirm that all tests pass.
"Why?" — Why Testing Matters More in the AI Era
In a world where AI writes code for you, tests are the safety net that validates AI's output.
Claude Code + Testing Workflow
1. Ask Claude to generate tests
2. Run tests → confirm failures
3. Ask Claude to implement
4. Run tests → confirm they pass
5. Request additional edge cases
6. Repeat
Useful Prompts for Test Generation
> Write tests for this function. Include normal cases, error cases, and edge cases.
> Write integration tests for this API endpoint. Include success, failure, and auth error cases.
> Find files with low test coverage and add tests.
Deep Dive
Can you use Claude's tests as-is?
Tests generated by Claude are a good starting point, but you know the business logic better. In particular:
- Add domain-specific edge cases yourself.
- Make sure test names clearly express intent.
- Check that tests aren't unnecessarily coupled to the implementation.
- Pick a function without tests in an existing project and ask Claude to generate tests.
- Build a new function the TDD way: tests first, implementation second.
- Ask Claude "Are there any edge cases missing from these tests?"
Q1. What's the correct order when working TDD-style with Claude Code?
- A) Implement → Test → Run
- B) Write tests → Run (fail) → Implement → Run (pass)
- C) Implement → Deploy → Test
- D) Implement without any tests