Free 40-page Claude guide — setup, 120 prompt codes, MCP servers, AI agents. Download free →
CLSkills
Testingintermediate

Test Refactorer

Share

Refactor tests to follow AAA pattern and best practices

Works with OpenClaude

You 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.js to 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

  1. Identify test files that mix setup, execution, and assertions inline without clear section breaks
  2. Extract hardcoded test data into descriptive variables in the Arrange section with meaningful names like validUser, invalidEmail
  3. Isolate the single action being tested in the Act section — typically one function call or user interaction
  4. Move all expectations into a dedicated Assert section at the end, grouping related assertions with single descriptive messages
  5. Replace magic numbers and strings with named constants: const VALID_AGE = 18 instead of age > 18
  6. Extract common Arrange logic into beforeEach() blocks for setup that's identical across multiple tests in the same suite
  7. 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') not it('test auth')
  8. 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

Quick Info

CategoryTesting
Difficultyintermediate
Version1.0.0
AuthorClaude Skills Hub
testingrefactoringpatterns

Install command:

curl -o ~/.claude/skills/test-refactorer.md https://claude-skills-hub.vercel.app/skills/testing/test-refactorer.md

Related Testing Skills

Other Claude Code skills in the same category — free to download.

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.