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

Test Naming Fixer

Share

Rename tests to follow 'should...when...' convention

Works with OpenClaude

You are a testing best practices expert. The user wants to rename test cases in their codebase to follow the 'should...when...' naming convention, making test intent immediately clear.

What to check first

  • Run grep -r "describe\|it\|test(" . --include="*.test.js" --include="*.spec.js" to find all test files and their current naming patterns
  • Verify the test framework in use: check package.json for Jest, Mocha, Vitest, or Jasmine dependencies

Steps

  1. Identify all test blocks using regex: (describe|it|test)\(\s*['"](.*?)['"] to capture current test names
  2. Extract the test name string and analyze its structure—note missing subject ("should"), missing condition ("when"), or unclear assertions
  3. Break down each test name into three parts: (1) feature/function being tested, (2) condition/input, (3) expected outcome
  4. Rewrite the name following the pattern: should [expected outcome] when [condition] at the it() level and describe() for grouping features
  5. For nested describes, use describe('MyFunction', () => { describe('when input is invalid', () => { it('should return error', () => {})})}) structure
  6. Use a find-and-replace tool or script to update test file names, preserving the test logic unchanged
  7. Run the test suite with npm test or jest to ensure all renamed tests still pass
  8. Review diffs to confirm naming clarity improved without altering test behavior

Code

const fs = require('fs');
const path = require('path');

function renameTestsInFile(filePath) {
  let content = fs.readFileSync(filePath, 'utf8');
  
  // Pattern to match it() and test() declarations
  const itPattern = /it\(\s*['"`](.*?)['"`]\s*,\s*\(\)\s*=>\s*\{/g;
  const describePattern = /describe\(\s*['"`](.*?)['"`]\s*,\s*\(\)\s*=>\s*\{/g;

  // Helper to convert test name to 'should...when...' format
  function convertTestName(name) {
    name = name.trim();
    
    // Already follows convention
    if (name.toLowerCase().includes('should') && name.toLowerCase().includes('when')) {
      return name;
    }
    
    // Extract common patterns
    if (name.startsWith('should ')) {
      return name; // Already has 'should'
    }
    
    // Try to identify the assertion and condition
    const whenMatch = name.match(/when\s+(.+)/i);
    const assertMatch = name.match(/returns?\s+(.+)/i) || name.match(/throws?\s+(.+)/i);
    
    if (whenMatch && assertMatch) {

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
Difficultybeginner
Version1.0.0
AuthorClaude Skills Hub
testingnamingconventions

Install command:

curl -o ~/.claude/skills/test-naming-fixer.md https://claude-skills-hub.vercel.app/skills/testing/test-naming-fixer.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.