Giter VIP home page Giter VIP logo

msw's Introduction

Package version Build status Dependencies status Dev dependencies status

MSW

Mock Service Worker (MSW) is a client-side API mocking library that operates by intercepting outgoing requests using Service Workers.

Features

  • Server-less. Doesn't establish any servers, operating entirely in a browser;
  • Deviation-free. Intercepts production URI requests from your page and mocks their responses, without having to deal with mocked URI.
  • Mocking as a tool. Enable/change/disable mocking on runtime instantly without any compilations or rebuilds. Control the MSW lifecycle from your browser's DevTools;
  • Essentials. Use Express-like syntax to define which requests to mock. Respond with custom status codes, headers, delays, or create custom response resolvers.

"This is awesome."

โ€“ Kent C. Dodds

Documentation

Quick look

$ npm install msw --save-dev

Now we have to put the mockServiceWorker.js file in your public directory. That is usually a directory being served by your server (i.e. public/ or dist/). The placing of the file is done by running the following command from your project's root directory:

$ npx msw init <PUBLIC_DIR>

For example, in a Create React App you would have to run: npx msw init public/.

MSW workflow consist of three phases:

// src/mocks.js
// 1. Import mocking utils
import { composeMocks, rest } from 'msw'

// 2. Define request handlers and response resolvers
const { start } = composeMocks(
  rest.get('https://github.com/octocat', (req, res, ctx) => {
    return res(
      ctx.delay(1500),
      ctx.status(202, 'Mocked status'),
      ctx.json({
        message: 'This is a mocked error',
      }),
    )
  }),
)

// 3. Start the Service Worker
start()

Import the mocks.js module into your application to enable the mocking.

// src/index.js
import './mocks'

Once enabled, any requests matching the defined paths will be intercepted by Service Worker, which would respond with mocked responses.

Chrome DevTools Network screenshot with the request mocked

Notice the 202 Mocked status (from ServiceWorker) status in the response.

There is a set of step-by-step tutorials to get you started with mocking the API type you need. Please refer to those tutorials below for more detailed instructions.

Tutorials

Examples

msw's People

Contributors

dashed avatar dependabot[bot] avatar hehehai avatar kettanaito avatar nodeteamdev avatar redraushan avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.