Skip to content

How to create an asynchronous mock response

Lloyd Brookes edited this page Jun 24, 2019 · 2 revisions

DEPRECATED. The --mocks option and lws-mock-response modules were removed in v3.

To make a response asynchronous, use a response function which returns a Promise which resolves when complete.

module.exports = MockBase => class MyMockModule extends MockBase {
  mocks (options) {
    return [
      {
        route: '/',
        responses: [
          {
            response: function (ctx) {
              return new Promise((resolve, reject) => {
                setTimeout(() => {
                  ctx.body = '<h1>You waited 2s for this</h1>'
                  resolve()
                }, 2000)
              })
            }
          }
        ]
      }
    ]
  }
}
Clone this wiki locally