Packages

msw

3.0.7 · 18 Nov 2026MIT36M / weekRepositoryHomepagenpmAtom feed

Your version

0.21.0

10 Sep 2020Where it was published

0.49.3 is the newest release of 0.21.x.

In short

1 breaking6 new3 fixed2 changed

  • Breaking

    ctx.fetch utility now returns the entire response, not just the body (#373).

Release note

This release contains changes to the mockServiceWorker.js file. Please follow the instructions in your browser to upgrade the Service Worker file.

Breaking changes

  • Breaking

    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 (#49, #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).

A whole project to check? Paste its package.json in the dependency check: every deprecated, outdated or vulnerable dependency, with what to upgrade to. Free, no account.