Client Mock Tips

Learn some quick tips to handle the nuances of client-side code in the Jest test environment.

The dimensions of client mocking

There are several high-level elements to keep in mind when mocking inside of our clients. To start, clients run in the browser, and the Jest test environment does not. Since one use for mocking is stubbing out functionality that we cannot or do not want to run in our test environment, we want to keep this in mind.

Additionally, clients typically rely on external services for data hydrationThe importing of data into an object.. Whether this is through communication with a server API, a client-side SDK, or something else, this type of external communication more than likely does not work inside our tests. In the best-case scenario, the result is unreliable. Once inside the client, the data might be managed by a global state management system. This system is likely also difficult and unreliable to test.

So, we have two main focal points here: the fact that our clients are built to run in a browser and that they rely on external data in order to function properly. We are going to ...