ai-test-generator
v0.1.8
Published
AI-powered test generator for JavaScript and TypeScript codebases using Google Gemini
Downloads
77
Maintainers
Readme
AI Test Generator
Command Line Interface
# Will interactively prompt for API key if not provided
ai-test-generator
# Generate tests for a specific file
ai-test-generator --file ./src/utils/helpers.js
# Specify test framework
ai-test-generator --framework jest
# Set output directory
ai-test-generator --output ./custom-tests
# Preview without writing files
ai-test-generator --dry-run
# Use specific Gemini model
ai-test-generator --model gemini-1.5-pro
# Skip interactive prompting
ai-test-generator --non-interactive
# Control API rate limiting (for quota management)
ai-test-generator --request-delay 3000 --batch-size 5 --batch-delay 10000
# Quota-friendly modes - for when you're hitting API limits
ai-test-generator --quota-friendly --file ./src/utils/helpers.js
ai-test-generator --ultra-quota-friendly --file ./src/utils/helpers.js
# Offline mode - generates templates without API calls
ai-test-generator --offline --file ./src/utils/helpers.jsProgrammatic API
import AITestGenerator from 'ai-test-generator';
const generator = new AITestGenerator({
testFramework: 'jest',
apiKey: 'your_gemini_api_key', // Optional - will prompt if not provided
outputDir: './tests',
verbose: true
});
// Generate tests for a file
await generator.generateForFile('./src/utils/helpers.js');
// Generate tests for entire codebase
await generator.generateForCodebase('./src');Configuration
Environment Variables
Create a .env file in your project root:
GEMINI_API_KEY=your_gemini_api_key_hereConfiguration File
Or use a configuration file .ai-test-generator.json:
{
"apiKey": "your_gemini_api_key_here",
"testFramework": "jest",
"outputDir": "./custom-tests-folder"
}API Options
| Option | Description | Default |
|--------|-------------|---------|
| testFramework | Testing framework to use (jest, mocha, vitest) | 'jest' |
| outputDir | Directory for generated tests | './tests' |
| apiKey | Gemini API key | undefined |
| interactive | Prompt for API key if not found | true |
| aiModel | Gemini model to use | 'gemini-1.5-flash' |
| testStyle | Type of tests to generate (unit, integration, both) | 'unit' |
| dryRun | Preview without writing files | false |
| verbose | Show detailed output | false |
| coverage | Target code coverage percentage | 80 |
| includeSnapshot | Include snapshot tests | false |
| requestDelay | Delay between API requests in ms | 1000 |
| batchSize | Number of components to process before pausing | 5 |
| batchDelay | Delay between batches in ms | 5000 |
How It Works
AI Test Generator uses a 3-step process:
- Analysis: Scans your codebase to find functions, classes, and methods that need tests
- AI Generation: Uses Gemini AI to create appropriate test cases for each component
- Output: Generates properly formatted test files with your chosen testing framework
The tool intelligently determines which components need tests based on complexity, importance, and other factors.
Examples
Testing a Utility Function
// Original code (utils.js)
export function formatCurrency(amount, currency = 'USD') {
return new Intl.NumberFormat('en-US', {
style: 'currency',
currency
}).format(amount);
}
// Generated test (utils.test.js)
import { formatCurrency } from './utils';
describe('formatCurrency', () => {
test('formats USD currency correctly', () => {
expect(formatCurrency(1000)).toBe('$1,000.00');
});
test('formats EUR currency correctly', () => {
expect(formatCurrency(1000, 'EUR')).toBe('€1,000.00');
});
test('handles zero correctly', () => {
expect(formatCurrency(0)).toBe('$0.00');
});
});Contributing
Contributions are welcome! Feel free to:
- Open issues for bugs or feature requests
- Submit pull requests
- Improve documentation
License
MIT
Created with ❤️ by Kidus
Features
- 🤖 Automatically analyzes your code to identify testable components
- ✨ Generates comprehensive tests using Google's Gemini AI
- 🧪 Supports Jest, Mocha, and Vitest testing frameworks
- 🔄 Works with both JavaScript and TypeScript projects
- 💬 Interactive mode for easy API key setup
- ⚙️ Customizable test generation options
- 🛠️ Available as both CLI tool and programmable API
Installation
npm install ai-test-generatorOr globally:
npm install -g ai-test-generatorRequirements
- Node.js 16 or higher
- A Google Gemini API key (Get one from Google AI Studio)
Usage
To use the AI Test Generator, you can run the command-line interface (CLI) provided in the package. Here’s a basic example:
npx ai-test-generator <path-to-your-code>Replace <path-to-your-code> with the path to the directory containing your codebase. The tool will analyze the code and generate test cases accordingly.
Contributing
Contributions are welcome! If you have suggestions for improvements or new features, please open an issue or submit a pull request.
License
This project is licensed under the MIT License. See the LICENSE file for more details.
