This release contains changes to the
mockServiceWorker.js
file. Please follow the instructions in your browser to upgrade the Service Worker file.
Breaking changes
ctx.fetch
utility now returns the entire response, not just the body (#373).
- const body = await ctx.fetch(req)
+ const response = await ctx.fetch(req)
+ const body = await response.json()
Features
- Adds support for mocking any GraphQL operation regardless of its type via
graphql.operation
API (#343).
import { graphql } from 'msw'
graphql.operation((req, res, ctx) => {
const { query, variables } = req.body
// Execute any GraphQL operation against a mock schema
// and return the response.
return executeSchema(mockSchema, { query, variables })
})
import { rest } from 'msw'
interface UserResponseBodyType {
firstName: string
}
rest.get<never, UserResponseBodyType>('/user', (req, res, ctx) => {
return res(ctx.json({ firstName: ‘John’ })
})
interface TodoRequestBodyType {
title: string
}
type TodoResponseBodyType = string[]
rest.post<TodoRequestBodyType, TodoResponseBodyType>('/todo', (req, res, ctx) => {
const { title } = req.body
return res(ctx.json(['one', title]))
})
- Adds support to
rest.head()
request handler to matchHEAD
method requests (#356). - Adds
findWorker
option toworker.start()
to locate the mock Service Worker script at a custom location (#335, #351). - Adds a keepalive mechanism between a client and the Service Worker to prevent the worker to be terminated due to inactivity (#367, #371).
ctx.json
now supports any data types that can be serialized to a valid JSON (#349, #350).
Bug fixes
- Fixes an issue that resulted into custom options to
worker.start()
not being deeply merged with the default ones (#347, #348). - Fixes an issue that resulted into
ctx.json()
utility function accepting only objects. Now it accepts any data that can be stringified into JSON (#349, #350). - Fixes an issue that resulted into “The “superCtor” argument must be of type function
”error message when usingsetupServer
(mswjs/interceptors#49, mswjs/interceptors#50). - GET and HEAD requests now forcefully contain no request body (#361, #366).