⚡ Testgen — Auto Test Generator for TypeScript
Save a file. Get instant tests. Zero configuration. No boilerplate.
Testgen automatically generates unit test files and mock data for your TypeScript projects — every time you save a source file.
✨ Features
🔄 Auto-Generate on Save
Save any .ts file in src/ and Testgen instantly generates:
testgen/tests/<path>/<file>.test.ts — structured test cases
testgen/mocks/<path>/<file>.mock.ts — realistic mock data
🧠 Smart Test Scenarios
Testgen doesn't just create empty test stubs — it analyzes your code and generates:
| What it detects |
What it generates |
| Function parameters |
Type-aware mock values (realistic numbers, strings, objects) |
async functions |
await calls + rejects.toThrow() scenarios |
throw statements |
expect(() => fn()).toThrow() scenarios |
| Module imports |
jest.mock() calls + dependency interaction checks |
| Class constructors |
Mock DI injection in beforeEach |
Custom types (User, Order) |
Realistic structured mock objects |
📂 Mirrored File Structure
Your test structure automatically mirrors your source:
src/services/user.service.ts
→ testgen/tests/services/user.service.test.ts
→ testgen/mocks/services/user.service.mock.ts
⚙️ Zero Configuration
The extension auto-detects TypeScript projects, initializes Testgen, and starts watching — all in the background.
📊 Status Bar
Always know if Testgen is running:
$(check) Testgen: Active — watching for changes
$(x) Testgen: Stopped — click to restart
🚀 Getting Started
- Install the extension from the VS Code Marketplace
- Open any TypeScript project with a
tsconfig.json
- Create or edit a
.ts file in your src/ folder
- Save — test and mock files appear instantly in
testgen/
- Run tests — open Command Palette →
Testgen: Run Tests
That's it. No terminal commands, no setup, no configuration.
🎮 Commands
Open the Command Palette (Ctrl+Shift+P / Cmd+Shift+P) and type "Testgen":
| Command |
Description |
| Testgen: Run Tests |
Run npm test in the integrated terminal |
| Testgen: Generate Tests for Current File |
Generate tests for the active .ts file |
| Testgen: Start Watcher |
Start the background file watcher |
| Testgen: Stop Watcher |
Stop the background file watcher |
| Testgen: Initialize Project |
Manually initialize Testgen in the workspace |
📖 Example
You write:
// src/services/order.service.ts
import { OrderRepository } from './orderRepository';
export class OrderService {
constructor(private repo: OrderRepository) {}
async createOrder(productId: number, quantity: number): Promise<string> {
if (quantity <= 0) throw new Error('Invalid quantity');
return this.repo.save({ productId, quantity });
}
}
Testgen generates:
Mock file (testgen/mocks/services/order.service.mock.ts):
export const OrderService_createOrderMocks = {
handlesTypicalValue: { productId: 100, quantity: 100 },
handlesZero: { productId: 0, quantity: 0 },
handlesBoundaryValue: { productId: Number.MAX_SAFE_INTEGER, quantity: Number.MAX_SAFE_INTEGER },
};
Test file (testgen/tests/services/order.service.test.ts):
jest.mock('../../src/services/orderRepository', () => ({
save: jest.fn(),
}));
import { OrderService } from '../../src/services/orderService';
import * as mocks from '../../mocks/services/orderService.mock';
describe('OrderService', () => {
let instance: OrderService;
const mockRepo = {} as any;
beforeEach(() => {
jest.clearAllMocks();
instance = new OrderService(mockRepo);
});
describe('createOrder', () => {
test('handles typical value', async () => {
const productId = mocks.OrderService_createOrderMocks.handlesTypicalValue.productId;
const quantity = mocks.OrderService_createOrderMocks.handlesTypicalValue.quantity;
const result = await instance.createOrder(productId, quantity);
expect(result).toBeDefined();
});
test('rejects on invalid input', async () => {
const productId = mocks.OrderService_createOrderMocks.handlesZero.productId;
const quantity = mocks.OrderService_createOrderMocks.handlesZero.quantity;
await expect(instance.createOrder(productId, quantity)).rejects.toThrow();
});
test('verifies dependency interactions', async () => {
// Assert
expect(save).toHaveBeenCalled();
});
});
});
⚙️ Configuration
After initialization, customize behavior in testgen/testgen.config.json:
{
"watchPatterns": ["src/**/*.ts"],
"ignorePatterns": ["node_modules", "testgen", "dist"],
"testDir": "testgen/tests",
"mockDir": "testgen/mocks",
"srcDir": "src"
}
🛠️ Requirements
- Node.js ≥ 16
- TypeScript project with
tsconfig.json
- The
testgen CLI package (auto-installed during initialization)
📋 How It Works
- Extension detects
tsconfig.json in your workspace
- Automatically runs
testgen init (installs Jest, creates config)
- Starts a background file watcher
- On every
.ts file save → analyzes code with ts-morph AST parsing
- Generates corresponding test + mock files
- All tests follow the Arrange / Act / Assert pattern and pass out of the box
🐛 Known Issues
- The watcher may take 1-2 seconds to process large files
- Custom type mocking is heuristic-based (uses type name patterns)
📄 License
ISC
Made with ⚡ by the Testgen team