Mock Service Worker

Mock Service Worker

mswjs.io

1

About this website

Mock Service Worker is an API mocking library created by Artem Zakharchenko in 2019, providing a seamless way to intercept and mock network requests at the service worker level, enabling developers to test, develop, and debug applications without a real backend by intercepting HTTP and WebSocket requests and returning configurable mock responses, with support for REST and GraphQL APIs, adopted by over three million weekly npm users and by companies including Vercel, GitLab, and Atlassian for their development and testing workflows. The service worker interception works by registering a service worker in the browser that intercepts all network requests matching configured handlers, returning mock responses instead of making real network calls, enabling full-stack development and testing without a backend server, with the mocks transparent to the application code that continues making fetch and XMLHttpRequest calls as normal, unaware that responses are mocked. The request handler system defines how requests are matched and responded to, with REST handlers matching by HTTP method and URL pattern using path parameters, query parameters, and headers, and GraphQL handlers matching by operation type and name, with response resolvers returning status codes, headers, body content, and delays, supporting context utilities for setting response properties, reading request data, and composing responses from multiple handlers. The error scenarios and edge cases can be mocked by returning error responses, network errors, or delayed responses, enabling comprehensive testing of error handling, loading states, timeout behavior, and retry logic. The Node.js support through request interception without service workers. The integration with testing frameworks including Jest, Vitest, Playwright, and Cypress. The request logging. The response composition. Designed for frontend developers and testers.

Tags & Categories

Categories

Statistics

1
Views
0
Clicks
0
Like
0
Dislike

Comments

Log In to post a comment

No comments yet. Be the first!