Skip to content
| Marketplace
Sign in
Visual Studio Code>Testing>Testgen - Auto Test GeneratorNew to Visual Studio Code? Get it now.
Testgen - Auto Test Generator

Testgen - Auto Test Generator

testgen

| (0) | Free
Automatically generate unit tests and mocks for TypeScript projects using the testgen CLI
Installation
Launch VS Code Quick Open (Ctrl+P), paste the following command, and press enter.
Copied to clipboard
More Info

⚡ 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

  1. Install the extension from the VS Code Marketplace
  2. Open any TypeScript project with a tsconfig.json
  3. Create or edit a .ts file in your src/ folder
  4. Save — test and mock files appear instantly in testgen/
  5. 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

  1. Extension detects tsconfig.json in your workspace
  2. Automatically runs testgen init (installs Jest, creates config)
  3. Starts a background file watcher
  4. On every .ts file save → analyzes code with ts-morph AST parsing
  5. Generates corresponding test + mock files
  6. 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

  • Contact us
  • Jobs
  • Privacy
  • Manage cookies
  • Terms of use
  • Trademarks
© 2026 Microsoft