# typescript-types-mock

> Generate mock objects from TypeScript types for Playwright tests. Rust-powered, 10-50x faster than ts-morph.

Latest version **1.0.10** (published 2026-09-16) · MIT license · 0 weekly downloads

## Install

```sh
npm install typescript-types-mock
pnpm add typescript-types-mock
yarn add typescript-types-mock
bun add typescript-types-mock
```

## Health

**Score 75/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; has provenance; recently updated; high maintenance score; high quality score.

Warnings: low downloads; large bundle.

## Facts

| | |
|---|---|
| Version | 1.0.10 |
| Published | 2026-09-16 |
| First published | 2026-09-01 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >= 18 |
| Dependencies | 0 |
| Unpacked size | 18.6 MB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 1 |
| Author | fess00666 |
| Maintainers | fess00666 |
| Keywords | typescript, mock, playwright, testing, e2e, codegen, rust, napi, test-data, fixtures, route-interception, performance |

## Links

- npm: https://www.npmjs.com/package/typescript-types-mock
- Repository: https://github.com/guber00666/typescript-types-mock
- Homepage: https://github.com/guber00666/typescript-types-mock#readme
- Issues: https://github.com/guber00666/typescript-types-mock/issues
- npm.io page: https://npm.io/package/typescript-types-mock

## Alternatives

