GitXplorerGitXplorer
d

msw-chaos-composition

public
4 stars
0 forks
0 issues

Commits

List of commits on branch main.
Unverified
941691078fea08b4edaa118611dc1a69b1268002

add npm package version badge

committed 3 years ago
Unverified
2810e08e3a83b36bc5ed515b7142ed4251b82f5f

Installation guide on README.md

committed 3 years ago
Unverified
c8848a164a4a8c297fa55b57ab1bb8bb5e2b9325

Merge branch 'main' of https://github.com/daisuke-awaji/msw-chaos-composition into main

committed 4 years ago
Unverified
a4c89b30d7106e2ed8c9a31b1c2d61eb3bb01a77

1.0.2

committed 4 years ago
Unverified
9b8879702865b2b518529f358b663187d35f647b

add package information

committed 4 years ago
Verified
66984c5e65c05c1962a13b238211a0b542203ca6

Update README.md

ddaisuke-awaji committed 4 years ago

README

The README file for this repository.

Chaos Mock Service Worker logo

msw-chaos-composition

msw-chaos-composition add chaos into the response of msw.

Features

  • Randomize Mock Service Worker response 🌪
  • Delay response time ⌛️

Install

npm install msw-chaos-composition

or

yarn add msw-chaos-composition

Usage

Basic msw usage

const handler = rest.get("/hello", (req, res, ctx) => {
  return res(
    ctx.status(200),
    ctx.json({
      message: "hello world",
    })
  );
});

const server = setupServer(handler);
server.listen();

fetch /hello then response is { status: 200, body: { message: "hello world" } }

Using mws-chaos-composition

Use chaosRes() created by createChaosResponse()

import { createChaosResponse } from "msw-chaos-composition";

const chaosRes = createChaosResponse();
const handler = rest.get("/hello", (req, res, ctx) => {
  return chaosRes(
    ctx.status(200),
    ctx.json({
      message: "hello world",
    })
  );
});

const server = setupServer(handler);
server.listen();

fetch /hello then one of the following will be returned as a response.

  • { status: 429, body: "Too Many Requests", }
  • { status: 500, body: "Internal Server Error" }
  • { status: 502, body: "Bad Gateway" }
  • { status: 503, body: "Service Unavailable" }
  • { status: 504, body: "Gateway Timeout" }

Params

Specify the response that will result in an error

const errors = [
  {
    status: 500,
    body: "Internal Server Error",
    delay: 500,
    rate: 10,
  },
  {
    status: 504,
    body: "Gateway Timeout",
    delay: 100000,
    rate: 20,
  },
];

const chaosRes = createChaosResponse(errors);
const handler = rest.get("/hello", (req, res, ctx) => {
  return chaosRes(
    ctx.status(200),
    ctx.json({
      message: "hello world",
    })
  );
});

const server = setupServer(handler);
server.listen();

fetch /hello then one of the following will be returned as a response.

  • { status: 500, body: "Internal Server Error" }
  • { status: 504, body: "Gateway Timeout" }