Skip to main content
command

generate-tests — Claude Code command: what it does, how to install

Creates a complete test suite for a specified component or file. The command inspects your testing setup and generates assertions following the AAA pattern.

Install into Claude Code · claude-code-templates All methods and platforms ↓
npx claude-code-templates@latest --command testing/generate-tests
Open on GitHub

What generate-tests does

It accepts a file path or component name. The prompt inspects the repository with Bash commands, searching for Jest, Vitest, Mocha, or Jasmine in package.json and config files. It also checks for existing test files and a test:coverage script. If you provide a component name instead of a path, it uses Grep to locate the source.

Once the target is located, the command analyzes exported functions, classes, and method signatures. It generates unit and integration tests following the Arrange, Act, Assert structure, with mocks for external I/O, network APIs, and timers. The instructions contain dedicated rules for React, Vue, Angular, Node.js, Python, Go, and Rust.

Without Bash access, Claude Code cannot inspect your configs or test scripts automatically. The command also requires Read, Write, and Edit permissions to inspect source code and write generated test files.

What it helps with

  • write unit tests for an individual file or component
  • detect the project test runner and existing conventions
  • design mocks for external APIs and test data factories
  • cover boundary conditions and error handling using AAA

Example prompts for the agent

  • /generate-tests src/utils/formatters.ts
  • /generate-tests UserProfile
  • /generate-tests src/api/client.ts

How to install generate-tests

1claude-code-templates

npx claude-code-templates@latest --command testing/generate-tests

2Manually: put the files in place

.claude/commands/generate-tests.md

Call it with /generate-tests. For all projects use ~/.claude/commands/

Questions about generate-tests

How to install generate-tests in Claude Code?

Run npx claude-code-templates@latest --command testing/generate-tests in your terminal. You can also place the command file directly at .claude/commands/generate-tests.md.

What does generate-tests do?

It analyzes a target file or component, detects the current test framework, and writes a test file following the AAA pattern. It focuses on isolating business logic and mocking external dependencies.

Which frameworks and languages are supported?

The prompt includes guidance for React, Vue, Angular, Node.js, Python (pytest), Go, and Rust. It also inspects package.json for Jest, Vitest, Mocha, and Jasmine.

Source text

generate-tests.md · MIT
---
allowed-tools: Read, Write, Edit, Bash
argument-hint: "[file-path] | [component-name]"
description: Generate a complete test file for a specified source file or component. Use when the user explicitly asks to write, create, or generate tests for a specific file.
---

# Generate Tests

Generate comprehensive test suite for: $ARGUMENTS

## Current Testing Setup

- Test framework: !`cat package.json 2>/dev/null | grep -E '"jest"|"vitest"|"mocha"|"jasmine"' | head -3 || cat jest.config.* vitest.config.* 2>/dev/null | head -5 || echo "Framework not detected"`
- Existing tests: !`find . -name "*.test.*" -o -name "*.spec.*" | head -5`
- Test coverage: !`npm run test:coverage 2>/dev/null || echo "No coverage script"`
- Target: if $ARGUMENTS is a file path, read it with @$ARGUMENTS; if it is a component name, search for it with Grep before writing tests

## Test Generation Framework

1. **Analyze** the target file/component structure — identify all exported functions, classes, methods, and their signatures
2. **Strategy** — examine existing test patterns in the project; choose unit vs integration scope; identify critical paths and error scenarios
3. **Mock Design** — map all external dependencies (I/O, APIs, timers, dates); create factories for test data; plan cleanup for async operations
4. **Unit Tests** — write isolated tests per function/method covering happy path, edge cases, and error conditions; follow AAA pattern (Arrange, Act, Assert)
5. **Integration Tests** — test component interactions, API layers with mocked responses, and end-to-end user workflows where applicable
6. **Quality Check** — verify naming describes behavior not implementation; confirm 80%+ coverage on critical business logic; ensure test isolation

## Framework-Specific Guidance

- **React**: Component testing with React Testing Library; test user interactions and rendering
- **Vue**: Component testing with Vue Test Utils; test props, events, and slots
- **Angular**: Component and service testing with TestBed; test dependency injection
- **Node.js**: API endpoint and middleware testing; test request/response cycles
- **Python**: `pytest` with fixtures, `unittest.mock` for patching, `pytest-cov` for coverage
- **Go**: Table-driven tests in `_test.go` files, `testify/assert` for assertions, subtests via `t.Run()`
- **Rust**: `#[cfg(test)]` modules, `#[test]` attributes, `mockall` for mocking

## Best Practices

- Follow AAA pattern (Arrange, Act, Assert)
- 80%+ coverage; prioritize critical business logic and error paths
- Mock external I/O; use factories for test data
- Naming: describe what the function does, not implementation details

© davila7 · MIT · davila7/claude-code-templates

Similar slash commands

All slash commands for Testing and QA →

From the same set238

The whole set →

Project sponsors

Companies that keep this analytics open
Gold sponsor

Impressions, clicks and code copies — open statistics. We keep the numbers public for the same reason NeBlask itself is public. Become a sponsor