2018-11-15 12:33:30 +01:00
2018-11-13 15:58:06 +01:00
2018-11-13 15:58:06 +01:00
2018-11-15 12:33:30 +01:00
2018-11-13 15:58:06 +01:00
2018-11-13 15:58:06 +01:00
2018-11-15 12:23:24 +01:00
2018-11-13 15:58:06 +01:00
2018-11-13 15:58:06 +01:00

Motivation

Problems of traditional mocking:

  • Often relies a mocking server which you need to run and maintain;
  • Doesn't really mock the requests, rather replaces the requests so they go to the mocking server, instead of the production server;
  • Brings extra dependencies to your application, instead of being a dependency-free development tool;

Getting started

Install

–

Configure routes

import { MSW } from '-'

/* Create a new instance of MockServiceWorker */
const msw = new MSW()

/* Configure mocking routes */
msw.get('https://api.github.com/repo/:repoName', (req, res) => {
  /* Access request's params */
  const { repoName } = req.params

  res
    /* Set custom response status */
    .status(403)
    /* Set headers */
    .set({ 'Custom-Header': 'foo' })
    /* Delay the response */
    .delay(1000)
    /* Mock the body */
    .json({
      errorMessage: `Repository "${repoName}" not found`
    })
})

/* Start the Service Worker */
msw.start()

How does this work?

MSW uses a Service Worker that analyzes the outgoing requests and matches them against the specified mocking routes. Whenever a request matches a route, the handler function is executed, and the mock is being sent as a response to that request.

S
Description
Industry standard API mocking for JavaScript.
Readme
14 MiB
Languages
TypeScript 97.4%
JavaScript 2.3%
HTML 0.2%