msw

Seamless REST/GraphQL API mocking library for browser and Node.js.

MIT License

Downloads
15.7M
Stars
14.9K
Committers
153

Bot releases are hidden (Show)

msw -

Published by kettanaito about 4 years ago

Bug fixes

  • Fixes an issue that resulted into the first matching request handler being treated as the only relevant request handler, even if it doesn't return any mocked response (#234, #258, #265).
msw -

Published by kettanaito about 4 years ago

Features

  • Adds graphql.link request handler that allows to mock GraphQL operations on per-endpoint basis (#315, #319).
import { setupWorker, graphql } from 'msw'

const github = graphql.link('https://api.github.com/graphql')
const stripe = graphql.link('https://api.stripe.com/graphql')

const worker = setupWorker(
  github.query('GetUser', resolver),
  stripe.mutation('Payment', resolver),
)

worker.start()

Bug fixes

  • Fixes a TypeScript issue that resulted into the following error (#321, #322):
ERROR in node_modules/msw/lib/types/context/cookie.d.ts:1:8 - error TS1192:
Module '"/node_modules/@types/cookie/index"' has no default export.
msw -

Published by kettanaito about 4 years ago

Bug fixes

  • Fixes an issue with the node-fetch dependency that wasn't listed as a save dependency (#308)
  • Fixes an issue that resulted into the React Native build module at /native not being published to NPM (#309)
msw -

Published by kettanaito about 4 years ago

Breaking changes

  • composeMocks function has been removed (#236, #239). Please use setupWorker instead.
-import { composeMocks, rest } from 'msw'
-composeMocks(rest.get(...))

+import { setupWorker, rest } from 'msw'
+setupWorker(rest.get(...))

Features

  • Adds React Native support (#203, #269).
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

const server = setupServer(
  rest.get('/books', (req, res, ctx) => res())
)

server.listen({
  onUnhandledRequest: 'error'
})

fetch('/user') // ERROR! Unhandled request.
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).
msw -

Published by kettanaito over 4 years ago

Features

  • Adds a warning when using redundant query parameters in a request handler URL (#231, #233).
  • Throws an exception when running setupWorker in a NodeJS environment (#219).
  • Throws an exception when running setupServer in a browser environment (#214, #219).
  • Adds a custom error message when calling worker.start() and the worker script doesn’t exist (#237, #242).
  • Exports GraphQL type annotations for GraphQLMockedRequest, GraphQLMockedContext, GraphQLRequestPayload, GraphQLResponseResolver types (#213, #241).

Bug fixes

  • Sets the default realistic response time of ctx.delay() to 5ms when run in Node environment (including jsdom) (#205).
  • Fixes an issue that resulted into the response Promise to never resolve when using jest.useFakeTimers() in your tests (#243).
  • Fixes a bug that resulted into a request handler URL with query parameters to produce a false negative match with the identical actual request URL. Query parameters and hashes in a request handler URL no longer affect the matching process.
msw -

Published by kettanaito over 4 years ago

Internal

  • Updates the repository's homepage and README.
msw -

Published by kettanaito over 4 years ago

Bug fixes

  • Fixes an issue that resulted into GraphQL requests issued using a GET method not being intercepted (#210, #212).
msw -

Published by kettanaito over 4 years ago

Bug fixes

  • Fixes an issue that resulted into TypeError [ERR_INVALID_URL]: Invalid URL: undefined//undefined exception when using third-party request issuing libraries in Node (#209, #211).
msw -

Published by kettanaito over 4 years ago

Features

  • Calling ctx.delay() without any arguments now simulates a random realistic server response time in a range between 100-400ms (#192, #198). Responses in Node are instantaneous.

Bug fixes

  • Fixes an issue that resulted into Uncaught (in promise) SyntaxError: Unexpected end of input exception when processing a request. Internally, a request’s body is now always retrieved as text, parsing it as JSON is delegated to the client side (based on the Content-Type header) (#200, #201).
msw -

Published by kettanaito over 4 years ago

Features

  • The library now defers all the client-side requests that happen between calling worker.start() and successful worker activation. This eliminates race condition between the two, and now comes as the default behavior. (#190, #196)

The behavior of deferring requests can be configured using the waitUntilReady: boolean option on worker.start():

import { setupWorker, rest } from 'msw'

const worker = setupWorker(/* request handlers */)

worker.start({
  // You can opt-out of the deferred network requests behavior.
  waitUntilReady: false
})
msw -

Published by kettanaito over 4 years ago

Bug fixes

  • Fixes an outdated repository URL in the Service Worker activation message.
  • Fixes an issue that resulted into ReferenceError: location is not defined error when using a request handler with a relative URL in setupServer (#193)
msw -

Published by kettanaito over 4 years ago

Features

rest.get('/books', (req, res, ctx) => {
  return res.once(ctx.json([1, 2, 3]))
})

Bug fixes

  • Fixes an issue that resulted into the following TypeScript error when installing msw (#185, #186 ):
TS7016: Could not find a declaration file for module 'cookie'
  • Fixes an outdated GitHub repository link in the mockServiceWorker.js file.
msw -

Published by kettanaito over 4 years ago

Bug fixes

  • Fixes an issue that caused an empty non-JSON request body being parsed as JSON (#175, #179)
msw -

Published by kettanaito over 4 years ago

Bug fixes

  • Fixes an issue that resulted into responses to axios requests not being mocked properly (#180, #181).

Internal

  • Lists node-request-interceptor as an external dependency of the msw/node bundle.
msw -

Published by kettanaito over 4 years ago

Features

  • Custom request handlers have reworked lifecycle and accept new methods:
    • parse(), to retrieve additional information about the request before the predicate.
    • getPublicRequest(), to modify the original request with public information (i.e. adding req.params or req.variables).

Bug Fixes

  • Fixes an issue that resulted into GraphQL mutations not being logged properly (#161, #176)
msw -

Published by kettanaito over 4 years ago

Features

  • GraphQL requests are now being printed into browser’s console using a proper format (operation name / response status) (#161, #174)
  • Each custom request handler can have its own log function that controls what and how gets printed into browser’s console for introspection:
// my-handler.js
export const myHandler = () => {
  return {
    log(req, res, handler) {
      console.log('%s %s', req.method, req.url.href)
    }
  }
}

The information available in the log function:

  • req, intercepted request instance;
  • res, resolved mocked response object;
  • handler, exact request handler that captured this request.

Bug fixes

  • Fixes an issue that resulted into a request body with the Content-Type: application/json to be unparsed in req.body reference of the request handler (#171, #172).
msw -

Published by kettanaito over 4 years ago

Bug fixes

  • Fixes an issue that resulted into a relative request's URL being printed as absolute in the browser's console (#164, #167).
  • Fixes an issue that resulted into a request JSON body not being parsed inside a request handler (#159, #168).
msw -

Published by kettanaito over 4 years ago

This release introduces bug fixes to the Service Worker file. Please follow the update instructions in your browser's console. Thanks.

Breaking changes

  • The req.url reference in request handlers is now a URL instance (#158):
rest.get('/user', (req) => {
-  req.url // "/user"
+  req.url.href // "/user"
})

Features

  • Adds support for running in NodeJS (#104, #146, docs). This makes it possible to use the same mock definition for unit and integration tests.
// test/LoginForm.test.js
import { rest } from 'msw'
import { setupServer } from 'msw/node'

describe('LoginForm', () => {
  const server = setupServer(
    rest.post('/login', (req, res, ctx) => {
      return res(ctx.json({ success: true }))
    })
  )

  beforeAll(() => {
    server.listen()
  })

  afterAll(() => {
    server.close()
  })

  it('allows a user to log in', () => {
    // your assertions here...
  })
})

Bug fixes

  • Fixes an issue that resulted into the last value of a multi-value header being available in req.headers (#154)
msw -

Published by kettanaito over 4 years ago

This release contains changes to mockServiceWorker.js file. Please follow the instructions in your browser's console to update the worker file.

Bug fixes

  • Fixes an issue that resulted into failed mocking with multiple pages open after page refresh (#139, #155).
  • Fixes an issue that resulted into the following exception when using ctx.fetch() (#150, #151):
Failed to execute 'text' on 'Response': body stream is locked
  • Fixes an issue that resulted into nested paths attempting to register the mockServiceWorker.js file relatively to the path, instead of the root (#152, #153).
msw -

Published by kettanaito over 4 years ago

Bug fixes

  • Fixed an issue that resulted into request headers not being propagated to an actual request when calling ctx.fetch() to perform response patching (#145, #149).
  • Fixed an issue that resulted into an exception when trying to register a Service Worker in a non-supported environment (legacy browser, or custom hostname) (#147, #148).