Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

msw-when-then

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

msw-when-then

A non-invasive 'when-then' style API for MSW

  • 1.5.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
955
decreased by-3.34%
Maintainers
1
Weekly downloads
 
Created
Source

logo

MSW When Then

A non-invasive `when-then` style API for MSW.

Npm Version Test status

Why?

MSW is a brilliant tool for mocking, but is missing a few things to make it perfect for testing. msw-when-then aims to help with that.

Notable Features

  • Succinct when-then style Api
  • Mock Chaining
  • Implicitly assert request data is correct
  • Support for both Rest and GraphQL requests

Installation

npm install msw-when-then

Usage

Initialise using MSW server and rest:

import { whenThen, get, ok } from "msw-when-then";

const { when } = whenThen(server, rest);

Then in your test:

when(get("https://some.url")).thenReturn(ok({ foo: "bar" }));

Features

Chaining Mocks

Familiar chaining pattern, the responses are return in order with the last response returned for all subsequent requests.

import { get, badRequest, ok } from "msw-when-then";

when(get("https://some.url"))
  .thenReturn(badRequest({ response: "first request" }))
  .thenReturn(ok({ response: "subsequent requests" }));

Custom Resolvers

Sometimes you need to take things into your own hands. We expose the original MSW resolver function, so you can do whatever you like. See MSW Docs for more details.

import { get } from "msw-when-then";

when(get("https://some.url")).then((req, res, ctx) => {
  // Any additional logic here
  return res(ctx.status(400), ctx.json({ response: "last response" }));
});

Implicitly assert request data

Mocking APIs is great, but how can we ensure our app is sending the right data? We can do this by specifying the expected request data when mocking.

Note: The id key in the withParams here matches the :id argument in the post url

import { post, request, withBody, withHeaders, withParams, ok } from "msw-when-then";

when(post("https://some.url/:id")).thenReturnFor(
  request(
    withBody({ foo: "bar" }),
    withHeaders({ "content-type": "application/json" }),
    withParams({ id: "expected-id" })
  ),
  ok({ response: "success" })
);

Keywords

FAQs

Package last updated on 19 Jan 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc