Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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.

13 changes: 10 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
RequestHandler,
WebSocketHandler,
getResponse,
type UnhandledRequestStrategy,
} from 'msw'
import {
type WebSocketClientEventMap,
Expand All @@ -25,6 +26,7 @@ import {

export interface CreateNetworkFixtureArgs {
initialHandlers: Array<RequestHandler | WebSocketHandler>
onUnhandledRequest?: UnhandledRequestStrategy
}

/**
Expand All @@ -48,7 +50,6 @@ export interface CreateNetworkFixtureArgs {
*/
export function createNetworkFixture(
args?: CreateNetworkFixtureArgs,
/** @todo `onUnhandledRequest`? */
): [
TestFixture<NetworkFixture, PlaywrightTestArgs & PlaywrightWorkerArgs>,
{ auto: boolean },
Expand All @@ -58,9 +59,10 @@ export function createNetworkFixture(
const worker = new NetworkFixture({
page,
initialHandlers: args?.initialHandlers || [],
onUnhandledRequest: args?.onUnhandledRequest || 'warn',
})

await worker.start()
await worker.start({onUnhandledRequest: args?.onUnhandledRequest})
await use(worker)
await worker.stop()
},
Expand All @@ -74,12 +76,13 @@ export class NetworkFixture extends SetupApi<LifeCycleEventsMap> {
constructor(args: {
page: Page
initialHandlers: Array<RequestHandler | WebSocketHandler>
onUnhandledRequest?: UnhandledRequestStrategy
}) {
super(...args.initialHandlers)
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 +115,10 @@ export class NetworkFixture extends SetupApi<LifeCycleEventsMap> {
: undefined,
})
return
} else {
if (typeof onUnhandledRequest === 'function') {
Copy link
Author

Choose a reason for hiding this comment

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

This is quite less than ideal, waiting on closure in this thread from @kettanaito for guidance on how to proceed.

await onUnhandledRequest(fetchRequest, { warning: console.warn, error: console.error })
}
}

route.continue()
Expand Down
20 changes: 20 additions & 0 deletions tests/auto.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@ interface Fixtures {
network: NetworkFixture
}

const unhandledRoutes: string[] = [];

const test = testBase.extend<Fixtures>({
network: createNetworkFixture({
initialHandlers: [
http.get('/resource', () => {
return new Response('hello world')
}),
],
onUnhandledRequest: (request) => {
// store unhandled routes so we can assert on them
unhandledRoutes.push(new URL(request.url).pathname);
},
}),
})

Expand All @@ -27,3 +33,17 @@ test('automatically applies the network fixture', async ({ page }) => {

expect(data).toBe('hello world')
})

test("invokes the onUnhandledRequest handler", async ({
page,
}) => {
await page.goto('/')

const data = await page.evaluate(() => {
return fetch('/unhandled-resource').then((response) => {
return; // no-op
})
})

expect(unhandledRoutes).toContain("/unhandled-resource");
});