apollographql / apollo-feature-requests

🧑‍🚀 Apollo Client Feature Requests | (no 🐛 please).
Other
130 stars 7 forks source link

Add an option to MockProvider to throw when a request fails to match a mock #420

Open JonnyIngeno opened 10 months ago

JonnyIngeno commented 10 months ago

MockProvider currently offers an option showWarnings with the following description:

When a request fails to match a mock, a warning is logged to the console to indicate the mismatch. Set this to false to silence these warnings.

The default value is true.

The problem is that it's insufficient for robust testing because showing a warning doesn't fail the tests.

For reliable integration tests, no network request should actually hit the network. A warning is easily missed in the tests output logs and unfortunately we're shipping that code because the tests passed while we didn't want them to. This often leaves a plethora of garbage in the tests output logs that we need to fix after the fact...

What we need is an option to fail the tests on such occasion:

throwExceptionOnRequestMatchMiss

When a request fails to match a mock, throws an exception. This will allow your test framework to fail a test and fix the problem before it's pushed to production. Set to true to activate this functionality.

The default value is false.

I know such feature exists in MSW (Mocked Server Worker) and other solutions for mocking requests at the network layer. It would be a nice addition to the MockedProvider.