Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
fc8c4ba
feat(wip): implement the `defineNetwork` API
kettanaito Jan 10, 2026
30f32a8
fix(ws): implement the handler lookup handling
kettanaito Jan 10, 2026
f65e91d
fix: implement `setupServer` and `SetupServerCommonApi`
kettanaito Jan 11, 2026
2b64a7f
fix: implement `setupWorker`
kettanaito Jan 11, 2026
980f4be
chore: remove legacy `msw/node` apis
kettanaito Jan 11, 2026
92d4fb7
chore: comment on the `WebSocketInterceptor` type error
kettanaito Jan 11, 2026
28d69b4
fix: annotate network frames on source level as unknown
kettanaito Jan 13, 2026
c7bc89e
chore: use instanceof checks for frame handling
kettanaito Jan 13, 2026
9caaf66
chore: ignore `BatchInterceptor` listener type inference issue
kettanaito Jan 13, 2026
3f4b90f
chore: do not reference `~/core` in core
kettanaito Jan 13, 2026
d3659be
chore: use `rettime` for life cycle events
kettanaito Jan 13, 2026
2ee72e3
fix(ts): infer event maps from network sources
kettanaito Jan 13, 2026
c47b26f
chore: remove old `setupWorker`
kettanaito Jan 13, 2026
1a9a94b
chore: use `#core` import alias
kettanaito Jan 14, 2026
2d33af0
fix(setupWorker): add node.js environment check
kettanaito Jan 14, 2026
bfbe16c
chore: fix some tests
kettanaito Jan 14, 2026
12ddb9f
fix(onUnhandledFrame): check for common assets after function check
kettanaito Jan 14, 2026
48160ec
fix: use node 24, surface lookup errors
kettanaito Jan 14, 2026
7e2bf32
chore: fix type issues, run on node 22
kettanaito Jan 14, 2026
36d8f5a
fix(setupWorker): print stop message on `.disable()`
kettanaito Jan 14, 2026
cb28c95
fix(defineNetwork): merge `controller` and `handlers` options
kettanaito Jan 14, 2026
fd3776e
chore(setupWorker): reorder imports
kettanaito Jan 14, 2026
99cc047
fix(setupWorker): forward start options to service worker source
kettanaito Jan 14, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: 20
node-version: 22
cache: 'pnpm'

- name: Install dependencies
Expand Down Expand Up @@ -93,7 +93,7 @@ jobs:
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: 20
node-version: 22
cache: 'pnpm'

- name: Restore build cache
Expand Down Expand Up @@ -129,7 +129,7 @@ jobs:
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: 20
node-version: 22
cache: 'pnpm'

- name: Restore build cache
Expand Down Expand Up @@ -195,7 +195,7 @@ jobs:
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: 20
node-version: 22
cache: 'pnpm'

- name: Install dependencies
Expand All @@ -220,7 +220,7 @@ jobs:
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: 20
node-version: 22
cache: 'pnpm'

- name: Restore build cache
Expand Down Expand Up @@ -263,7 +263,7 @@ jobs:
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: 20
node-version: 22
cache: 'pnpm'

- name: Restore build cache
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/compat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: 20
node-version: 22

- name: Set up pnpm
uses: pnpm/action-setup@v4
Expand Down Expand Up @@ -62,7 +62,7 @@ jobs:
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 20
node-version: 22

- uses: pnpm/action-setup@v4
with:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release-preview.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: 20
node-version: 22

- name: Set up pnpm
uses: pnpm/action-setup@v4
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: 20
node-version: 22
always-auth: true
registry-url: https://registry.npmjs.org

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/smoke-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: 20
node-version: 22

- name: Set up pnpm
uses: pnpm/action-setup@v4
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/typescript-nightly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: 20
node-version: 22

- name: Get latest TypeScript version
id: get-versions
Expand Down Expand Up @@ -42,7 +42,7 @@ jobs:
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: 20
node-version: 22

