autotest-ai
v1.0.0
Published
AI-powered test generator. Generate unit tests from your code using Claude or OpenAI.
Downloads
101
Maintainers
Readme
autotest-ai
AI-powered test generator. Generate unit tests from your code using Claude or OpenAI.
Stop writing tests manually. Let AI analyze your code and generate comprehensive unit tests.
Features
- AI-Powered: Uses Claude or OpenAI to understand your code and generate meaningful tests
- Multiple Frameworks: Supports Jest, Vitest, Node.js test runner, and Mocha
- Edge Cases: Automatically generates tests for edge cases (null, undefined, empty values)
- TypeScript Support: Generates TypeScript or JavaScript tests
- CLI & API: Use from command line or programmatically
- Batch Processing: Generate tests for entire directories
Installation
npm install -g autotest-aiOr use with npx:
npx autotest-ai src/utils.tsQuick Start
1. Set your API key
# For Claude (recommended)
export ANTHROPIC_API_KEY=your-api-key
# For OpenAI
export OPENAI_API_KEY=your-api-key2. Generate tests
# Generate tests for a file
autotest src/utils.ts
# Generate tests for all files in a directory
autotest src/3. Run your tests
npm testCLI Usage
autotest <file|directory> [options]Options
| Option | Description |
|--------|-------------|
| -p, --provider <claude\|openai> | AI provider (default: claude) |
| -m, --model <model> | Model to use |
| -f, --framework <framework> | Test framework: jest, vitest, node, mocha |
| -o, --output <dir> | Output directory (default: tests) |
| --no-edge-cases | Skip edge case tests |
| --js | Generate JavaScript instead of TypeScript |
| --dry-run | Print tests without writing files |
| -h, --help | Show help |
Examples
# Generate Jest tests (default)
autotest src/utils.ts
# Generate Vitest tests
autotest src/utils.ts --framework vitest
# Use OpenAI instead of Claude
autotest src/utils.ts --provider openai
# Preview generated tests without writing
autotest src/utils.ts --dry-run
# Generate tests for entire src directory
autotest src/
# Generate JavaScript tests
autotest src/utils.js --jsProgrammatic API
import { generateTests, generateAndWriteTests } from 'autotest-ai';
// Generate tests (returns test code)
const result = await generateTests('src/utils.ts', {
provider: 'claude',
framework: 'jest',
edgeCases: true,
});
console.log(result.testCode);
console.log(`Functions tested: ${result.functions.join(', ')}`);
// Generate and write tests to file
const written = await generateAndWriteTests('src/utils.ts', {
framework: 'vitest',
outputDir: '__tests__',
});
console.log(`Test file created: ${written.testFile}`);API Reference
generateTests(filePath, config?)
Generates tests for a file and returns the result.
interface GenerateResult {
sourceFile: string; // Original source file path
testFile: string; // Generated test file path
testCode: string; // Generated test code
functions: string[]; // Functions that were tested
tokensUsed?: number; // API tokens used
}generateAndWriteTests(filePath, config?)
Generates tests and writes them to a file.
generateTestsForDirectory(dirPath, config?)
Generates tests for all source files in a directory.
Config Options
interface AutoTestConfig {
provider?: 'claude' | 'openai'; // AI provider
apiKey?: string; // API key (or use env var)
model?: string; // Model to use
framework?: 'jest' | 'vitest' | 'node' | 'mocha';
outputDir?: string; // Output directory
edgeCases?: boolean; // Include edge cases
typescript?: boolean; // Generate TypeScript
}Example Output
Input: src/math.ts
export function add(a: number, b: number): number {
return a + b;
}
export function divide(a: number, b: number): number {
if (b === 0) throw new Error('Division by zero');
return a / b;
}Generated: __tests__/math.test.ts
import { describe, it, expect } from '@jest/globals';
import { add, divide } from '../src/math';
describe('add', () => {
it('should add two positive numbers', () => {
expect(add(2, 3)).toBe(5);
});
it('should add negative numbers', () => {
expect(add(-1, -1)).toBe(-2);
});
it('should handle zero', () => {
expect(add(0, 5)).toBe(5);
expect(add(5, 0)).toBe(5);
});
});
describe('divide', () => {
it('should divide two numbers', () => {
expect(divide(10, 2)).toBe(5);
});
it('should handle decimal results', () => {
expect(divide(5, 2)).toBe(2.5);
});
it('should throw error for division by zero', () => {
expect(() => divide(10, 0)).toThrow('Division by zero');
});
});Supported Test Frameworks
| Framework | Command |
|-----------|---------|
| Jest | autotest file.ts --framework jest |
| Vitest | autotest file.ts --framework vitest |
| Node.js | autotest file.ts --framework node |
| Mocha | autotest file.ts --framework mocha |
Environment Variables
| Variable | Description |
|----------|-------------|
| ANTHROPIC_API_KEY | API key for Claude |
| OPENAI_API_KEY | API key for OpenAI |
Tips
- Better tests with better code: Well-documented functions with clear types produce better tests
- Review generated tests: AI-generated tests are a starting point, review and adjust as needed
- Use --dry-run first: Preview tests before writing to files
- Combine with TDD: Generate initial tests, then refine as you develop
Support
If autotest-ai saved you time:
- Star the GitHub repo
- Buy me a coffee
License
MIT
