mirror of
https://github.com/hanzoai/msw.git
synced 2026-09-24 17:00:34 +00:00
9831c09952a2017c588790fdd9ad7f9771ea67c2
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.
Languages
TypeScript
97.4%
JavaScript
2.3%
HTML
0.2%