- name: Set up pnpm
uses: pnpm/action-setup@v4
Expand Down
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v20
v22
4 changes: 2 additions & 2 deletions config/replaceCoreImports.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const CORE_ESM_IMPORT_PATTERN = /from ["'](~\/core(.*))["'](;)?/gm
const CORE_CJS_IMPORT_PATTERN = /require\(["'](~\/core(.*))["']\)(;)?/gm
const CORE_ESM_IMPORT_PATTERN = /from ["'](#core(.*))["'](;)?/gm
const CORE_CJS_IMPORT_PATTERN = /require\(["'](#core(.*))["']\)(;)?/gm

function getCoreImportPattern(isEsm) {
return isEsm ? CORE_ESM_IMPORT_PATTERN : CORE_CJS_IMPORT_PATTERN
Expand Down
16 changes: 8 additions & 8 deletions config/scripts/patch-ts.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ async function patchTypeDefs() {

if (typeDefsWithCoreImports.length === 0) {
console.log(
'Found no .d.ts modules containing the "~/core" import, skipping...',
'Found no .d.ts modules containing the "#core" import, skipping...',
)
return process.exit(0)
}

console.log(
'Found %d module(s) with the "~/core" import, resolving...',
'Found %d module(s) with the "#core" import, resolving...',
typeDefsWithCoreImports.length,
)

Expand All @@ -56,9 +56,9 @@ async function patchTypeDefs() {
typeDefsWithCoreImports.length,
)

// Next, validate that we left no "~/core" imports unresolved.
// Next, validate that we left no "#core" imports unresolved.
const result = await execAsync(
`grep "~/core" ./**/*.d.{ts,mts} -R -l || exit 0`,
`grep "#core" ./**/*.d.{ts,mts} -R -l || exit 0`,
{
cwd: BUILD_DIR,
shell: '/bin/bash',
Expand All @@ -67,7 +67,7 @@ async function patchTypeDefs() {

invariant(
result.stderr === '',
'Failed to validate the .d.ts modules for the presence of the "~/core" import. See the original error below.',
'Failed to validate the .d.ts modules for the presence of the "#core" import. See the original error below.',
result.stderr,
)

Expand All @@ -77,7 +77,7 @@ async function patchTypeDefs() {
.filter(Boolean)

console.error(
`Found .d.ts modules containing unresolved "~/core" import after the patching:
`Found .d.ts modules containing unresolved "#core" import after the patching:

${modulesWithUnresolvedImports.map((path) => ` - ${new URL(path, BUILD_DIR).pathname}`).join('\n')}
`,
Expand All @@ -86,7 +86,7 @@ ${modulesWithUnresolvedImports.map((path) => ` - ${new URL(path, BUILD_DIR).pat
return process.exit(1)
}

// Ensure that the .d.ts files compile without errors after resolving the "~/core" imports.
// Ensure that the .d.ts files compile without errors after resolving the "#core" imports.
console.log('Compiling the .d.ts modules with tsc...')
const tscCompilation = await execAsync(
`tsc --noEmit --skipLibCheck ${typeDefsPaths.join(' ')}`,
Expand Down Expand Up @@ -135,7 +135,7 @@ ${modulesWithUnresolvedImports.map((path) => ` - ${new URL(path, BUILD_DIR).pat
}

console.log(
'The "~/core" imports resolved successfully in %d .d.ts modules! 🎉',
'The "#core" imports resolved successfully in %d .d.ts modules! 🎉',
typeDefsWithCoreImports.length,
)
}
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,12 @@
"./mockServiceWorker.js": "./lib/mockServiceWorker.js",
"./package.json": "./package.json"
},
"imports": {
"#core": "./src/core"
},
"bin": {
"msw": "cli/index.js"
},
"engines": {
"node": ">=18"
},
"scripts": {
"start": "tsup --watch",
"clean": "rimraf ./lib",
Expand Down Expand Up @@ -248,7 +248,7 @@
"outvariant": "^1.4.3",
"path-to-regexp": "^6.3.0",
"picocolors": "^1.1.1",
"rettime": "^0.7.0",
"rettime": "^0.9.1",
"statuses": "^2.0.2",
"strict-event-emitter": "^0.5.1",
"tough-cookie": "^6.0.0",
Expand Down
10 changes: 5 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 14 additions & 33 deletions src/browser/setupWorker/glossary.ts → src/browser/glossary.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,13 @@
import { Emitter } from 'strict-event-emitter'
import type { HttpRequestEventMap, Interceptor } from '@mswjs/interceptors'
import type { DeferredPromise } from '@open-draft/deferred-promise'
import {
LifeCycleEventEmitter,
LifeCycleEventsMap,
SharedOptions,
} from '~/core/sharedOptions'
import { RequestHandler } from '~/core/handlers/RequestHandler'
import type { RequiredDeep } from '~/core/typeUtils'
import type { WebSocketHandler } from '~/core/handlers/WebSocketHandler'
import type { WorkerChannel } from '../utils/workerChannel'
import type { LifeCycleEventEmitter, SharedOptions } from '#core/sharedOptions'
import type { RequiredDeep } from '#core/typeUtils'
import type { HttpNetworkFrameEventMap } from '#core/new/frames/http-frame'
import type { WebSocketNetworkFrameEventMap } from '#core/new/frames/websocket-frame'
import { AnyHandler } from '#core/new/handlers-controller'

export interface StringifiedResponse extends ResponseInit {
body: string | ArrayBuffer | ReadableStream<Uint8Array> | null
}

export type SetupWorkerInternalContext = {
isMockingEnabled: boolean
workerStoppedAt?: number
startOptions: RequiredDeep<StartOptions>
workerPromise: DeferredPromise<ServiceWorker>
registration: ServiceWorkerRegistration | undefined
getRequestHandlers: () => Array<RequestHandler | WebSocketHandler>
emitter: Emitter<LifeCycleEventsMap>
keepAliveInterval?: number
workerChannel: WorkerChannel
fallbackInterceptor?: Interceptor<HttpRequestEventMap>
}

export type ServiceWorkerInstanceTuple = [
ServiceWorker | null,
ServiceWorkerRegistration,
Expand Down Expand Up @@ -62,6 +42,7 @@ export interface StartOptions extends SharedOptions {
* Defers any network requests until the Service Worker
* instance is activated.
* @default true
* @deprecated
*/
waitUntilReady?: boolean

Expand Down Expand Up @@ -98,11 +79,11 @@ export interface SetupWorker {

/**
* Prepends given request handlers to the list of existing handlers.
* @param {RequestHandler[]} handlers List of runtime request handlers.
* @param {Array<AnyHandler>} handlers List of runtime request handlers.
*
* @see {@link https://mswjs.io/docs/api/setup-worker/use `worker.use()` API reference}
*/
use: (...handlers: Array<RequestHandler | WebSocketHandler>) => void
use: (...handlers: Array<AnyHandler>) => void

/**
* Marks all request handlers that respond using `res.once()` as unused.
Expand All @@ -113,26 +94,26 @@ export interface SetupWorker {

/**
* Resets request handlers to the initial list given to the `setupWorker` call, or to the explicit next request handlers list, if given.
* @param {RequestHandler[]} nextHandlers List of the new initial request handlers.
* @param {Array<AnyHandler>} nextHandlers List of the new initial request handlers.
*
* @see {@link https://mswjs.io/docs/api/setup-worker/reset-handlers `worker.resetHandlers()` API reference}
*/
resetHandlers: (
...nextHandlers: Array<RequestHandler | WebSocketHandler>
) => void
resetHandlers: (...nextHandlers: Array<AnyHandler>) => void

/**
* Returns a readonly list of currently active request handlers.
*
* @see {@link https://mswjs.io/docs/api/setup-worker/list-handlers `worker.listHandlers()` API reference}
*/
listHandlers(): ReadonlyArray<RequestHandler | WebSocketHandler>
listHandlers: () => ReadonlyArray<AnyHandler>

/**
* Life-cycle events.
* Life-cycle events allow you to subscribe to the internal library events occurring during the request/response handling.
*
* @see {@link https://mswjs.io/docs/api/life-cycle-events Life-cycle Events API reference}
*/
events: LifeCycleEventEmitter<LifeCycleEventsMap>
events: LifeCycleEventEmitter<
HttpNetworkFrameEventMap | WebSocketNetworkFrameEventMap
>
}
5 changes: 2 additions & 3 deletions src/browser/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export { setupWorker } from './setupWorker/setupWorker'
export type { SetupWorker, StartOptions } from './setupWorker/glossary'
export { SetupWorkerApi } from './setupWorker/setupWorker'
export { setupWorker } from './setup-worker'
export type { SetupWorker, StartOptions } from './glossary'
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
/**
* @vitest-environment node
*/
import { setupWorker } from './setupWorker'
// @vitest-environment node
import { setupWorker } from './setup-worker'

test('returns an error when run in a Node.js environment', () => {
expect(setupWorker).toThrow(
Expand Down
Loading
Loading