Display request log once the response is signaled by the worker
See original GitHub issueScope
Improves an existing behavior
Compatibility
- This is a breaking change
Feature description
Current behavior
Right now MSW prints the [MSW] /resource 200 OK
request log immediately once the mocked response has been sent to the worker.
Proposed behavior
Instead, print the request log once the worker reports the response event. This way if the response has some delay, or took some time to process for other reasons, the developer would know that, translating the moment when the request log has appeared to the moment when the response has been truly handled by the worker.
Links
This is the wrong place to call handler.log()
. We should migrate from this and reconsider if onMockedResponseSent
is used for any other purpose.
We may also consider rewriting onMockedResponseSent
to actually await the response event from the worker and then dispatching its callback. For example, handleRequest()
accepts context.emitter
as an input, so it can create a listener on the response events:
// handleRequest.ts
// We have two distinct events for different response resolution type,
// so listen to both. One request will always have only one of the response
// events emitter by the worker.
context.emitter.once('response:mocked', handleResponse)
context.emitter.once('response:bypassed', handleResponse)
const handleResponse = (response, requestId) => {
// Ignore response events from irrelevant requests.
if (request.id !== requestId) {
return
}
onMockedResponseSent?.(info)
}
Issue Analytics
- State:
- Created a year ago
- Comments:14 (12 by maintainers)
Top GitHub Comments
I can confim @kleinfreund !
I still get “MaxListenersExceededWarning” (
Chrome Browser
) although as you (@kettanaito) recommended (https://github.com/mswjs/msw/pull/1407) to fully re-install (delete msw -> add mws@0.47.3, also triedrm -rf node_modules
).Some side Note: I switched from 0.43.0 to 0.47.3 and now new additional strange behavior appeared. The logs in the devTools are displayed (logged) multiple/many/>=10x times in the browser. In 0.43.0 everything works fine.
DevTools Log
@kettanaito Not a problem. I’ve filed an issue for this here: https://github.com/mswjs/msw/issues/1411