koa-mocks

0.6.1 • Public • Published

koa-mocks

koa2/express mock middleware

install

yarn add koa-mocks

init

node_modules/.bin/mock init

use

const koaMocks = require('koa-mocks')
app.use(koaMocks(config))

Parameter Description

  • config.basePath [string] The root directory of mock
    • default process.cwd()
  • config.routeFile [string] Mock routing file
    • default config.basePath + 'route.js'
  • config.staticFolder [string] Mock static file address
    • default config.basePath + 'static'
  • config.express [boolean] is whether use express
    • default false

route.js Explain

Example

[method]::<url> : <type>::<file>

module.exports = {
  'post::/test': 'mock::test.js',
  'get::/test2': 'mock::test.json',
  '/lock':'file::lock.png',
  '/baidu':'url::https://www.baidu.com'
}

Parameter description

  • method default get support any method and all
  • url Address of mock
  • type Type of mock
    • mock
    • file
    • url

test.js Explain

module.exports = function(req, res, utils) {
  return {
    "data": utils.random([111,222,333])
  }
}
  • req request Object
  • utils tool
    • utils.random() Random fetch of a data in an array

test.json Explain

{
	"$$statusCode":400,
	"$$delay":3000,
	"data":"111"
}
  • $$statusCode default 200
  • $$delay delay time default 0

TO DO LIST

Package Sidebar

Install

npm i koa-mocks

Weekly Downloads

2

Version

0.6.1

License

MIT

Unpacked Size

33.4 kB

Total Files

16

Last publish

Collaborators

  • laoguo4578963
  • sunxiaoyes