question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Display request log once the response is signaled by the worker

See original GitHub issue

Scope

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

https://github.com/mswjs/msw/blob/6c6e1193327ba6c7d55826fd7997ddc9f1fb4fd5/src/setupWorker/start/createRequestListener.ts#L66-L80

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:closed
  • Created a year ago
  • Comments:14 (12 by maintainers)

github_iconTop GitHub Comments

2reactions
SerkanSipahicommented, Sep 17, 2022

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 tried rm -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

  • MaxListenersExceededWarning
  • multiple logs
Screenshot 2022-09-17 at 09 24 02
1reaction
kleinfreundcommented, Sep 20, 2022

@kettanaito Not a problem. I’ve filed an issue for this here: https://github.com/mswjs/msw/issues/1411

Read more comments on GitHub >

github_iconTop Results From Across the Web

Logging from Workers - Cloudflare Docs
You can use a service like Sentry Open external link to collect error logs from your Worker, by making an HTTP request to...
Read more >
Gunicorn worker terminated with signal 9 - Stack Overflow
I have a gunicorn app with a single uvicorn worker, which only handles one request at a time. It worked fine locally but...
Read more >
Capturing Request Logs From Signal Sciences
In this post, I'll provide an overview of several options available to users of Signal Sciences for capturing request logs.
Read more >
Abortable fetch - Chrome Developers
This means every Request has a signal, whether one is given to its constructor or not. # Reacting to an aborted fetch. When...
Read more >
Web Push Troubleshooting - OneSignal Documentation
A bad HTTP response code (403) was received when fetching the script. ... The OneSignal Service Worker files are being redirected but should...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found