Registry / testing / vite-plugin-mock-server

vite-plugin-mock-server

JSON →
library1.3.1jsnpmunverified

vite-plugin-mock-server is a Vite plugin designed to provide local API mocking capabilities for development servers. It enables developers to define mock APIs using both TypeScript and JavaScript files, automatically hot-reloading them upon modification. The plugin is built with TypeScript, ensuring type safety for mock definitions. A key differentiator is its robust compatibility with `express.js` middlewares, allowing for advanced request processing like body parsing and cookie handling directly within the mock server configuration. It leverages ant-style path matching for flexible URL pattern definition, making it versatile for various API structures. The current stable version is 1.3.1. While no explicit release cadence is stated, it appears to be actively maintained, with updates released as needed to support Vite and address community feedback.

npm install vite-plugin-mock-server
INSTALL
IMPORT
SIG · VITE-PLUGIN-MOCK-S
V
vite-plugin-mock-server
testingjavascriptv1.3.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

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

mockServer
import mockServer from 'vite-plugin-mock-server'
const mockServer = require('vite-plugin-mock-server')
The plugin is a default export. Vite is ESM-first, so CommonJS `require` is not the recommended or typical way to import plugins.
MockOptions
import type { MockOptions } from 'vite-plugin-mock-server'
This is a TypeScript type definition for configuring the plugin.
MockHandler
import type { MockHandler } from 'vite-plugin-mock-server'
import { MockHandler } from 'vite-plugin-mock-server'
This is a TypeScript type definition for individual mock API configurations. It should be imported as a type.

Demonstrates configuring `vite-plugin-mock-server` in `vite.config.ts` with Vue and common Express-compatible middlewares for body and cookie parsing, alongside an example mock file.

import { defineConfig } from 'vite' import vue from '@vitejs/plugin-vue' import bodyParser from 'body-parser' import cookieParser from 'cookie-parser' import mockServer from 'vite-plugin-mock-server' export default defineConfig({ plugins: [ vue(), mockServer({ logLevel: 'info', mockRootDir: './mock', urlPrefixes: ['/api/'], middlewares: [ cookieParser(), bodyParser.json(), bodyParser.urlencoded({ extended: true }), bodyParser.text(), bodyParser.raw() ] }) ] }) // example/mock/users.mock.ts import { MockHandler } from 'vite-plugin-mock-server' const mocks: MockHandler[] = [ { pattern: '/api/users/{userId}', method: 'GET', handle: (req, res) => { const userId = req.params?.userId || 'unknown'; res.setHeader('Content-Type', 'application/json'); res.end(JSON.stringify({ id: userId, name: `User ${userId}`, method: req.method, query: req.query })); } }, { pattern: '/api/posts', method: 'POST', handle: (req, res) => { res.setHeader('Content-Type', 'application/json'); res.statusCode = 201; res.end(JSON.stringify({ message: 'Post created successfully', data: req.body })); } } ]; export default mocks;
Debug
Known issues
deprecatedThe `urlVars` parameter in the `MockFunction` signature is deprecated in favor of `req.params`. Developers should migrate to using `req.params` for accessing URL path variables.
fix
Update `MockFunction` definitions to use `req.params` instead of the `urlVars` argument. For example, `(req, res) => { const userId = req.params?.userId; }`
affects: >=2.0
gotchaThis plugin requires Node.js version 12.0.0 or higher. Running with an older version may lead to unexpected errors or a failure to start the development server.
fix
Upgrade your Node.js installation to version 12.0.0 or newer.
affects: <12.0.0
gotchaThis plugin is designed for Vite 2.0.0 and above. Using it with older Vite versions is not officially supported and may cause compatibility issues.
fix
Ensure your project's Vite dependency is version 2.0.0 or higher.
affects: <2.0.0
gotchaFor requests with JSON or URL-encoded bodies (e.g., POST, PUT), `req.body` will be `undefined` unless appropriate `express.js`-compatible body parsing middlewares (like `body-parser`) are configured in the plugin options.
fix
Install `body-parser` (or similar) and add `bodyParser.json()` and/or `bodyParser.urlencoded({ extended: true })` to the `middlewares` array in your `vite.config.ts`.
affects: >=1.0.0
gotchaMock API patterns use Ant-style path matching. Misunderstanding this syntax can lead to mocks not being triggered correctly or unexpected routing behavior.
fix
Refer to the `@howiefh/ant-path-matcher` documentation for correct pattern syntax (e.g., `*` for single segment wildcard, `**` for multi-segment wildcard, `{var}` for path variables).
affects: >=1.0.0
Errors
Common errors & fixes
No mock handler found for URL: /api/your-path-here
The incoming request URL does not match any `pattern` defined in your mock files, or the `urlPrefixes` in the plugin configuration are incorrect.
fix
Verify that the `pattern` in your `MockHandler` definitions correctly matches the incoming request URL, considering Ant-style path matching. Also, check the `urlPrefixes` option in `vite.config.ts` to ensure it covers your API paths.
Property 'body' does not exist on type 'Request' OR req.body is undefined for POST/PUT requests.
The `Request` type or runtime `req.body` object is missing data because no middleware is processing the request body before it reaches your mock handler.
fix
Install `body-parser` (npm i body-parser -D) and add `bodyParser.json()` and `bodyParser.urlencoded({ extended: true })` to the `middlewares` array in your `vite.config.ts`.
Error: [vite-plugin-mock-server] Your Node.js version is X, but vite-plugin-mock-server requires >=12.0.0
The Node.js environment running Vite is older than the minimum version required by the plugin.
fix
Upgrade your Node.js installation to version 12.0.0 or higher. You can use tools like `nvm` to manage Node.js versions.
Upgrade
Version history
1.3.1latest on npm
Audit
Dependencies
viterequiredPeer dependency as it's a Vite plugin. Requires Vite >=2.0.0.
body-parseroptionalRequired for parsing request bodies (e.g., JSON, URL-encoded) when using the `middlewares` option.
cookie-parseroptionalRequired for parsing request cookies when using the `middlewares` option.
Agent activity
22 hits · last 30 days
node
18
OpenAI (training)
1
Resources
vite-plugin-mock-server — npm install vite-plugin-mock-server · libregistry