TestGen AI
✨ Features
- 🤖 AI-Powered Generation - Uses GPT-4 or GPT-3.5-turbo to generate intelligent, contextualized tests
- 🔍 Smart Code Analysis - Automatically detects hooks and utility functions that need testing
- ⚡ Zero Configuration - Works out of the box with basic templates, AI optional
- 🎯 Intelligent Detection - Distinguishes between 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 or individual files
🚀 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 (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
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"
}
📖 Usage
Commands
| 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 |
Example
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 (src/hooks/useBoolean.test.ts):
import { act, renderHook } from '@testing-library/react-hooks'
import { useBoolean } from './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
})
⚙️ 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 |
🎯 What Gets Tested?
✅ Detected & Tested
- 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
🛠️ 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.
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