- [@snazzah/davey](https://npm.io/package/@snazzah/davey.md) — 1.5M weekly downloads
- [@vendure/testing](https://npm.io/package/@vendure/testing.md) — 8.3K weekly downloads
- [vue-simple-context-menu](https://npm.io/package/vue-simple-context-menu.md) — 6.6K weekly downloads
- [cypress-webpack-preprocessor-v5](https://npm.io/package/cypress-webpack-preprocessor-v5.md) — 2.1K weekly downloads
- [@backstage/plugin-catalog-backend-module-puppetdb](https://npm.io/package/@backstage/plugin-catalog-backend-module-puppetdb.md) — 1.3K weekly downloads

## Recent versions

- 1.0.10 (latest) — 2026-09-16
- 1.0.2 (next) — 2026-09-04
- 1.0.9 — 2026-09-06
- 1.0.8 — 2026-09-06
- 1.0.6 — 2026-09-06
- 1.0.5 — 2026-09-06
- 1.0.4 — 2026-09-06
- 1.0.3 — 2026-09-06
- 1.0.1 — 2026-09-04
- 1.0.0 — 2026-09-04
- 0.4.0 — 2026-09-02
- 0.3.1 — 2026-09-02
- 0.3.0 — 2026-09-02
- 0.2.0 — 2026-09-02
- 0.1.0 — 2026-09-01

## README

# typescript-types-mock

[![npm version](https://img.shields.io/npm/v/typescript-types-mock.svg)](https://www.npmjs.com/package/typescript-types-mock)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

> Generate mock objects from TypeScript types for **Playwright** testing. Resolves types across local modules and npm packages.

## Installation

```bash
npm install -D typescript-types-mock ts-morph typescript
```

## Quick Start — Playwright

```typescript
// e2e/example.spec.ts
import { test, expect } from "@playwright/test";
import {
  createMockFromFile,
  createRouteResponse,
  createApiResponse,
} from "typescript-types-mock";
import path from "path";

const TYPES = path.resolve(__dirname, "../src/types.ts");

test("user profile page renders mocked data", async ({ page }) => {
  const user = createMockFromFile(TYPES, "User");

  await page.route("**/api/user", (route) => {
    route.fulfill(createRouteResponse(user));
  });

  await page.goto("/profile");
  await expect(page.getByText(user.name as string)).toBeVisible();
});
```

## API

### `createMockFromFile(filePath, typeName, options?)`

```typescript
const user = createMockFromFile("./types.ts", "User", { seed: 42 });
// => { name: "Alice Smith", age: 25, email: "alice@example.com", ... }
```

### `createManyMocks(filePath, typeName, count, options?)`

```typescript
const users = createManyMocks("./types.ts", "User", 5);
// => [{ name: "Alice Smith", ... }, { name: "Bob Johnson", ... }, ...]
```

### `listTypes(filePath)`

```typescript
const types = listTypes("./types.ts");
// => ["User", "Admin", "Product", ...]
```

### `MockContext` — caching for performance

Parse once, reuse resolved types across multiple calls.

```typescript
import { createMockContext } from "typescript-types-mock";

const ctx = createMockContext("./types.ts");
const user = ctx.mock("User");       // single mock
const users = ctx.many("User", 10);  // array of mocks
const types = ctx.listTypes();       // ["User", "Admin", ...]
```

## Playwright Helpers

### `createRouteResponse(body, options?)`

Create a Playwright-compatible `route.fulfill()` response:

```typescript
const user = createMockFromFile("./types.ts", "User");
const response = createRouteResponse(user);
// => { status: 200, contentType: "application/json", headers: {...}, body: "..." }

await page.route("**/api/user", (route) => {
  route.fulfill(response);
});

// Custom status:
route.fulfill(createRouteResponse(user, { status: 201 }));
```

### `createApiResponse(data, options?)`

Wrap data in a standard API response envelope:

```typescript
const envelope = createApiResponse(user);
// => { data: user, error: null, status: 200, timestamp: "..." }

// Error response:
createApiResponse(null, { status: 404, error: "Not found" });
```

### `createPaginatedResponse(items, options?)`

Paginated response with metadata:

```typescript
const items = createManyMocks("./types.ts", "Product", 10);
const page = createPaginatedResponse(items, { page: 1, pageSize: 10, total: 50 });
// => { data: [...], meta: { page: 1, pageSize: 10, total: 50, totalPages: 5 }, ... }
```

## Options

| Option | Type | Default | Description |
|--------|------|---------|-------------|
| `seed` | `number` | — | Deterministic generation (same seed = same output) |
| `overrides` | `Record<string, unknown>` | `{}` | Override property values (supports nested merge) |
| `generators.string` | `() => string` | random | Custom string generator |
| `generators.number` | `() => number` | random | Custom number generator |
| `generators.boolean` | `() => boolean` | random | Custom boolean generator |
| `generators.date` | `() => Date` | random | Custom date generator |
| `maxDepth` | `number` | `5` | Maximum depth for nested objects |
| `arrayLength` | `number` | `2` | Number of items in generated arrays |
| `includeOptional` | `boolean` | `true` | Whether to include optional properties |

## Supported Types

- **Primitives**: `string`, `number`, `boolean`, `bigint`, `null`, `undefined`
- **Literals**: `"hello"`, `42`, `true`
- **Enums**: `enum Color { Red = "RED" }`
- **Unions**: `"active" | "inactive"`
- **Intersections**: `User & Timestamped`
- **Arrays/Tuples**: `string[]`, `[number, string]`
- **Utility Types**: `Record<K,V>`, `Partial<T>`, `Required<T>`, `Pick<T,K>`, `Omit<T,K>`
- **Built-ins**: `Date`, `RegExp`, `Map<K,V>`, `Set<T>`, `Promise<T>`
- **Classes, Interfaces, Generics**

## Cross-Module Resolution

Types are resolved across imports automatically:

```typescript
// types/models.ts
export interface User { name: string; email: string; role: Role; }

// types/enums.ts
export type Role = "admin" | "user";

// types/index.ts
export { User } from "./models";
export { Role } from "./enums";

// In your Playwright test:
const user = createMockFromFile("./types/index.ts", "User");
// => role is correctly resolved as "admin" or "user"
```

### npm Package Types

Types from installed packages are resolved from `.d.ts` files:

```typescript
// consumer.ts
import { AxiosResponse } from "axios";
interface MyData { response: AxiosResponse; }

const data = createMockFromFile("./consumer.ts", "MyData");
```

## Playwright Patterns

### Mocking multiple endpoints

```typescript
import { test } from "@playwright/test";
import { createMockContext, createRouteResponse } from "typescript-types-mock";

test("dashboard loads", async ({ page }) => {
  const ctx = createMockContext("./types.ts");

  await page.route("**/api/user", (route) =>
    route.fulfill(createRouteResponse(ctx.mock("User")))
  );
  await page.route("**/api/products", (route) =>
    route.fulfill(createRouteResponse(ctx.many("Product", 5)))
  );

  await page.goto("/dashboard");
});
```

### Error scenarios

```typescript
await page.route("**/api/user/999", (route) => {
  route.fulfill(createRouteResponse(
    createApiResponse(null, { status: 404, error: "Not found" }),
    { status: 404 }
  ));
});
```

### Overrides with nested merge

```typescript
const order = createMockFromFile("./types.ts", "Order", {
  overrides: {
    id: "ORD-001",
    shippingAddress: { city: "Moscow" }, // merges, other fields generated
  },
});
```

### Deterministic tests

```typescript
const user = createMockFromFile("./types.ts", "User", { seed: 42 });
// Same seed → same output → stable assertions
```

## How It Works

1. **Parsing**: Uses [ts-morph](https://ts-morph.com/) to parse `.ts` files and extract type information.
2. **Dependency Resolution**: Follows `import`/`export` chains across local modules, re-exports, and npm packages.
3. **Type Resolution**: Converts TypeScript AST into an internal `TypeNode` representation.
4. **Mock Generation**: Generates realistic random values with configurable generators, overrides, and depth limits.

## License

MIT

---
_Source: https://npm.io/package/typescript-types-mock · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
