Skip to content

Commit

Permalink
Hold on to reference to MSW worker start promise
Browse files Browse the repository at this point in the history
  • Loading branch information
mrloop committed Jan 10, 2024
1 parent 36fbe40 commit 1ef73b3
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions lib/msw-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -67,6 +68,10 @@ type RouteOptions = {
timing?: boolean | number;
};

type SetupWorkerApiWithStartPromise = SetupWorkerApi & {
_startPromise: StartReturnType;
}

const defaultRouteOptions = {
coalesce: false,
timing: undefined,
Expand Down Expand Up @@ -127,7 +132,7 @@ export default class MswConfig {

timing?: number;

msw?: SetupWorkerApi;
msw?: SetupWorkerApiWithStartPromise;

mirageServer?: MirageServer;

Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 1ef73b3

Please sign in to comment.