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

msw-inspector

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

msw-inspector

Inspect requests intercepted by MSW

  • 0.1.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
78
increased by6.85%
Maintainers
1
Weekly downloads
 
Created
Source

MSW inspector

Build status Npm version Test coverage report

Plug-and-play request assertion utility for any msw mock setup, as highly discouraged by msw authors :)

Why?

From msw docs:

Instead of asserting that a request was made, or had the correct data, test how your application reacted to that request.

There are, however, some special cases where asserting on network requests is the only option. These include, for example, polling, where no other side effect can be asserted upon.

MSW inspector has you covered for these special cases.

How

MSW inspector provides a thin layer of logic over msw life-cycle events.

Each request is saved as a function mock call retrievable by URL. This allows elegant assertions against request information like method, headers, body, query.

Example

This example uses Jest, but MSW inspector integrates with any testing framework.

import { createMSWInspector } from 'msw-inspector';
import { server } from '@/mocks/server';

const mswInspector = createMSWInspector({
  mockSetup: server,
  mockFactory: () => jest.fn(), // Provide any function mock supported by your testing library
});

beforeAll(() => {
  mswInspector.setup();
});

beforeEach(() => {
  mswInspector.clear();
});

afterAll(() => {
  mswInspector.teardown();
});

describe('My test', () => {
  it('My test', async () => {
    // Perform your test preparation

    expect(mswInspector.getRequests('http://my.url/path')).toHaveBeenCalledWith(
      {
        method: 'GET',
        headers: {
          'my-header': 'value',
        },
        body: {
          'my-body': 'value',
        },
        query: {
          'my-query': 'value',
        },
      }
    );
  });
});

API

createMSWInspector

Create a MSW inspector instance bound to a specific msw SetupServerApi or SetupWorkerApi instance:

import { createMSWInspector } from 'msw-inspector';

createMSWInspector({
  mockSetup, // Any `msw` SetupServerApi or SetupWorkerApi instance
  mockFactory, // Function returning a mocked function instance to be inspected in your tests
  requestMapper, // Optional mapper function to customize how requests are stored
});
Options object

createMSWInspector accepts the following options object:

 {
  mockSetup: SetupServerApi | SetupWorkerApi;
  mockFactory: () => FunctionMock;
  requestMapper?: (req: MockedRequest) => {
    key: string;
    record: Record<string, any>;
  };
}
OptionDescriptionDefault value
mockSetup (required)The instance of msw mocks expected to inspect (setupWorker or setupServer result)-
mockFactory (required)A function returning the function mock preferred by your testing framework: It can be () => jest.fn() for Jest, () => sinon.spy() for Sinon, () => vi.fn() for Vitest, etc...-
requestMapperCustomize default request's key and record mapping with your own logic.See defaultRequestMapper

getRequests

Returns a mocked function containing all the calls intercepted at the given absolute url (by default):

mswInspector.getRequests('http://my.url/path');

Each intercepted request calls the matching mocked function with the following default payload:

type CallPayload = {
  method: string;
  headers: Record<string, string>;
  body?: any;
  query?: Record<string, string>;
};

Todo

  • Consider a better name for getRequests
  • Consider listening to network layer with @mswjs/interceptors and make MSW inspector usable in non-msw projects
  • Consider accepting a function to customize requests mapping and payload

Keywords

FAQs

Package last updated on 02 Apr 2022

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