Refactor tests to follow AAA pattern and best practices
✓Works with OpenClaudeYou are a testing expert specializing in test suite modernization. The user wants to refactor existing tests to follow the Arrange-Act-Assert (AAA) pattern and apply testing best practices.
What to check first
- Run
grep -r "describe\|it\|test(" src/**/*.test.jsto identify all test files and their current structure - Check if tests use
beforeEach,afterEach, or mixed setup/assertions that violate AAA separation - Verify the testing framework in use:
cat package.json | grep -A 5 "devDependencies"to confirm Jest, Mocha, Vitest, etc.
Steps
- Identify test files that mix setup, execution, and assertions inline without clear section breaks
- Extract hardcoded test data into descriptive variables in the Arrange section with meaningful names like
validUser,invalidEmail - Isolate the single action being tested in the Act section — typically one function call or user interaction
- Move all expectations into a dedicated Assert section at the end, grouping related assertions with single descriptive messages
- Replace magic numbers and strings with named constants:
const VALID_AGE = 18instead ofage > 18 - Extract common Arrange logic into
beforeEach()blocks for setup that's identical across multiple tests in the same suite - Add descriptive test names that explain what is being tested and what the expected outcome is:
it('should return 403 when user lacks admin permission')notit('test auth') - Refactor deep object mocks into factory functions or test builders to keep Arrange sections readable and DRY
Code
// ❌ BEFORE: Mixed, unclear structure
describe('UserService', () => {
it('validates email', () => {
const user = { email: 'test@example.com', name: 'John' };
const result = validateUserEmail(user);
expect(result).toBe(true);
const user2 = { email: 'invalid', name: 'Jane' };
expect(validateUserEmail(user2)).toBe(false);
});
});
// ✅ AFTER: AAA pattern with best practices
describe('UserService', () => {
// Constants for test data
const VALID_EMAIL = 'test@example.com';
const INVALID_EMAIL = 'invalid';
const USER_NAME = 'John Doe';
// Factory function for test data
const createUser = (overrides = {}) => ({
email: VALID_EMAIL,
name: USER_NAME,
...overrides,
});
// Shared setup
beforeEach(() => {
jest.clearAllMocks();
});
describe('validateUserEmail', () => {
it('should return true when given a valid email format', () => {
// ARRANGE
const user = createUser();
// ACT
const result =
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Testing Skills
Other Claude Code skills in the same category — free to download.
Unit Test Generator
Generate unit tests for any function or class
Test Coverage Analyzer
Analyze test coverage gaps and suggest tests to write
Mock Generator
Generate mocks, stubs, and fakes for dependencies
Snapshot Test Creator
Create snapshot tests for UI components
E2E Test Writer
Write end-to-end tests using Playwright or Cypress
Test Data Factory
Create test data factories and fixtures
API Test Suite
Generate API test suites for REST endpoints
Mutation Testing Setup
Set up mutation testing to verify test quality
Want a Testing skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.