Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(SetupServerApi): allow using custom interceptors #2464

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

tastypackets
Copy link

  • Make interceptors an optional 2nd parameter on SetupServerApi, if no interceptors are passed the default 3 node.js interceptors are used.
  • Update contributing file to call out Playwright chromium install when running browser tests.

resolves #2460

Make sure Playwright chromium has been installed before running browser tests.

```sh
pnpm playwright install chromium
Copy link
Member

Choose a reason for hiding this comment

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

This is kind of implied by using playwright (it won't run without browsers) and isn't really specific to MSW. I would rather not include this step here. With it here, we need to make sure it reflects any possible future changes to playwright, its browser usage, or its CLI.

import { FetchInterceptor } from '@mswjs/interceptors/fetch'
import { XMLHttpRequestInterceptor } from '@mswjs/interceptors/XMLHttpRequest'

const fetchInterceptorSpy = vi.spyOn(FetchInterceptor.prototype, 'apply')
Copy link
Member

Choose a reason for hiding this comment

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

Let's drop these spies and instead:

  1. Declare a request handler as you normally would.
  2. Assert that the matching request receives the mocked response.

This will give us assurance that the custom interceptor indeed works.

)
constructor(
handlers: Array<RequestHandler | WebSocketHandler>,
customInterceptors: Array<{ new (): Interceptor<HttpRequestEventMap> }> = [
Copy link
Member

@kettanaito kettanaito Apr 7, 2025

Choose a reason for hiding this comment

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

Hmm, technically, we can accept interceptors of any event map, like WebSocket. I need to think on this one... I am tempted to say we should do Interceptor<any> here as the end event map is defined by the MSW usage of particular interceptors, but this may have unexpected repercussions.

Copy link
Member

Choose a reason for hiding this comment

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

But probably a better solution would be to limit the supported event maps as we only have two at the moment:

Interceptor<HttpRequestEventMap | WebSocketEventMap>

Both types are available from the @mswjs/interceptors package.

What do you think?

Copy link
Member

@kettanaito kettanaito left a comment

Choose a reason for hiding this comment

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

Thanks for working on this one, @tastypackets!

I left a few suggestions, let me know your thoughts about them. Excited to see this landing in MSW soon!

@kettanaito kettanaito changed the title feat: allow passing custom interceptors to SetupServerApi feat(SetupServerApi): allow using custom interceptors Apr 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Expose a way to pass custom interceptors
2 participants