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.

Allow asserting on requests

See original GitHub issue

Is your feature request related to a problem? Please describe. We are currently migrating cypress integration tests (network mocked) to an app-level JSDOM integration tests, and chose MSW as the network-layer mock framework.

We cannot use MSW alone to test all our scenarios.

Describe the solution you’d like Ability to assert on network calls:

  • What has been called.
  • How many times.

Describe alternatives you’ve considered A different product (keep some tests in cypress), or spy on fetch.

A VERY Good Advice

From the docs:

Adding such assertions, however, is implementation details testing and is highly discouraged.

I think this is a VERY good advice (albeit with some reservations on the term “implementation details”, below).

Asserting requests in such way is testing how your application is written, instead of what it does.

This I disagree with because part of what our app does (ie, its behaviour) is calling some endpoints (see elaboration below).

There are various other related issues, including this one where @kettanaito wrote:

I’m afraid accessing such list and performing assertions on it would be implementation details testing.

See comment on “implementation details” below.

And then:

Assert what your component renders, not the network layer.

Agree with this. An excellent advice, albeit not always possible.

But

There is no such thing as context-free wisdom.

There aren’t always a side effects

Polling. We poll every X minutes and if the data hasn’t changed nor will the view. No side-effects (other than the network call).

The network is a test boundary

So in our case - app-level integration tests - there are two main boundaries:

  • The UI (which we assert on)
  • The network (which we cannot assert on with MSW).

There are other boundaries, like window.ga, localStorage, etc. But we can disregard them here.

As a test boundary, we would like to make some assertions on it.

Example

So we had a page test in Cypress, and through the command log we saw two, near-consecutive, network requests to the same endpoint. Turns out a component was re-rendering and firing twice a data-fetching request.

So we assert in the test that the endpoint should only called once.

The component was re-rendering due to other mocked network responses. So unit-testing the component wouldn’t catch this - only an app level realistic page load.

It isn’t implementing detail

You are always testing some implementation detail. Regardless of the level of your tests. Even an e2e test in cypress involves an implementation detail - it tests against a browser (if tomorrow the app is to run as a native app, you’ll have to modify the tests).

What is an implementation detail is contextual. It depends on the point of reference.

A low-level date formatter is an implementation detail for a table component that renders dates (think of a unit-test for that table component.

The very same table component is an implementation detail for the page containing it (think of testing the page).

At any point, whatever is below the interface you interface with is implementation detail.

So branding assertions on network calls “implementation details”, specifically when they are one of test boundary (ie, test interface), is somewhat not the best way to put it.

When the network is the testing boundary, hitting that boundary is what the system is doing as far as the tests are concerned. It isn’t implementation detail - what’s inside the box (the system between the network and UI) that is implementation detail here.

Make perfect sense to me you’d like to assert on network side-effects in some cases (whilst I do totally agree that doing so may not be a good practice in many cases - UI assertions should take precedence if possible).

Summary

So while I think it is a good advice, there are still some scenarios where request assertions are needed. I hope the team will re-consider.

Issue Analytics

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

github_iconTop GitHub Comments

4reactions
Dobby89commented, May 19, 2021

You can make request/response-based assertions even today using the library’s Life-cycle events API.

This is exactly what we need. Not sure how I’ve missed that in the docs and other issues. Had I seen this before I would not have raised the ticket.

I really think this should be added as a note here: https://mswjs.io/docs/recipes/request-assertions. Happy to raise a PR.

@kettanaito I only found this issue because it wasn’t mentioned on that page. @Izhaki and I can’t be the only people.

2reactions
Izhakicommented, Apr 30, 2021

You can make request/response-based assertions even today using the library’s Life-cycle events API.

This is exactly what we need. Not sure how I’ve missed that in the docs and other issues. Had I seen this before I would not have raised the ticket.

I really think this should be added as a note here: https://mswjs.io/docs/recipes/request-assertions. Happy to raise a PR.

MSW will not ship request assertions API, as it’s simply outside of the library’s responsibilities and promises it establishes with the end developer.

This makes a lot of sense - keep API surface low.

Just to be clear, I wasn’t after an assertion mechanism (like chai). I was after the ability to assert on network requests (which the life-cycle events allow).

This is a book-worth discussion about what and how to test

I feel we are 100% align on test good practices - I gave a tech talk last week pretty much arguing everything you argue here.

My point was that there are some cases where asserting on network request is valid.

Thanks for the detailed response!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Asserting Network Calls from Cypress Tests
Let's confirm the web application sends the expected data. The test passes and asserts the request and response objects and some headers.
Read more >
Making assertions - Assertible
Assertions allow you to validate HTTP responses in your API tests. For example, a basic assertion would ensure that a request to assertible.com...
Read more >
Python's assert: Debug and Test Your Code Like a Pro
Assertions allow you to test the correctness of your code by checking if some specific conditions remain true, which can come in handy...
Read more >
SOAPUI: Apply assertions to all requests? - Stack Overflow
I'm creating a test that pings multiple services (24 or so) and validates they contain (or don't contain) the same element of a...
Read more >
Automating Response Validation With Assertions In Postman
In this section, let's get started by writing a simple assertion. For example, verifying the status code of the request being executed, ...
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