API Mocking English: MSW v2 and Testing Vocabulary

Learn the English vocabulary of API mocking with MSW v2 — request handlers, interceptors, stubs, fixtures, and network boundary — for clearer testing discussions.

Introduction

MSW (Mock Service Worker) is the leading library for intercepting and mocking HTTP requests in JavaScript applications. Version 2 brought a redesigned API and first-class support for Node.js, making it useful for both browser tests and server-side unit tests. For non-native English speakers, the vocabulary of API mocking — intercept, stub, fixture, passthrough, handler override — can be particularly challenging because these words have common English meanings that differ from their technical usage. This post clears up the confusion and teaches you to use these terms correctly.

Mocking vs. Stubbing

Understanding the difference between mock and stub will immediately improve the precision of your technical communication.

A stub is a replacement for a dependency that returns a fixed, predefined response without any logic. “We replaced the payment API with a stub that always returns a successful charge response, so we can test the checkout flow without a real credit card.” A stub does not verify how it was called.

A mock is similar but also records how it was called and can assert on that behaviour. “Use a mock for the email service so we can verify that exactly one confirmation email was sent after a successful registration.” In everyday usage, engineers often say “mock” for both concepts — but in precise testing discussions, the distinction matters.

Test isolation is the principle that each test should run independently, without being affected by other tests or by real network calls. API mocking is one of the main tools for achieving test isolation. “Without test isolation, a flaky external API can cause unrelated tests to fail intermittently.”

Intercepting Requests

To intercept a request means to capture an outgoing HTTP request before it reaches the actual server and handle it within your test environment. MSW uses a service worker in the browser and a Node.js interceptor to do this transparently. “MSW intercepts every fetch and XMLHttpRequest call in the browser, so your application code does not need to change at all.”

A request handler is the function you define in MSW that matches a specific URL and method, and returns a response. “Add a request handler for GET /api/users that returns a list of three fake users.” In MSW v2, handlers are written using the http.get(), http.post() helpers.

A response resolver is the function inside a handler that receives the intercepted request and returns the mock response. “In the response resolver, check the request body to decide whether to return a success or an error response.”

A network boundary is the conceptual line between your application code and external services. Good tests should not cross the network boundary into real external APIs — they should stop at the mock. “MSW sits at the network boundary, so your tests never make real HTTP calls to production servers.”

Passthrough and Handler Overrides

A passthrough is an instruction to MSW to let a specific request through to the real server instead of mocking it. “We configured a passthrough for the feature-flag API so that tests use real feature flags from staging, while all other requests are mocked.”

A handler override is a temporary handler you add within a single test to change the default mock behaviour. The override applies only for that test, and the default handler is restored afterwards. “In the error-state test, we add a handler override that makes the user endpoint return a 500 status code, so we can verify that the error boundary renders correctly.”

Fixtures

A fixture is a file containing sample data used in tests — for example, a JSON file representing an API response. “Store the mock API responses as JSON fixtures so that multiple tests can share the same sample data without repeating it.”

Using fixtures makes tests more readable and easier to maintain. “When the API schema changes, update the fixture file in one place instead of hunting through every test file.” In MSW, you import fixture data into your request handlers to build realistic mock responses.

A service worker is the browser technology that MSW uses to intercept network requests in a browser environment. The service worker sits between the browser and the network, giving MSW a chance to return a mock response before the request goes anywhere. “Install the MSW service worker in your public directory and register it in your test setup file.”

Key Vocabulary

TermDefinition
mockA test replacement that also records and verifies how it was called
stubA test replacement that returns a fixed response without verifying usage
request handlerA function in MSW that matches a URL and method and returns a mock response
interceptorA mechanism that captures outgoing requests before they reach the network
passthroughAn instruction to let a specific request reach the real server
handler overrideA temporary handler that changes mock behaviour for a single test
fixtureA file containing sample data shared across multiple tests
network boundaryThe conceptual line between application code and external services

Practice Tips

  1. Use “intercept” as a transitive verb. We intercept a request, never intercept on a request. “MSW intercepts the fetch call and returns the mock response.” The noun form is “interception”: “The interception happens transparently at the service worker level.”

  2. Practise explaining test isolation to non-technical stakeholders. A good English explanation: “We mock the external payment API in tests so that a problem with the payment provider never causes our own tests to fail.” Keep it focused on the benefit, not the mechanism.

  3. Learn fixed expressions with “mock”. Common phrases: “mock the API”, “set up a mock”, “mock a response”, “mock data”. The word “mock” is flexible — it works as a verb, noun, and adjective. Pay attention to how native speakers switch between these forms naturally.

  4. Distinguish “handler” from “middleware”. A handler processes a specific matched request. Middleware processes all requests before they reach handlers. In MSW, http.get('/users', resolver) is a handler; a logging wrapper around all handlers behaves like middleware. Using the right term in code reviews signals engineering maturity.

Conclusion

API mocking is a foundational testing skill, and MSW v2 makes it practical for both browser and Node.js environments. The vocabulary — intercept a request, mock a response, test isolation, network boundary, handler override, fixture — describes specific engineering decisions that directly affect test reliability and maintainability. When you can use these terms accurately in pull request discussions and documentation, you communicate as a testing professional, not just someone who writes tests. This vocabulary is worth investing in.