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.
- Uses the MIT license, source: https://github.com/miragejs/miragejs/blob/master/LICENSE.md
- Fairly active user-base with 224,715 weekly downloads as of 2022-12-20
- Support seems decent. A couple releases per year, source: https://www.npmjs.com/package/miragejs?activeTab=versions
- Also a bit of PR activity: https://github.com/miragejs/miragejs/pulls?q=is%3Apr+is%3Aclosed
- Bundle size probably doesn't matter. It should be a dev dependency.
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.