Skip to content

Mock Service Worker 3.0 drops CommonJS-era builds, intercepts at the socket level and adds GraphQL subscriptions

Mock Service Worker has released version 3.0 as an ESM-only package, with a new Node.js interception layer that works at the TCP and TLS level and support for GraphQL subscriptions. Teams upgrading from v2 face breaking changes.

3 min readBased on 1 source16 views

TL;DR

  • Mock Service Worker (MSW) 3.0 ships as ECMAScript Modules only and drops support for older Node.js releases.
  • Node.js network interception now happens at the TCP and TLS layer, below the HTTP stack, which lets the library cover more protocols.
  • GraphQL subscriptions are supported for the first time, and some OPTIONS preflight requests that were previously ignored are now intercepted, which can break existing test suites.

What changed in the package

MSW is an open-source, MIT-licensed library for mocking network traffic. It uses Service Workers in the browser and runtime-level interception in Node.js. Its maintainer, Artem Zakharchenko, released version 3.0 nearly three years after the Fetch-based v2.0 rewrite. As InfoQ put it, the headline change is that the package is now distributed natively as ECMAScript Modules (ESM) only. ESM is the standard JavaScript module format, and it replaces the older CommonJS require() style that many test setups still use.

The release also removes several transitive dependencies, including graphql, which becomes an optional peer dependency, along with path-to-regexp, picocolors and statuses. The effect on size is significant: the library tarball falls from 416KB to 234KB, and the type definitions shrink from 190KB to 90KB. TypeScript 5.9 is now the minimum supported version.

Instead of a single entry point, v3.0 introduces granular imports for each protocol, such as msw/http, msw/graphql, msw/sse and msw/ws. Server-Sent Events and WebSocket coverage were already part of the project, so the split mainly makes the imports clearer.

Interception moves down to the socket layer

The most technically significant change is in the Interceptors library, which MSW uses in Node.js. Zakharchenko writes that after seven attempts at the problem, the team moved interception down to the TCP and TLS wraps, the JavaScript bindings for Node's C-level network code. Going lower, he notes, would require recompiling Node.js itself.

For developers, this means the interceptor can handle arbitrary socket-based protocols. SMTP is cited as an early example. Nock also relies on the Interceptors library for its Node.js support, so changes there reach that project too. Among Node-only mocking tools, MSW competes with Nock, fetch-mock and undici's built-in MockAgent. A recent comparison, as InfoQ reports, notes that none of them cover both browser and server with a single handler definition.

GraphQL subscriptions were a long-standing request on the project's tracker. In v3.0, subscription handlers are defined against graphql.link() and can publish events on demand. This builds on the WebSocket interception added earlier and closes the gap between HTTP, WebSocket and GraphQL coverage.

The release also adds an experimental defineNetwork API, intended as the eventual replacement for setupServer and setupWorker. It separates interception into sources, which describe where traffic comes from, and handlers, which decide how it is answered. Teams can use it to replay HAR files, combine custom interceptors or let a test runner control request timing. The first ecosystem package to expose it directly is @msw/cloudflare, which wraps the API for Cloudflare Workers.

Compatibility and migration

The upgrade is not drop-in. A GitHub discussion reports failing tests after upgrading. The cause is that MSW now intercepts browser-issued OPTIONS preflight requests, which earlier versions quietly ignored. Test suites that assumed those requests never reached the mock layer may need updating.

Another discussion challenges the earlier decision to deprioritise Jest and JSDOM support. The commenter argues that a runner with 46 million weekly downloads deserves a formal breaking-change note rather than a quiet documentation change. Teams that rely on Jest should check the release notes before upgrading.

Teams on v2 should follow the official 2.x to 3.x migration guide. The full release notes are on GitHub.

Why it matters

For frontend and full-stack teams, the main gain is one mocking library that covers HTTP, WebSocket, Server-Sent Events and GraphQL subscriptions in both browser and Node.js tests. The smaller package and the granular imports should also reduce install size and make the dependency tree easier to audit.

The cost is migration work. Projects still on CommonJS need to switch to ESM, raise their TypeScript version and check that their test runner handles the new module format. Anyone who depended on unintercepted preflight requests should review their handlers. Because the Node.js interception now sits lower in the stack, it is also worth checking any Nock-based setup that shares the same underlying library.

Sources

This article was written by Blozhik from the reporting below. Read the originals for full detail.

InMock Service Worker Releases v3.0 with ESM-Only Build, Socket-Based Interception and GraphQL SubsInfoQ
Was this useful?

Keep reading

Discussion · 0

Sign in to join the discussion. Liking does not need an account.Sign inRegister
Loading comments…
blozhikDeveloper news from across the web, explained. Every story links to its original sources.