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.

Give more guidelines if mocking fails

See original GitHub issue

Is your feature request related to a problem? Please describe.

When a request is not mocked in msw the error of not mocking the request is sometimes hard to understand. For example if in a GraphQL setup and mock is created for a query, but the tested unit actually does a mutation, msw will fail. The error I’ve seen here was Network error: request to http://localhost:8080/api failed, reason: connect ECONNREFUSED 127.0.0.1:8080.

Describe the solution you’d like

What would be helpful in this case is that msw will look at the existing (setup) mocks and suggest if there are cases which could match. Like: You mocked a query with name 'some-query' and there is a mock for a mutation with that name. Did you want to use that?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:8
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
KnisterPetercommented, Sep 16, 2020

Hi @kettanaito, we already have worker.start({ onUnhandledRequest: 'error' }) enabled. This does not help that well. We debugged quite some time to detect that we used the wrong mock method (query instead of mutation). The suggestion by msw was that an unmocked request should be mocked using msw.rest... (if I remember correctly). The only helpful part here was, that we have had an unmocked request.

It would be at least helpful (in case of onUnhandledRequest = ‘error’) to maybe output the headers and the body. I know this already from nock which is at least a bit more helpful.

1reaction
kettanaitocommented, Jan 22, 2021

Thanks for volunteering, @tigerabrodi. I think I’ll take this one and try to add the following DX improvements:

  • Suggest similar request handlers when encountered an unhandled request. Account for typos.
  • Suggest handlers based on the request type (REST/GraphQL).
  • Change the suggested “dummy” handler implementation based on the request type (now we always recommend creating a REST API request handler).
Read more comments on GitHub >

github_iconTop Results From Across the Web

Simulate first call fails, second call succeeds - Stack Overflow
From the docs: Sometimes we need to stub with different return value/exception for the same method call. Typical use case could be mocking...
Read more >
To mock or not to mock: is that even a question? - Accenture
Accenture's Steve Bement reveals mocking is a popular approach for handling dependencies while unit testing, but it comes at a cost.
Read more >
Mocking | Guide - Vitest
Mocking #. When writing tests it's only a matter of time before you need to create a "fake" version of an internal —...
Read more >
A Unit Testing Practitioner's Guide to Everyday Mockito - Toptal
Calling real methods may be useful on partial mocks, but make sure that the called method has no unwanted side effects and doesn't...
Read more >
Golang Testing: Mocking and Error Handling | by William Infante
There are multiple ways of implementing our mock functions. For this simple case, we just used an if statement like if len(args)>1 to...
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