TestGen AI
✨ Features
- 🤖 AI-Powered Generation - Uses GPT-4 or GPT-3.5-turbo to generate intelligent, contextualized tests
- 🔍 Smart Code Analysis - Automatically detects React components, hooks, and utility functions that need testing
- ⚡ Zero Configuration - Works out of the box with basic templates, AI optional
- 🎯 Intelligent Detection - Distinguishes between components, hooks, and functions automatically
- 📁 Path Alias Support - Resolves TypeScript path aliases from
tsconfig.json
- 🚫 Smart Filtering - Ignores service hooks (useQuery/useMutation wrappers)
- 🔄 Batch Processing - Generate tests for entire projects, individual files, or staged files
- ▶️ Run Tests - Execute tests directly from VS Code/Cursor with one command
- 📦 Git Integration - Generate and run tests for staged files before committing
🚀 Quick Start
Installation
From Marketplace:
- Open VS Code or Cursor
- Press
Cmd+Shift+X (Mac) or Ctrl+Shift+X (Windows/Linux)
- Search for
TestGen AI
- Click Install
Or install via command:
code --install-extension RoinerCamacho.testgen-ai
First Use
- Open a TypeScript/TSX file (component, hook, or utility function)
- Press
Cmd+Shift+P (or Ctrl+Shift+P)
- Type:
TestGen: Generate test for current file
- Done! Your test file is generated and opened automatically
- Run the test:
TestGen: Run test for current file
For AI-powered test generation:
- Get your OpenAI API key from platform.openai.com/api-keys
- Open Settings (
Cmd+,)
- Search for
testgen
- Enter your API key in
testgen.openaiApiKey
Or add to settings.json:
{
"testgen.openaiApiKey": "sk-your-key-here",
"testgen.model": "gpt-4",
"testgen.testCommand": "npm test"
}
📖 Usage
Workflow Example
Complete workflow with Git integration:
- Make changes to your code
- Stage files:
git add src/components/Button.tsx
- Generate tests:
TestGen: Generate tests for staged files
- Run tests:
TestGen: Run tests for staged files
- Commit if all tests pass ✅
Commands
Generate Tests
| Command |
Description |
TestGen: Generate test for current file |
Generate test for the active file |
TestGen: Analyze project |
Scan project and list files without tests |
TestGen: Generate all missing tests |
Generate tests for all untested files |
TestGen: Generate tests for staged files |
Generate tests for all files staged in git |
Run Tests
| Command |
Description |
TestGen: Run test for current file |
Run test for the active file or its test file |
TestGen: Run all tests |
Run all tests in the project |
TestGen: Run tests for staged files |
Run tests for files staged in git |
Examples
Example 1: React Hook
Input (src/hooks/useBoolean.ts):
import { useState } from 'react'
export const useBoolean = (initialValue = false) => {
const [value, setValue] = useState(initialValue)
const setTrue = () => setValue(true)
const setFalse = () => setValue(false)
const toggle = () => setValue((v) => !v)
return { value, setValue, setTrue, setFalse, toggle }
}
Generated Output (__test__/src/hooks/useBoolean.test.ts):
import { act, renderHook } from '@testing-library/react-hooks'
import { useBoolean } from '@hooks/useBoolean'
describe('useBoolean', () => {
it('should return initial state', () => {
const { result } = renderHook(() => useBoolean(false))
expect(result.current.value).toBe(false)
})
it('should set value to true', () => {
const { result } = renderHook(() => useBoolean())
act(() => {
result.current.setTrue()
})
expect(result.current.value).toBe(true)
})
// ... more comprehensive tests generated by AI
})
Example 2: React Component
Input (src/components/Button.tsx):
import React from 'react'
interface ButtonProps {
label: string
onClick: () => void
disabled?: boolean
}
export const Button: React.FC<ButtonProps> = ({ label, onClick, disabled = false }) => {
return (
<button onClick={onClick} disabled={disabled}>
{label}
</button>
)
}
Generated Output (__test__/src/components/Button.test.tsx):
import { render, screen, fireEvent } from '@testing-library/react'
import { Button } from '@components/Button'
describe('Button', () => {
it('should render correctly', () => {
const mockOnClick = jest.fn()
render(<Button label="Click me" onClick={mockOnClick} />)
expect(screen.getByText('Click me')).toBeInTheDocument()
})
it('should handle click events', () => {
const mockOnClick = jest.fn()
render(<Button label="Click me" onClick={mockOnClick} />)
fireEvent.click(screen.getByText('Click me'))
expect(mockOnClick).toHaveBeenCalledTimes(1)
})
// ... more comprehensive tests generated by AI
})
⚙️ Configuration
| Setting |
Type |
Default |
Description |
testgen.openaiApiKey |
string |
"" |
OpenAI API key for AI-powered generation |
testgen.model |
string |
"gpt-4" |
Model to use: "gpt-4" or "gpt-3.5-turbo" |
testgen.ignoreServiceHooks |
boolean |
true |
Skip simple useQuery/useMutation wrappers |
testgen.testCommand |
string |
"" |
Custom command to run tests (e.g., "npm test", "jest", "yarn test"). Leave empty to auto-detect from package.json |
🎯 What Gets Tested?
✅ Detected & Tested
- React Components (functional and class components)
- React Hooks with state (
useState, useReducer)
- Hooks with side effects (
useEffect)
- Hooks with business logic
- Utility Functions in
utils/ or funcs/ folders
- Pure Functions with defined types
❌ Ignored
- Service hooks (simple
useQuery/useMutation wrappers)
- Hooks without business logic
- Non-exported functions
- Non-exported components
🛠️ Requirements
- VS Code 1.74+ or Cursor
- TypeScript project with
tsconfig.json
- Node.js 18+ (for development)
- OpenAI API Key (optional, for AI generation)
📚 Documentation
🐛 Troubleshooting
Extension doesn't appear in commands
- Reload VS Code/Cursor window (
Cmd+R or Ctrl+R)
- Verify the extension is installed and enabled
- Check if you're in a TypeScript project with
tsconfig.json
OpenAI API key not working
- Verify the key starts with
sk-
- Check your OpenAI account has credits
- Ensure the key has proper permissions
- Try regenerating the key
Tests have incorrect imports
- Verify
tsconfig.json has paths configured
- Check path aliases are correctly set up
- The extension attempts automatic resolution
No workspace open error
Open a folder (File → Open Folder), not just a file. The extension requires a workspace context.
Test command not found
- The extension auto-detects test commands from
package.json scripts
- If auto-detection fails, set
testgen.testCommand in settings (e.g., "npm test", "jest", "yarn test")
- Ensure your test runner (Jest, Vitest, etc.) is installed in your project
Cannot find test file
- Tests are generated in
__test__/ folder by default, maintaining folder structure
- The extension also checks for test files next to source files
- Use
TestGen: Generate test for current file if test doesn't exist
For more help, see the Troubleshooting Guide.
🤝 Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature)
- Commit your changes (
git commit -m 'Add some AmazingFeature')
- Push to the branch (
git push origin feature/AmazingFeature)
- Open a Pull Request
📄 License
This project is licensed under the MIT License - see the LICENSE file for details.
🙏 Acknowledgments
- OpenAI for the GPT API
- ts-morph for TypeScript analysis
- VS Code team for the excellent extension API