Mocking API requests in React tests

Recently I came across tests that require mocking server responses.
In general I am talking about tests that expect an API call during some action, and want to control (and later assert) the response that comes from the server.

From a quick look in the internet, I can see two directions to add such a capability to tests:

  1. Use a ready-made npm package, for example GitHub - nettofarah/axios-vcr: 📼 Record and Replay requests in JavaScript.
  2. Write our own helper that will do something along the lines of helper.expectRequest('put', '/foo/1', apiResponse).

What are your opinions about it?

I’m torn. Maintaining explicit mocks/stubs for an API is a pain, but storing tons of huge VCRs is also noisy. My preference is still to use integration tests where possible.

In this particular scenario there is a tight client and server integration so I’d suggest system tests (which in our code are often still called integration tests) to really test the flow end-to-end.

1 Like

this project is unmaintained since ~ 6 years.

Katello uses GitHub - nock/nock: HTTP server mocking and expectations library for Node.js in a lot of our React tests and it’s worked pretty well.

I’m wondering if we can do something that will automatically update the api snapshot files, for example before every tests run (all the tests, not before each single test). So we can combine the big foreman set up (like in capybara), but only do it once before the tests

Being able to (re)record API snapshots is something I certainly expect from any VCR-like library. It looks like nock (as used by Katello) can do this (GitHub - nock/nock: HTTP server mocking and expectations library for Node.js) so I could see some scripting (rake task?) that sets up the Foreman test server in a certain state for the fixtures to be recorded. I’d hope the building up the state heavily relies on the factories in Foreman.

We can then decide if we want to store these in git (which to implies some CI that also verifies the fixtures are up to date) or depend on the invoker (either CI or a developer) to run the recording task.

I’ll also note that apipie-rails can also [record examples](GitHub - Apipie/apipie-rails: Ruby on Rails API documentation tool while running tests). If those could somehow be reused in Javascript testing then that may be a very interesting way to reuse a lot of existing infrastructure.

As a closing note I’d like to emphasize that I do see value in testing at multiple levels of integration. It can be faster to have a limited scope, but you can also miss the big picture bugs that only show up in combinations.