Skip to content

Find a library to help us stub APIs

This library request applies to solstice-assets and membership.eclipse.org's front-end.

We need to mock some of our own APIs to test our functions located under the /js/api/ directory.

I found two APIs: MirageJS, and MSW.

MirageJS

I have been looking into MirageJS. It intercepts fetch requests and stubs the response. This is not only useful for unit tests, but I could also see it benefiting membership.eclipse.org with stubbing our APIs for dev mode.

Currently for membership.eclipse.org, we have a bunch of if statements everywhere we do a fetch (ex. if dev mode then fetch json file here, otherwise fetch from live api). Here is an example of this in our React code. I think we can remove a lot of complexity if we simply fetch our APIs directly and let MirageJS handle the stubbing if required.

How it works

For unit testing:

You create a MirageJS server and define routes and responses. You can initialize a server per unit test and destroy the instance afterwards. Every time your code fetches from a route defined within MirageJS, it will send you the stubbed response.

For dev mode:

Initialize once somewhere. It will stub any request. Any unhandled request can hit a live API using a passthrough.

Information about MirageJS

Filling in this information based on the wiki guide.

If for whatever reason we need to exchange this library for something else in the future, it shouldn't be a problem. This could easily be swapped to something else.

MSW (mock service worker)

This library seems to be more popular. https://mswjs.io/docs/

It does the same thing as MirageJS. Has an MIT license, 1.6M weekly downloads.

Edited by Olivier Goulet