Registry / testing / koa-mock-response

koa-mock-response

JSON →
library1.0.0jsnpmunverified

Middleware for Koa that mocks REST APIs or backend services by matching routes to configurable responses. Version 1.0.0 requires Node >=10. Allows static/conditional/dynamic responses via JSON config or custom modules. Tightly coupled to Koa’s response API; no standalone usage. Low release cadence (no updates since 2020).

npm install koa-mock-response
INSTALL
IMPORT
SIG · KOA-MOCK-RESPONSE
K
koa-mock-response
testingjavascriptv1.0.0
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.

koaMockResponse
const koaMockResponse = require('koa-mock-response')
import koaMockResponse from 'koa-mock-response'
Package is CJS-only, no ESM export. Use require().
default export
const mock = require('koa-mock-response')
const { koaMockResponse } = require('koa-mock-response')
Exports a single middleware function directly, not a named export.
Koa usage
const Koa = require('koa'); const app = new Koa(); app.use(require('koa-mock-response')(config))
app.use(require('koa-mock-response'))
Must be called with a config object containing 'mocks' array.

Starts a Koa server that mocks two endpoints: a 200 JSON response and a 500 error.

const Koa = require('koa'); const koaMockResponse = require('koa-mock-response'); const app = new Koa(); const config = { mocks: [ { route: '/api/user', response: { status: 200, body: { id: 1, name: 'John' } } }, { route: '/api/error', response: { status: 500, body: 'Internal Server Error' } } ] }; app.use(koaMockResponse(config)); app.listen(3000, () => console.log('Mock server running on port 3000'));
Debug
Known issues
breakingOnly one config object can be passed; subsequent app.use() calls with mock middleware will overwrite previous routes.
fix
Consolidate all mocks into a single config object or use multiple Koa apps on different ports.
affects: >=1.0.0
gotchaThe 'responses' array property is defined on the mock object, but it's easily confused with the singular 'response'.
fix
Use 'response' for a single response, 'responses' for an array with conditional logic.
affects: >=1.0.0
gotchaDynamic mock modules require absolute paths or paths relative to the config file location (not the working directory).
fix
Use path.resolve(__dirname, 'path/to/module') for relative paths.
affects: >=1.0.0
deprecatedThe 'lws' keyword suggests this was part of the lws ecosystem, which is now unmaintained.
fix
Consider forking or migrating to a more active mock library like @wesleytodd/koa-mock.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: app.use() requires a generator or async function
koa-mock-response returns a non-async function that doesn't yield, incompatible with Koa 2+
fix
Ensure Koa version is >=2 but the middleware works with Koa 2's async/await style; actual issue is missing config object.
Error: Cannot find module 'koa-mock-response'
Package not installed or wrong import path
fix
Run: npm install koa-mock-response
Uncaught TypeError: Cannot read properties of undefined (reading 'mocks')
Config object not passed to middleware function
fix
Call koaMockResponse({ mocks: [...] }) and pass result to app.use.
Upgrade
Version history
1.0.0latest on npm
Audit
Dependencies
koarequiredPeer dependency: runs as Koa middleware
Agent activity
49 hits · last 30 days
node
42
OpenAI (training)
1
Resources
koa-mock-response — npm install koa-mock-response · libregistry