DEV Community

Daniel Zotti
Daniel Zotti

Posted on

#LearnedToday: MSWjs

Do you need to reuse the same mock definition for testing, development, and debugging?

Try Mock Service Worker!

It’s a library that uses Service Worker API to intercept requests on the network level and it replies with your mock data.

Just install it

npm install msw --save-dev
Enter fullscreen mode Exit fullscreen mode

and configure your APIs:

// src/mocks/handlers.js
import { setupWorker, rest } from 'msw'

const worker = setupWorker(
  rest.post('/login', async (req, res, ctx) => {
    const { username } = await req.json()

    return res(
      ctx.json({
        username,
        firstName: 'John'
      })
    )
  }),
)

worker.start()
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Image of Docusign

πŸ› οΈ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more