Breaking changes
-import { composeMocks, rest } from 'msw'
-composeMocks(rest.get(...))
+import { setupWorker, rest } from 'msw'
+setupWorker(rest.get(...))
Features
import { setupServer } from 'msw/native'
import { rest } from 'msw'
const server = setupServer(
rest.get('/user', (req, res, ctx) => {
return res(ctx.json({ firstName: 'John' })
})
)
The
setupServer
API for React Native is the exact mirror of the same API for NodeJS. Read more in the documentation: https://mswjs.io/docs/api/setup-server
- Adds support for binary response types (#221, #224, Binary response type recipe).
- Adds an option to warn/error/execute arbitrary logic on unmatched request (#191,
setupWorker
docs,setupServer
docs).
const server = setupServer(
rest.get('/books', (req, res, ctx) => res())
)
server.listen({
onUnhandledRequest: 'error'
})
fetch('/user') // ERROR! Unhandled request.
- Adds a
req.cookies
property to access a request’s cookies (#199, #261, Accessing request cookies recipe). - Adds support for network error emulation (#253,
res.networkError
API):
rest.post('/login', (req, res, ctx) => {
return res.networkError('Custom error message')
})
Bug fixes
- Fixes an issue that resulted into a "global is not defined" error being thrown in a browser (#255).
- Fixes an issue that resulted into the worker listeners being established multiple times after repetitive calls to
worker.start()
or Hot Reload (#244). - Fixes an issue that failed response patching in NodeJS (#264, #266).
- Fixes an issue that resulted into the
XMLHttpRequest.prototype.addEventListener
events being ignored (#267). - Fixes an issue that resulted into timeout in POST requests with a body in NodeJS (#277).
- Fixes an invalidly typed
req.body
in case of GraphQL request (#297, #302). - Updates library's dependencies (#306).