How to "avoid request assertions" ? #2361
-
Hi there, My team and I have been using MSW for a few months and we think it's great. How to avoid request assertions, as the doc's best practices suggest, and test what the application does since every HTTP request is mocked ? 1/ Testing that a list of products is correctly displayed: If I want to test that my list of products display every product title and price, it is easy since it's a simple GET request with no path param and no body : 2/ Testing that a product gets updated: This is where I think it gets trickier. We have a boolean flag that we set to true if the update of the product is called with the proper params and body. This is testing implementation details, which is not great, but at least it verifies that the back-end is called with the right information.
Here, we check that the UPDATE is called with the expected information AND we mock differently the new call to GET products. We assert that the title of the product was updated. Of course it was updated since I explicitly indicated that the second GET products should return the updated product ! What's the best way to handle this ? Thanks in advance, |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Hi, @francoisbillet. Thanks for raising this, I will use this thread for reference since I heard about a similar confusion in the past.
The best practice in the docs is meant to be a safe default. Most teams and most tests must focus on testing the UI, asserting what happens in the realm observable by the user. There are exceptions to this, and the page mentions those too! Not all requests will find reflection in the UI. Some won't. For those, it's okay to assert on the request itself.
Drop this state and instead return a mocked Let's talk about mocking mutations in general. One thing a lot of folks miss out on is that an API mock is your boundary. For example, on a typical GET+POST+GET test scenario, you don't have to create an initial set of data, then update it, then make sure the updated version is displayed. That's fine to do in an E2E test, but we are talking about integration tests here. For integration tests, you check two things:
The update flow is tangentially covered within these two test cases. Then, you do a proper user flow that includes mock data and actual updates in an E2E test to complete the picture. Does this make sense? |
Beta Was this translation helpful? Give feedback.
Hi, @francoisbillet. Thanks for raising this, I will use this thread for reference since I heard about a similar confusion in the past.
The best practice in the docs is meant to be a safe default. Most teams and most tests must focus on testing the UI, asserting what happens in the realm observable by the user. There are exceptions to this, and the page mentions those too! Not all requests will find reflection in the UI. Some won't. For those, it's okay to assert on the request itself.
Drop this state and instead return a mocked
400
respons…