How to use msw - 7 common examples

To help you get started, we’ve selected a few msw examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github pipe-cd / pipe / pkg / app / web / src / mocks / create-handler.ts View on Github external
export function createHandlerWithError(
  serviceName: string,
  statusCode: number
): HandlerType {
  return rest.post(createMask(serviceName), (_, res, ctx) => {
    return res(
      ctx.status(200),
      ctx.set({
        "content-length": "0",
        "content-type": "application/grpc-web+proto",
        "grpc-message": "Error Message",
        "grpc-status": `${statusCode}`,
      })
    );
  });
}
github pipe-cd / pipe / pkg / app / web / src / mocks / create-handler.ts View on Github external
export function createHandler(
  serviceName: string,
  getResponseMessage: (requestData: Uint8Array) => T
): HandlerType {
  return rest.post(
    createMask(serviceName),
    (req, res, ctx) => {
      const arr: Uint8Array =
        typeof req.body === "string" ? encoder.encode(req.body) : req.body;
      const message = getResponseMessage(arr.slice(5));
      const data = serialize(message.serializeBinary());
      return res(
        ctx.status(200),
        ctx.set("Content-Type", "application/grpc-web+proto"),
        ctx.body(data)
      );
    }
  );
}
github pipe-cd / pipe / pkg / app / web / src / mocks / create-handler.ts View on Github external
import { rest } from "msw";
import { serialize } from "./serializer";
import { createMask } from "./utils";

const DummyHandler = rest.post("", (_0, res, ctx) => {
  return res(ctx.status(200));
});

type HandlerType = typeof DummyHandler;

const encoder = new TextEncoder();

export function createHandler(
  serviceName: string,
  getResponseMessage: (requestData: Uint8Array) => T
): HandlerType {
  return rest.post(
    createMask(serviceName),
    (req, res, ctx) => {
      const arr: Uint8Array =
        typeof req.body === "string" ? encoder.encode(req.body) : req.body;
github open-draft / msw / example / src / mocks / index.js View on Github external
import { composeMocks, rest } from 'msw'
import userMocks from './user'

const { start } = composeMocks(
  ...userMocks,
  rest.get('https://api.github.com/users/:username', (req, res, { json }) => {
    const { username } = req.params

    return res(
      json({
        avatar_url: 'https://i.imgflip.com/wnv7r.jpg',
        login: username,
        name: 'John Maverick',
        public_repos: Math.ceil(Math.random() * 100),
      }),
    )
  }),
)

start()
github open-draft / msw / example / src / mocks.js View on Github external
import { msw } from 'msw'

msw.get('https://api.github.com/users/:username', (req, res, { json }) => {
  const { username } = req.params

  return res(
    json({
      avatar_url: 'https://i.imgflip.com/wnv7r.jpg',
      login: username,
      name: 'John Maverick',
      public_repos: Math.ceil(Math.random() * 100),
    }),
  )
})

msw.start()
github open-draft / msw / example / src / mocks.js View on Github external
import { msw } from 'msw'

msw.get('https://api.github.com/users/:username', (req, res, { json }) => {
  const { username } = req.params

  return res(
    json({
      avatar_url: 'https://i.imgflip.com/wnv7r.jpg',
      login: username,
      name: 'John Maverick',
      public_repos: Math.ceil(Math.random() * 100),
    }),
  )
})

msw.start()
github open-draft / msw / example / src / mocks / index.js View on Github external
import { composeMocks, rest } from 'msw'
import userMocks from './user'

const { start } = composeMocks(
  ...userMocks,
  rest.get('https://api.github.com/users/:username', (req, res, { json }) => {
    const { username } = req.params

    return res(
      json({
        avatar_url: 'https://i.imgflip.com/wnv7r.jpg',
        login: username,
        name: 'John Maverick',
        public_repos: Math.ceil(Math.random() * 100),
      }),
    )
  }),
)

start()

msw

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

MIT
Latest version published 7 days ago

Package Health Score

88 / 100
Full package analysis