Skip to content

Cado-Labs/devmock

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DevMock · Supported by Cado Labs · CI · npm version

Webpack Dev Server mocking engine

Install

npm i --save devmock

or

yarn add devmock

And require it for further usage

const { DevMock } = require("devmock")

Usage

For webpack-dev-server < 4.0

devServer: {
  before: DevMock.v3({
    mocksPath: path.resolve("mocks/**/*.mock.js")
  })
}

For webpack-dev-server >= 4.0

devServer: {
  onBeforeSetupMiddleware: DevMock.make({
    mocksPath: path.resolve("mocks/**/*.mock.js")
  })
}

API

DevMock.make({
  mocksPath: "mocks/*.mock.js", // glob supported path to your mock files (required)
})

DevMock.make and DevMock.v3 has the same params.

Mock file format

// mocks/exmaple.mock.js

module.exports = {
  path: "/users",           // Query path (required)
  method: "GET",            // HTTP method (optional, default: GET)
  delay: 300,               // Response delay in ms (optional, default: 0)
  status: 200,              // Response status code (options, default: 200)
  response: [               // Response object (optional, default: {})
    { id: 1, name: "John" },
    { id: 2, name: "Mary" },
    { id: 3, name: "Bob" },
  ]
}

Both status and response params can be a function of request params:

module.exports = {
  path: "/users",
  status: params => params.error ? 422 : 200,
  response: params => params.error ? { success: false } : { success: true }
}

Contributing

  • Fork it ( https://github.com/Cado-Labs/devmock )
  • Create your feature branch (git checkout -b feature/my-new-feature)
  • Commit your changes (git commit -am '[feature_context] Add some feature')
  • Push to the branch (git push origin feature/my-new-feature)
  • Create new Pull Request

License

Released under MIT License.

Supporting

Supported by Cado Labs

Authors

Aleksei Bespalov