typescript-mock-builder
v1.0.0
Published
Reads a TypeScript interface and generates a typed mock object — zero config CLI
Maintainers
Readme
typescript-mock-builder
Zero-config CLI and library that reads a TypeScript interface and instantly generates a typed mock object or factory function — no compiler plugins, no schemas, no setup.
Install
npm install -g typescript-mock-builder
# or run directly
npx typescript-mock-builder ./src/types/User.tsUsage
Given a TypeScript file:
// src/types/User.ts
export interface User {
id: number;
email: string;
name: string;
createdAt: Date;
isActive: boolean;
phone?: string;
}Generate a plain mock object
npx typescript-mock-builder ./src/types/User.tsOutput:
const mockUser: User = {
id: 1,
email: '[email protected]',
name: 'mock-name',
createdAt: new Date(),
isActive: false,
};Generate a factory function
npx typescript-mock-builder ./src/types/User.ts --factoryOutput:
function createUser(overrides: Partial<User> = {}): User {
return {
id: 1,
email: '[email protected]',
name: 'mock-name',
createdAt: new Date(),
isActive: false,
...overrides,
};
}Options
| Flag | Description |
|------|-------------|
| --interface <name> | Generate mock for a specific interface only |
| --factory | Generate a factory function instead of a plain object |
| --export | Add export keyword to the generated mock |
| --var <name> | Custom variable or function name |
Examples
# Specific interface
npx typescript-mock-builder ./src/types.ts --interface Product
# With export + custom name
npx typescript-mock-builder ./src/types.ts --export --var testUser
# Factory with export
npx typescript-mock-builder ./src/types.ts --factory --exportHow mock values are chosen
The generator uses name-based heuristics first, then type-based fallbacks:
| Property name pattern | Value |
|-----------------------|-------|
| id, *Id | 1 |
| email, *Email | '[email protected]' |
| url, *Url, *Uri | 'https://example.com' |
| name, *Name | 'mock-<propName>' |
| token, *Token | 'mock-token' |
| phone, *Phone | '+1234567890' |
| createdAt, updatedAt, *Date, *At | new Date() |
| Type | Value |
|------|-------|
| string | 'mock-<propName>' |
| number | 0 |
| boolean | false |
| Date | new Date() |
| string[], number[], T[] | [] |
| Union (A \| B \| null) | picks first non-null option |
| anything else | {} |
Optional properties are skipped by default.
Use as a library
import { readInterfaces, generateMock, generateMockFactory } from 'typescript-mock-builder';
const interfaces = readInterfaces('./src/types/User.ts');
const code = generateMock(interfaces[0]);
const factory = generateMockFactory(interfaces[0], { exportMock: true });Works with type aliases too
export type Product = {
id: number;
name: string;
};Type aliases that are object types are parsed the same way as interfaces.
License
MIT
