msw-request-assertions weekly downloads statistics
By marklai1998 · npm · Testing
Request assertions for MSW
msw-request-assertions has 7,775 recorded weekly downloads on npm, an increase of 863% over the past 30 days, ranking #1367 of 3,247 tracked listings.
About msw-request-assertions
MSW Request Assertions
<div align="center">A powerful testing library that provides custom assertion matchers for Mock Service Worker (MSW). Test your API interactions with confidence by asserting on request properties like headers, body, query parameters, and more.
Features | Installation | Migration | API | Contributing
</div>[!IMPORTANT]
Before using this library, please read MSW's official guidance on avoiding request assertions. The MSW team recommends testing how your application reacts to requests rather than asserting on request details. This library should primarily be used for edge cases, like request details have no observable application behavior.
✨ Features
- ✅ Assert on any aspect of HTTP and GraphQL requests
- ✅ Works with Vitest and Jest
📦 Installation
npm install --save-dev msw-request-assertions msw
# or
yarn add --dev msw-request-assertions msw
# or
pnpm add --save-dev msw-request-assertions msw
🚀 Quick Start
Setup for Vitest
msw-request-assertions/vitest ships Vitest 5 matcher types (Matchers<R, T>).
// vitest.setup.ts
import 'msw-request-assertions/vitest';
// vitest.config.ts
import { defineConfig } from 'vitest/config';
export default defineConfig({
test: {
setupFiles: ['./vitest.setup.ts'],
},
});
// tsconfig.json
{
"compilerOptions": {
// ...rest of your options
"types": ["vitest/globals", "msw-request-assertions/vitest"]
}
}
Requires Vitest 5.
Setup for Jest
[!NOTE]
Jest support is not a first-class citizen. This package is ESM only, so Jest must run in ESM mode. While we provide Jest compatibility, our main development and testing efforts are focused on Vitest. Feel free to raise an issue if you encounter Jest-specific problems.
// jest.setup.ts
import 'msw-request-assertions/jest';
// jest.config.js
module.exports = {
setupFilesAfterEnv: ['<rootDir>/jest.setup.ts'],
};
// tsconfig.json
{
"compilerOptions": {
// ...rest of your options
"types": ["vitest/globals", "msw-request-assertions/jest"]
}
}
Basic Example
import { http, HttpResponse } from 'msw';
import { setupServer } from 'msw/node';
// Create MSW handlers
const userHandler = http.post('/api/users', () => {
return HttpResponse.json({ id: 1, name: 'John Doe' });
});
const server = setupServer(userHandler);
beforeAll(() => server.listen());
afterEach(() => server.resetHandlers());
afterAll(() => server.close());
test('should create user with correct data', async () => {
const userData = { name: 'John Doe', email: 'john@example.com' };
await fetch('/api/users', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(userData),
});
// Assert the request was made
expect(userHandler).toHaveBeenRequested();
// Assert the request body
expect(userHandler).toHaveBeenRequestedWithJsonBody(userData);
// Assert multiple properties at once
expect(userHandler).toHaveBeenRequestedWith({
jsonBody: userData,
headers: { 'content-type': 'application/json' },
});
});
🔄 Migration
MSW 3 (breaking)
Peer dependency is msw@^3. Node.js 22 or newer is required.
GraphQL handlers are created from graphql.link() (msw/graphql). This library patches query and mutation on the link, so import the setup file before calling graphql.link().
import { HttpResponse } from 'msw';
import { graphql } from 'msw/graphql';
import 'msw-request-assertions/vitest';
const api = graphql.link('http://localhost/graphql');
const getUser = api.query('GetUser', () => {
return HttpResponse.json({ data: { user: { id: '1' } } });
});
Install graphql when you assert on GraphQL handlers. MSW 3 treats it as an optional peer.
server.listen({ onUnhandledRequest }) is now onUnhandledFrame in MSW itself.
toHaveBeenRequestedWithHash, toHaveBeenNthRequestedWithHash, and the hash field on toHaveBeenRequestedWith are removed. MSW 3 does not expose the URL fragment on the intercepted request.
ESM only (breaking)
The package ships ES modules only. The CommonJS build (dist/cjs, require()) is removed. Use import, or run Jest in ESM mode.
Vitest 5 only (breaking)
Vitest 3 and 4 are no longer supported, and the msw-request-assertions/vitest/v4 entry point is removed. msw-request-assertions/vitest types match Vitest 5's Matchers<R, T>. Upgrade to Vitest 5 and keep the existing import:
import 'msw-request-assertions/vitest';
💻 API…
Description from the npm listing.
- msw
- vitest
- vitest-plugin
- api
- mock
- mocking
- testing
README badge
Show your rank in a README or on your website. The badge updates automatically.
Charts
Images: 30d · 1y · all time · 1600×900 dark
Adoption over time
7/30/90-day growth
Ranking history
Listing placement
Listing placement
Filled points are observed values; hollow points come from Internet Archive snapshots of the listing page. Dashed segments span periods with no observations — values between points are not interpolated.
Each band is one page of 20. Weekly downloads on a linear scale. Placement is projected from weekly downloads, so it can differ from the marketplace's own sort order. Hover a dot to see the listing.
Recent observations
Full history (592)Similar npm Testing plugins
All 20 msw-request-assertions alternativesData source
Weekly downloads (trailing 7 complete days) come from the official npm downloads API. Downloads include CI and mirrors and are not equivalent to installs or users. npm publishes no ratings.
Listing: https://www.npmjs.com/package/msw-request-assertions. First seen Oct 16, 2026; last successful collection Dec 6, 2026. See the methodology.