Search⌘ K
AI Features

Client Mock Tips

Explore effective strategies for mocking external dependencies and browser-specific features in Jest. Understand how to simulate API calls, browser routing, events, and SDKs, enabling you to create isolated, controlled test environments for client-side JavaScript applications. This lesson helps you ensure reliable and comprehensive testing without a real browser or network connections.

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 ...