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

Use mswjs/interceptors as interceptors layer for Nock #425

Closed
mikicho opened this issue Sep 17, 2023 · 3 comments
Closed

Use mswjs/interceptors as interceptors layer for Nock #425

mikicho opened this issue Sep 17, 2023 · 3 comments

Comments

@mikicho
Copy link
Contributor

mikicho commented Sep 17, 2023

Hey,
As part of our efforts to add fetch support to Nock, we thought we could utilize this package to intercept the requests and add Nock's great API on top of it.
I'm curious how we can simulate some exotic cases as we can do in Nock, like delay the connection.

More context: nock/nock#2397

@kettanaito
Copy link
Member

Hi, @mikicho. I appreciate you reaching out.

Interceptors currently supports all means to make a request in Node.js, which includes the global Fetch API. If your testing uncovers some unexpected behaviors, it'd be a great opportunity for us to improve.

I'm curious how we can simulate some exotic cases as we can do in Nock, like delay the connection.

Sure. You can simulate a delay by awaiting a timeout promise in any request listener on the interceptor. Here's an example:

import nodePreset from '@mswjs/interceptors/presets/node'

const interceptor = new BatchInterceptor({
  name: 'my-interceptor',
  interceptors: nodePreset
})

interceptor.on('request', async ({ request }) => {
  await new Promise(resolve => setTimeout(resolve, 1000))
  request.respondWith(new Response('Hello world')
})

This example will wait for 1000ms before responding to any intercepted request with a "Hello world" mocked response. Interceptors is not opinionated in how you decide to inject such a delay. For example, with MSW 2.0, we are bringing a new delay() function that, effectively, does the above but wrapped in a nicer developer-facing API:

await delay(1000)

Your usual Promise-based sleep and such would work here too.

We can go through all the other use cases together if you could prepare a list of things Nock needs to implement.

@mikicho mikicho changed the title Use mswjs/interceptors as interceptors later for Nock Use mswjs/interceptors as interceptors layer for Nock Sep 17, 2023
@kettanaito kettanaito added this to the Nock compatibility milestone Mar 26, 2024
@kettanaito
Copy link
Member

The socket-based interceptor has been merged to main! 🎉 The remaining Nock-compatibility tasks are tracked in #575. Looking forward to getting the rest done!

@kettanaito
Copy link
Member

I will close this in favor of #575.

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

No branches or pull requests

2 participants