Skip to content

Releases: mswjs/msw

v0.23.0

29 Nov 12:58
Compare
Choose a tag to compare

Features

  • Supports asynchronous response transformers (#465, #467).
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

24 Nov 11:36
Compare
Choose a tag to compare

Features

v0.22.2

23 Nov 16:18
Compare
Choose a tag to compare

Bug fixes

  • Fixes an issue that resulted into a "Duplicate parameter" exception in Safari (#470).
  • Fixes an issue where the setupServer function had no explicitly provided return type signature (#458).

v0.22.1

19 Nov 14:19
Compare
Choose a tag to compare

Bug fixes

  • Fixes an issue that resulted into Content-Type: application/json request body not being parsed as a JSON whenever it had an additional charset header value (#462, #463).

Internals

  • Updates package dependencies.

v0.22.0

18 Nov 11:18
Compare
Choose a tag to compare

Breaking changes

Features

  • Supports setting both ctx.data and ctx.errors in GraphQL responses (#401, #403).
graphql.query('GetUser', (req, res, ctx) => {
  return res(
    ctx.data({ id: 'abc-123' }),
    ctx.errors([
      { message: 'Failed to get user posts' }
    ])
  )
})
  • Supports creation of custom response transformers (#440, #441).

Bug fixes

  • Fixes an issue that resulted into inferred response status texts always set to "OK" (#438).

v0.21.3

18 Oct 10:51
Compare
Choose a tag to compare

Features

  • Adds support for type annotation of path parameters (#393, #421).
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 the GraphQLRequestParsedResult type from the package (#425).

v0.21.2

12 Sep 09:12
Compare
Choose a tag to compare

Bug fixes

  • Fixes type annotations of ResponseTransformer to drop the usage of the unknown generics causing type violations in strict mode (#381, #382).

v0.21.1

11 Sep 18:43
Compare
Choose a tag to compare

Bug fixes

  • Fixes an issue that resulted into TypeScript violations in strict mode (#377, #379).

v0.21.0

10 Sep 08:56
Compare
Choose a tag to compare

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 })
})
  • Adds support for request and response body type generics to rest.* request handlers (#345, #352).
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 match HEAD method requests (#356).
  • Adds findWorker option to worker.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 using setupServer (mswjs/interceptors#49, mswjs/interceptors#50).
  • GET and HEAD requests now forcefully contain no request body (#361, #366).

Internals

  • Adjusts internal tooling to support contributors with Windows machines (#363, #364).
  • Updates dependencies (#376).

v0.20.5

11 Aug 09:49
Compare
Choose a tag to compare

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 version 0.3.5 (see Changelog).