Give more guidelines if mocking fails
See original GitHub issueIs 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:
- Created 3 years ago
- Reactions:8
- Comments:5 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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 ofmutation
). The suggestion bymsw
was that an unmocked request should be mocked usingmsw.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.Thanks for volunteering, @tigerabrodi. I think I’ll take this one and try to add the following DX improvements: