From 5060e7b2aff3e3e182700fba9402b3880c79023a Mon Sep 17 00:00:00 2001 From: mrloop Date: Wed, 10 Jan 2024 11:27:23 +0000 Subject: [PATCH] Hold on to reference to MSW worker start promise --- lib/msw-config.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/msw-config.ts b/lib/msw-config.ts index d39b121..de6d15f 100644 --- a/lib/msw-config.ts +++ b/lib/msw-config.ts @@ -6,6 +6,7 @@ import { type SetupWorkerApi, MockedRequest, } from 'msw'; +import { StartReturnType } from 'msw/lib/types/setupWorker/glossary'; import PassthroughRegistry from './passthrough-registry'; import type { Server } from 'miragejs'; import type { HTTPVerb, RouteHandler, ServerConfig } from 'miragejs/server'; @@ -67,6 +68,10 @@ type RouteOptions = { timing?: boolean | number; }; +type SetupWorkerApiWithStartPromise = SetupWorkerApi & { + _startPromise: StartReturnType; +}; + const defaultRouteOptions = { coalesce: false, timing: undefined, @@ -127,7 +132,7 @@ export default class MswConfig { timing?: number; - msw?: SetupWorkerApi; + msw?: SetupWorkerApiWithStartPromise; mirageServer?: MirageServer; @@ -447,10 +452,10 @@ export default class MswConfig { } start() { - this.msw = setupWorker(...this.handlers); + this.msw = setupWorker(...this.handlers) as SetupWorkerApiWithStartPromise; let logging = this.mirageConfig?.logging || false; - this.msw.start({ + this.msw._startPromise = this.msw.start({ quiet: !logging, onUnhandledRequest: (req) => { const verb = req.method.toUpperCase();