Registry / testing / unplugin-ts-mock

unplugin-ts-mock

JSON →
library0.2.1jsnpmunverified

A build-time bundler plugin (v0.2.1) that scans TypeScript types and auto-generates faker-based mock data, replacing createMock<T>() calls with inline object literals. Supports Vite, Rollup, webpack, and esbuild. Handles interfaces, type aliases, enums, unions, intersections, tuples, utility types (Partial, Required, etc.), and field name inference (e.g., *Id → UUID). No runtime overhead; types reference project + node_modules. Caveats: optional fields omitted ~30% of the time, advanced utility types (Extract, Exclude) generate empty objects. Requires Node >=20.11.0 and one of the supported bundlers (vite >=4, rollup >=3, webpack >=5, esbuild >=0.18).

npm install unplugin-ts-mock
INSTALL
IMPORT
SIG · UNPLUGIN-TS-MOCK
U
unplugin-ts-mock
testingjavascriptv0.2.1
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

createMock
import { createMock } from 'unplugin-ts-mock'
import createMock from 'unplugin-ts-mock'
Named export, not default. Works in both ESM and CJS.
createMockList
import { createMockList } from 'unplugin-ts-mock'
const { createMockList } = require('unplugin-ts-mock')
Also available as named export. The require destructuring works in CJS, but ESM is preferred.
TsMock
import TsMock from 'unplugin-ts-mock/vite'
import { TsMock } from 'unplugin-ts-mock/vite'
Plugin is default export from the framework-specific subpath. For webpack: 'unplugin-ts-mock/webpack', rollup: 'unplugin-ts-mock/rollup', esbuild: 'unplugin-ts-mock/esbuild'.

Sets up unplugin-ts-mock with Vite, defines a User interface, and uses createMock and createMockList to generate mock data at build time.

// vite.config.ts import { defineConfig } from 'vite' import TsMock from 'unplugin-ts-mock/vite' export default defineConfig({ plugins: [TsMock({ dir: './src', exclude: ['fixtures'], })] }) // types.ts export interface User { id: string name: string email: string role: 'admin' | 'user' createdAt: Date } // app.ts import { createMock, createMockList } from 'unplugin-ts-mock' import type { User } from './types' const user = createMock<User>() const users = createMockList<User>(3) console.log(user.id) // e.g., "f3152fb1-..." console.log(users.length) // 3
Debug
Known issues
gotchaOptional fields (?) are omitted ~30% of the time.
fix
Override specific fields after creation (e.g., user.name = 'Fixed Name') if deterministic values are required.
affects: >=0.0.0
gotchaAdvanced utility types (Extract, Exclude, ReturnType, Parameters, etc.) generate empty objects ({}).
fix
Manually define mocks for types using these utilities, or avoid them in mock-generating contexts.
affects: >=0.0.0
breakingNode.js version requirement: >=20.11.0. Earlier versions will cause runtime errors.
fix
Update Node.js to version 20.11.0 or higher.
affects: >=0.0.0
deprecatedThe plugin may not work with future bundler versions beyond specified peer ranges.
fix
Monitor peer dependency requirements; upgrade bundlers accordingly.
affects: >=0.0.0
gotchaEnum imports are automatically injected into the bundle, which may cause naming conflicts if an enum with the same name is manually imported.
fix
Rename your own imports or avoid manual imports of enums that the plugin auto-injects.
affects: >=0.0.0
Errors
Common errors & fixes
TypeError: Cannot read properties of undefined (reading 'createProgram')
Missing TypeScript compiler API, or tsconfig.json not found.
fix
Ensure TypeScript is installed and a valid tsconfig.json exists in your project root.
Module not found: Error: Can't resolve 'unplugin-ts-mock/vite'
Importing from wrong subpath or package not installed.
fix
Run 'npm install unplugin-ts-mock' and import using the correct framework subpath: 'unplugin-ts-mock/vite', 'unplugin-ts-mock/webpack', etc.
Error: This dependency was not found: * faker in ./node_modules/unplugin-ts-mock/...
Faker is a transitive dependency, but if using an older package manager, it may not be hoisted.
fix
Install faker explicitly: 'npm install @faker-js/faker'.
TypeScript error: 'createMock' is not a function
Importing the plugin usage incorrectly, e.g., calling createMock without the generic type argument or using it at runtime without the plugin.
fix
Ensure the plugin is correctly configured in your bundler and that you provide a type argument: createMock<YourType>()
Upgrade
Version history
0.2.1latest on npm
Audit
Dependencies
viteoptionalRequired if used with Vite (peer dependency; >=4.0.0)
rollupoptionalRequired if used with Rollup (peer dependency; >=3.0.0)
webpackoptionalRequired if used with webpack (peer dependency; >=5.0.0)
esbuildoptionalRequired if used with esbuild (peer dependency; >=0.18.0)
Agent activity
4 hits · last 30 days
node
4
Resources
unplugin-ts-mock — npm install unplugin-ts-mock · libregistry