BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Mock Service Worker Releases v3.0 with ESM-Only Build, Socket-Based Interception and GraphQL Subs

Mock Service Worker Releases v3.0 with ESM-Only Build, Socket-Based Interception and GraphQL Subs

Listen to this article -  0:00

Mock Service Worker, the API mocking library maintained by Artem Zakharchenko, has released version 3.0, nearly three years after the Fetch-based v2.0 rewrite. The release ships as ESM-only, completes the library's GraphQL story with subscription support, and introduces a socket-based interception algorithm in Node.js that Zakharchenko describes as going as deep into the runtime as JavaScript will allow without recompiling Node.

The headline breaking change is that MSW is now distributed natively as ECMAScript Modules only, with support for Node.js v18 and v20 dropped in favour of v22, v24, and v26. TypeScript 5.9 is now the minimum supported version. Several transitive dependencies have been removed, including graphql (now a proper optional peer), path-to-regexp, picocolors, and statuses. The result is a library tarball that drops from 416KB to 234KB, with type definitions shrinking by half, from 190KB to 90KB.

Alongside ESM, v3.0 introduces granular entrypoints that replace the single msw import with designated paths for each protocol:

import { http } from 'msw/http'
import { graphql } from 'msw/graphql'
import { sse } from 'msw/sse'
import { ws } from 'msw/ws'

GraphQL subscription support, a long-standing community request on the project's tracker, lands in this release. Building on the WebSocket interception shipped previously, subscription handlers can now be defined against a graphql.link() and publish events on demand, closing the gap between MSW's HTTP, WebSocket, and GraphQL coverage.

An experimental defineNetwork API is introduced as the eventual replacement for setupServer and setupWorker. The new primitive splits interception into sources (where traffic comes from) and handlers (how it is answered), allowing teams to replay HAR files, combine custom interceptors, or hand control of request timing to a test runner. The first ecosystem package to expose it directly is @msw/cloudflare, which wraps the API for Workers environments.

The most technically significant change sits in the underlying Interceptors library. Zakharchenko writes that after seven attempts at the problem, the team has moved network interception in Node.js down to the TCP and TLS wraps, the JavaScript bindings for Node's C-level network code. Moving any lower, he notes, would require recompiling Node.js itself. The new architecture supports arbitrary socket-based protocols, with SMTP cited as an early example. Nock already relies on the Interceptors library for its Node.js support.

A GitHub discussion reports failing tests after upgrading, traced to MSW now intercepting the browser-issued OPTIONS preflight requests that previous versions quietly ignored. Separately, a developer in discussion #2771 challenged the earlier decision to deprioritise Jest and JSDOM, arguing that a runner with 46 million weekly downloads warrants a formal breaking-change note rather than a quiet docs change.

In the Node-only space, MSW continues to compete with Nock, fetch-mock, and undici's built-in MockAgent, though as a recent comparison notes, none of those cover both browser and server with a single handler definition.

Teams on v2 should consult the official 2.x to 3.x migration guide before upgrading. Full release notes are available on GitHub.

Mock Service Worker is an open-source, MIT-licensed JavaScript library for mocking network traffic using Service Workers in the browser and runtime-level interception in Node.js. It is used widely for frontend and full-stack testing, and now covers HTTP, WebSocket, Server-Sent Events, and GraphQL including subscriptions.

About the Author

Rate this Article

Adoption
Style

BT