Hone logo
Hone
Problems

Simulating Network Errors in Jest Tests (TypeScript)

Testing asynchronous code, particularly code that interacts with network requests, can be tricky. This challenge focuses on creating a reusable mock function that simulates network errors within your Jest tests, allowing you to verify how your application gracefully handles such failures. This is crucial for robust and reliable applications.

Problem Description

You need to implement a function called createNetworkErrorMock that returns a mock function. This mock function, when called, should simulate a network error by rejecting with a provided error message. The createNetworkErrorMock function should accept a single argument: errorMessage (a string). The returned mock function should, when invoked, reject with an error containing the provided errorMessage.

The purpose of this mock is to isolate your component or function under test from actual network calls, allowing you to focus solely on its error handling logic. You'll use this mock in your Jest tests to ensure your application behaves as expected when a network request fails.

Key Requirements:

  • The createNetworkErrorMock function must accept a string errorMessage as input.
  • The returned mock function must reject with an error object whose message property matches the provided errorMessage.
  • The mock function should not perform any actual network requests. It should only simulate the error.
  • The mock function should be reusable across multiple tests.

Expected Behavior:

When the mock function is called within a test, it should immediately reject with an error containing the specified error message. This allows you to assert that your component or function correctly handles the error condition.

Edge Cases to Consider:

  • Empty or null errorMessage: The mock should still reject with an error, even if the message is empty or null. Consider providing a default error message in this case.
  • Non-string errorMessage: While the problem specifies a string, consider how your mock should behave if a non-string value is passed. Throwing an error or coercing to a string are possible approaches. For this challenge, coercing to a string is acceptable.

Examples

Example 1:

Input: createNetworkErrorMock("Network request failed")
Output: A mock function.
Explanation: Calling the mock function will reject with an error containing the message "Network request failed".

Example 2:

const mockError = createNetworkErrorMock("Timeout Error");

return new Promise((resolve, reject) => {
  mockError().then(resolve).catch(error => {
    expect(error.message).toBe("Timeout Error");
  });
});

Input: The code above, where mockError is created with createNetworkErrorMock("Timeout Error") Output: The test passes, asserting that the error message is "Timeout Error". Explanation: The mock function rejects with the specified error message, allowing the test to verify the error handling.

Example 3:

const mockError = createNetworkErrorMock("");

return new Promise((resolve, reject) => {
  mockError().then(resolve).catch(error => {
    expect(error.message).toBe("");
  });
});

Input: The code above, where mockError is created with createNetworkErrorMock("") Output: The test passes, asserting that the error message is "". Explanation: The mock function rejects with an empty string error message, allowing the test to verify the error handling.

Constraints

  • The createNetworkErrorMock function must be implemented in TypeScript.
  • The mock function should be lightweight and efficient, avoiding unnecessary operations.
  • The solution should be easily testable and maintainable.
  • The errorMessage parameter can be any string, including empty strings.

Notes

Consider using a simple function closure to create the mock function within createNetworkErrorMock. Think about how to ensure the mock function consistently rejects with the correct error message, regardless of how it's called within the test. The goal is to create a reliable and reusable tool for simulating network errors in your Jest tests.

Loading editor...
typescript