Releases: mswjs/msw
Releases · mswjs/msw
v0.23.0
Features
import { ResponseTransformer, compose } from 'msw'
import base64Image from 'url-loader!../../fixtures/image.jpg'
async function jpeg(base64: string): Promise<ResponseTransformer> {
const buffer = await fetch(base64).then((res) => res.arrayBuffer())
return compose(
context.set('Content-Length', buffer.byteLength.toString()),
context.set('Content-Type', 'image/jpeg'),
context.body(buffer),
)
}
const worker = setupWorker(
rest.get('/image', async (req, res, ctx) => {
return res(await jpeg(base64Image))
}),
})
- Publicly exposes the
compose
function (#467).
import { compose } from 'msw'
v0.22.3
v0.22.2
v0.22.1
v0.22.0
Breaking changes
- Service Worker now short-circuits navigation requests (#412, #417).
- Updates to
node-match-path
0.6.0.
Features
graphql.query('GetUser', (req, res, ctx) => {
return res(
ctx.data({ id: 'abc-123' }),
ctx.errors([
{ message: 'Failed to get user posts' }
])
)
})
Bug fixes
- Fixes an issue that resulted into inferred response status texts always set to "OK" (#438).
v0.21.3
Features
rest.get<RequestType, ResponseType, RequestParamsType>(url, resolver)
- Validates the value given to the
setupWorker
/setupServer
functions to be a spread list of handlers, not an Array (#400, #402).
Bug fixes
- Fixes an issue that caused a TypeScript compilation to fail when importing the
graphql
handler by exporting theGraphQLRequestParsedResult
type from the package (#425).
v0.21.2
v0.21.1
v0.21.0
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).
Internals
v0.20.5
Bug fixes
- Fixes an issue that resulted into a
Export has or is using name 'ParsedRestRequest' from external module
error message when compiling a TypeScript project using MSW (#325, #333). - Fixes an issue that resulted into a wrong Service Worker registration being picked up on platforms that register their own workers (i.e. Codesandbox) (#289).
Dependencies
- Updates
node-request-interceptor
to version0.3.5
(see Changelog).