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

should call callback before response event #459

Open
Tracked by #2517
mikicho opened this issue Sep 29, 2023 · 2 comments
Open
Tracked by #2517

should call callback before response event #459

mikicho opened this issue Sep 29, 2023 · 2 comments

Comments

@mikicho
Copy link
Contributor

mikicho commented Sep 29, 2023

Node.js behaviour:

const http = require('http');

const req = http.get('http://nowhere.com/', res => {
  console.log('response event');
})
req.on('finish', () => console.log('finish'))
req.end('', null, () => console.log('callback'))

This prints:

finish
callback
response event

Mocked behavior:

const { ClientRequestInterceptor } = require('@mswjs/interceptors/ClientRequest')
const http = require('http');

const interceptor = new ClientRequestInterceptor({
  name: 'my-interceptor',
})
interceptor.apply();
interceptor.on('request', async ({request}) => {
  request.respondWith(new Response())
});

const req = http.request('http://nowhere.com/', res => {
  console.log('response event');
})
req.on('finish', () => console.log('finish'))
req.end('', null, () => console.log('callback'))

Prints:

finish
response event
callback

I can open a PR that pass the callback to the respondWIth and put it after the finish event.

@kettanaito
Copy link
Member

Hi, @mikicho. Can you please describe the expected behavior here? Is this the callback order that you find wrong? That seems to be correct (in your example). Are we not printing some of this? I suppose the .end() callback never gets called for bypassed requests?

@mikicho
Copy link
Contributor Author

mikicho commented Dec 22, 2023

Sorry for the incomplete description. I have updated it.

@kettanaito kettanaito added this to the Nock compatibility milestone Mar 26, 2024
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