Every web project I've contributed to had the same issue: How do we test our client independently of the API? Obviously, we would need to mock the HTTP requests, however, this usually just leads to another problem namely how to maintain these mocks. Furthermore, these mocks are now not only sensitive to the API changes, but also to our code changes. Does not sound very stable... and has proven to be unstable every time I've encountered it.
As a cure I've always wrote some tooling that essentially represents a mock server. While a simple solution may usually do it, I've refined my approach over the years and ended up with a Node.js server that has the following characteristics:
- Fully configurable (what to proxy, where to proxy, running on what port, SSL, ...)
- Transparent pipeline (multiple injectors, customize order, custom injectors possible, ...)
- Support for proxying multiple servers at once at the same port
- Out-of-the-box support for HTTP(S) and WS(S)
- Management endpoint with an integrated web client
Two weeks ago I took the time to rewrite it in TypeScript and open-source the solution. The project is called kras [sic], which means "kein reiner API server". It can run standalone (command:
kras) or embedded within a project. It can be used as a library or as a command line utility.
Since then I am taking care of little bugs, adding some interesting new features, or increasing the test coverage. For now the client is written fully in React and is simple enough to resist using any flux pattern or state container. Nevertheless, I think the code is clean and straight forward.
In the future I plan to bring more examples and documentation online. I will also try to write some article on CodeProject and get some attention for the project. Let's see if more folks are interested in that solution.
In our current project we already make use of kras. It greatly helps already by proxying certain servers / environment without the need to adjust CORS settings for the servers. Additionally, it can be very beneficial for unit testing (in our case with Jest).