Rename tests to follow 'should...when...' convention
✓Works with OpenClaudeYou 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.jsonfor Jest, Mocha, Vitest, or Jasmine dependencies
Steps
- Identify all test blocks using regex:
(describe|it|test)\(\s*['"](.*?)['"]to capture current test names - Extract the test name string and analyze its structure—note missing subject ("should"), missing condition ("when"), or unclear assertions
- Break down each test name into three parts: (1) feature/function being tested, (2) condition/input, (3) expected outcome
- Rewrite the name following the pattern:
should [expected outcome] when [condition]at theit()level anddescribe()for grouping features - For nested describes, use
describe('MyFunction', () => { describe('when input is invalid', () => { it('should return error', () => {})})})structure - Use a find-and-replace tool or script to update test file names, preserving the test logic unchanged
- Run the test suite with
npm testorjestto ensure all renamed tests still pass - 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
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.