unplugin-ts-mock
v0.2.1
Published
Build-time TypeScript type → faker mock generator for Vite, Rollup, webpack, and esbuild
Readme
unplugin-ts-mock
A build-time bundler plugin that scans TypeScript types and auto-generates faker-based mock data.
Supports Vite, webpack, Rollup, and esbuild.
Demo
# Requires Node.js 20+
cd test-vite
npm install
npm run dev # http://localhost:5173Mock data generated by createMock<User>(), createMock<Post>(), etc. is displayed as cards.
Click Regenerate to produce new faker values.
Usage
Write createMock<T>() in your code — the plugin replaces it with an object literal at build time.
Type inference and autocomplete work as-is with zero runtime overhead.
Installation
npm install unplugin-ts-mockPlugin setup
// vite.config.ts
import TsMock from 'unplugin-ts-mock/vite'
export default {
plugins: [TsMock()]
}// webpack.config.js
const TsMock = require('unplugin-ts-mock/webpack')
module.exports = {
plugins: [TsMock()]
}// rollup.config.js
import TsMock from 'unplugin-ts-mock/rollup'
export default {
plugins: [TsMock()]
}// esbuild
import TsMock from 'unplugin-ts-mock/esbuild'
await build({
plugins: [TsMock()]
})API
import { createMock, createMockList } from 'unplugin-ts-mock'
import type { User, Post } from './types'
const user = createMock<User>() // inferred as User
const post = createMock<Post>()
const users = createMockList<User>(3) // inferred as User[]After build, the actual bundle contains:
const user = {
id: "f3152fb1-...",
name: "Lola Friesen",
email: "[email protected]",
role: Role.Admin,
createdAt: "2024-01-15T09:23:00.000Z",
...
}The createMock<T>() call is replaced entirely with an inlined object literal.
Options
TsMock({
dir: './src', // directory to scan (default: project root)
exclude: ['fixtures'], // additional directory names to exclude
})Supported types
| Feature | Support |
|---|---|
| interface, type alias, enum | ✓ |
| union, intersection, tuple | ✓ |
| Partial / Required / Readonly / Pick / Omit / Promise / Array<T> | ✓ |
| interface extends inheritance | ✓ |
| Auto-discovery of all project files | ✓ |
| Types from node_modules | ✓ via TypeChecker |
| Namespaced types (WebAssembly.Memory, etc.) | ✓ via TypeChecker |
| Function types / method signatures | ✓ generates () => {} stub |
| Circular references | ✓ auto-blocked (depth limit 6) |
| Advanced utilities (Extract, Exclude, ReturnType, …) | △ generates {} |
Field name inference
Field names are matched to produce meaningful values.
| Pattern | Example fields | Generated value |
|---|---|---|
| Exact match | id | UUID |
| Exact match | name, email, phone, company | faker equivalent |
| Exact match | timezone, locale, slug, ip, mimeType | faker equivalent |
| *Id suffix | userId, teamId | UUID |
| *At suffix | createdAt, updatedAt | ISO date string |
| *Date suffix | startDate, dueDate | ISO date string |
| *Url suffix | avatarUrl, imageUrl | URL |
| Other string | — | lorem ipsum word |
Caveats
Optional fields (?) are omitted ~30% of the time.
Results vary per run — override specific fields after generation if a fixed value is required.
Advanced utility types (Extract, Exclude, ReturnType, Parameters, etc.) generate {}.
How it works
Directory scan
→ collect .ts / .tsx files (test and story files excluded)
→ ts.createProgram() build full program (auto-detects tsconfig.json)
→ TypeChecker resolve TypeReferences in declaration context
→ faker generate random data matching field types and names
[Plugin]
→ detect createMock<T>() patterns via regex
→ extract generic argument T and generate mock
→ replace call with inlined object literal
→ auto-inject missing enum imports