# name: generate-tests
# description:
Auto-generate Jest/Vitest test files with intelligent coverage and mock detection.
# tags:
Triggers on: generate tests, create tests, test this file, /test.
**user-invocable:** true
---
# Generate Tests
Generate robust unit or integration tests for JavaScript and TypeScript projects using local LLM (Ollama or MCP tools). Automatically detects async functions, edge cases, and external dependencies.
---
# Usage
```bash
/test src/userService.ts
/test src/userService.ts --framework jest
/test src/userService.ts --framework vitest
/test src/userService.ts --integration
/test src/userService.ts --coverage 80
/test src/userService.ts --watch
Options
--framework jest|vitest(default: auto-detect)--integration→ generate integration tests--unit→ generate unit tests (default)--coverage <n>→ target minimum coverage %--write→ auto-create test file--watch→ run test after generation--report→ return JSON metadata report
Workflow
1️⃣ Read Source File
cat <file>
Extract:
- Exported functions/classes
- Async functions
- Thrown errors
- External dependencies (axios, fetch, fs, db, env, etc.)
2️⃣ Analyze Dependencies
If external dependencies are detected:
- Generate mocks
- Stub environment variables
- Mock repositories/services
3️⃣ Generate Test Structure
Rules:
One
describeblock per exportCover:
- Happy path
- Invalid parameters
- Edge cases
- Exception paths
- Async rejection cases
No redundant tests
Clean naming
4️⃣ Output Preview
Show generated test file:
// userService.test.ts
Ask for confirmation if --write not passed.
5️⃣ Write File (if confirmed or --write)
Example:
touch src/__tests__/userService.test.ts
Write generated content.
6️⃣ Run Tests (optional)
If --watch:
npm test
If coverage < target:
- Re-analyze uncovered branches
- Generate missing tests
- Repeat cycle (max 3 iterations)
Test File Naming Convention
| Source | Output |
|---|---|
| userService.ts | userService.test.ts |
| auth.js | auth.test.js |
| services/user.ts | services/tests/user.test.ts |
Test Format
Default (Jest/Vitest)
import { functionName } from "./functionName"
describe("functionName", () => {
it("should return expected result for valid input", () => {
...
})
it("should throw error for invalid input", () => {
...
})
it("should handle edge cases", () => {
...
})
})
Async Example
If async detected:
it("should resolve with correct data", async () => {
await expect(getUser("123")).resolves.toEqual(...)
})
it("should reject when repository fails", async () => {
await expect(getUser("123")).rejects.toThrow()
})
Mock Strategy
Auto-mock when detecting:
- axios
- fetch
- fs
- database repositories
- crypto
- external services
Example (Vitest)
vi.mock("../repository/userRepository")
Example (Jest)
jest.mock("../repository/userRepository")
Coverage Rules
If --coverage 80:
- Run coverage
- Parse output
- Identify uncovered lines
- Generate additional edge tests
- Re-run (max 3 loops)
Output Modes
Default
Print generated test file.
With --write
Create file automatically.
With --report
Return JSON metadata:
{
"exportsDetected": ["getUser", "createUser"],
"asyncFunctions": ["getUser"],
"mocksGenerated": ["userRepository"],
"estimatedCoverage": 85
}
Tool Priority
mcp__aria__generate_tests(preferred)mcp__ollama__generate_tests- Local Ollama direct prompt
- Manual fallback template
Integration with CLI
Equivalent AC CLI command:
ac test src/userService.ts
ac test src/userService.ts --coverage 85
Can also bind to lazygit custom command.
Internal Prompt Template
You are a senior JavaScript/TypeScript test engineer.
Goal:
Generate robust {framework} tests.
Requirements:
- Cover all exported members
- Include happy path, edge cases, invalid inputs
- Mock external dependencies
- Use async testing correctly
- Avoid redundant tests
- Maintain clean structure
Return only the test file code.
Intelligent Behaviors
- Detects if file already has tests → suggest improvements
- Detects trivial functions → generate minimal tests
- Detects pure functions → avoid unnecessary mocks
- Detects controller layer → suggest integration mode
Notes
- Works fully offline with Ollama
- No API cost
- Avoids generating snapshot tests by default
- First describe block always matches filename
- Compatible with TypeScript strict mode
---
Se quiser, posso agora gerar:
- A versão `.json` para registrar como skill MCP
- A implementação base em TypeScript
- O esqueleto da CLI funcional
- O modo autônomo com loop de cobertura
Qual o próximo passo da arquitetura?