Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
58 changes: 29 additions & 29 deletions pnpm-lock.yaml

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

15 changes: 12 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ import {
WebSocketServerConnectionProtocol,
} from '@mswjs/interceptors/WebSocket'

/** @todo `Import from msw/core`? */
type UnhandledRequestStrategy = (request: Request) => void;
export interface CreateNetworkFixtureArgs {
initialHandlers: Array<RequestHandler | WebSocketHandler>
onUnhandledRequest?: UnhandledRequestStrategy
}

/**
Expand All @@ -48,7 +51,8 @@ export interface CreateNetworkFixtureArgs {
*/
export function createNetworkFixture(
args?: CreateNetworkFixtureArgs,
/** @todo `onUnhandledRequest`? */
onUnhandledRequest?: UnhandledRequestStrategy,
/** @todo `startOptions`? */
): [
TestFixture<NetworkFixture, PlaywrightTestArgs & PlaywrightWorkerArgs>,
{ auto: boolean },
Expand All @@ -60,7 +64,7 @@ export function createNetworkFixture(
initialHandlers: args?.initialHandlers || [],
})

await worker.start()
await worker.start({onUnhandledRequest: onUnhandledRequest})
await use(worker)
await worker.stop()
},
Expand All @@ -79,7 +83,7 @@ export class NetworkFixture extends SetupApi<LifeCycleEventsMap> {
this.#page = args.page
}

public async start() {
public async start({ onUnhandledRequest }: { onUnhandledRequest?: UnhandledRequestStrategy } = {}) {
// Handle HTTP requests.
await this.#page.route(/.+/, async (route, request) => {
const fetchRequest = new Request(request.url(), {
Expand Down Expand Up @@ -112,6 +116,11 @@ export class NetworkFixture extends SetupApi<LifeCycleEventsMap> {
: undefined,
})
return
} else {
if (onUnhandledRequest) {
onUnhandledRequest(fetchRequest)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't believe we need to invoke this manually. If you provide it to worker.start(), MSW will apply it for you. All you need here is a type-safe onUnhandledRequest option in the args.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kettanaito I tried removing this but was having issues getting the test to fire the onUnhandledRequest.

Correct me if I'm wrong but it doesn't seem like msw/playwright leverages handleRequest so we never get to the lookup logic in msw/core that executes the onUnhandledRequest

It seems we use the getResponse but to parse and return handler values but rely more on the native playwright page.route to trigger and return requests. It would be nice to let msw invoke onUnhandledRequest so please let me know if I'm missing something

For now I've explicitly invoked it and added a test so we can see how we'd like to proceed.

return
}
}

route.continue()
Expand Down