Hone logo
Hone
Problems

Testing Response Resolvers with Jest

Response resolvers are a common pattern in asynchronous operations, particularly within frameworks like GraphQL or when dealing with API calls. They take data and transform it into a final response, often handling error conditions and formatting. This challenge focuses on creating and testing a response resolver function using Jest, ensuring it behaves correctly under various scenarios, including successful data retrieval and error handling.

Problem Description

You are tasked with creating a response resolver function and writing Jest tests to verify its behavior. The resolver function, resolveResponse, takes two arguments: data (which can be any type) and error (which can be either an error object or null).

The resolver should behave as follows:

  • Successful Data: If error is null, the resolver should return a formatted response object with the structure { success: true, data: data }.
  • Error Handling: If error is not null, the resolver should return a formatted response object with the structure { success: false, error: error.message }. The error.message property is crucial; we only want the error message, not the entire error object.
  • Type Safety: The resolver function should be written in TypeScript and properly typed to ensure type safety.

You need to write Jest tests to cover the following cases:

  1. Successful data resolution (where data is a string and error is null).
  2. Error resolution (where data is undefined and error is an Error object).
  3. Error resolution with a custom error message (where data is undefined and error is an object with a message property).

Examples

Example 1:

Input: data = "Hello, world!", error = null
Output: { success: true, data: "Hello, world!" }
Explanation:  The error is null, so we return a success response with the provided data.

Example 2:

Input: data = undefined, error = new Error("Failed to fetch data")
Output: { success: false, error: "Failed to fetch data" }
Explanation: An error occurred, so we return a failure response with the error message.

Example 3:

Input: data = undefined, error = { message: "Invalid input provided" }
Output: { success: false, error: "Invalid input provided" }
Explanation: An error occurred with a custom message, so we return a failure response with that message.

Constraints

  • The resolveResponse function must be written in TypeScript.
  • The response object must always have the structure { success: boolean, data?: any, error?: string }. Note that data and error are optional.
  • The tests should use Jest and should be clear and concise.
  • The error object passed to the resolver can be any object with a message property, or a standard Error object.

Notes

Consider using Jest's expect function with appropriate matchers (e.g., toEqual, toBe) to verify the structure and content of the response objects. Think about how to handle different types of error objects gracefully. The key is to isolate the error message from the full error object when constructing the failure response. Focus on writing robust tests that cover all specified scenarios.

Loading editor...
typescript