Hone logo
Hone
Problems

Testing Asynchronous Code with done Callback in Jest

Asynchronous operations are common in modern JavaScript applications. Jest provides several ways to test asynchronous code, and one of the older, but still relevant, methods is using the done callback. This challenge focuses on correctly implementing and utilizing the done callback within a Jest test to ensure asynchronous code completes before assertions are made.

Problem Description

You are tasked with writing a Jest test for a function fetchData that simulates fetching data from an external source. fetchData takes a callback function as an argument. After a simulated delay (using setTimeout), it calls this callback with some sample data. Your goal is to write a Jest test that uses the done callback to ensure the data is fetched and the assertions are made after the asynchronous operation completes. Failing to call done() will cause the test to hang indefinitely. Calling done() prematurely will cause assertions to run before the data is available.

Key Requirements:

  • The fetchData function is provided.
  • You must write a Jest test using the done callback.
  • The test should assert that the data fetched is indeed "Sample Data".
  • The test should handle the case where fetchData might throw an error (though this is not explicitly tested in the examples, consider it for robustness).

Expected Behavior:

The test should:

  1. Call fetchData with a callback function that receives the data and calls done().
  2. Assert that the received data is "Sample Data".
  3. Call done() to signal the completion of the asynchronous operation.

Edge Cases to Consider:

  • What happens if fetchData throws an error? (While not explicitly tested, a robust solution should consider this).
  • What happens if done() is not called? (The test should fail).
  • What happens if done() is called before the data is available? (The test should fail).

Examples

Example 1:

Input: fetchData(callback); where callback(data) is used in the test.
Output: Test passes.
Explanation: The test correctly calls fetchData, asserts the data is "Sample Data", and calls done() after the asynchronous operation completes.

Example 2:

Input: fetchData(callback); where done() is called before the data is received.
Output: Test fails.
Explanation: The assertion is made before the data is available, leading to an incorrect assertion and test failure.

Example 3: (Edge Case - Not explicitly tested, but good practice)

Input: fetchData(callback); where fetchData throws an error.
Output: Test fails with an error message.
Explanation: The test should handle the error thrown by fetchData gracefully, potentially using a try/catch block within the callback.

Constraints

  • The fetchData function is provided and should not be modified.
  • You must use the done callback in your Jest test.
  • The test should be written in TypeScript.
  • The test should assert that the fetched data is exactly "Sample Data".

Notes

  • The done callback is a mechanism to signal the completion of an asynchronous operation to Jest.
  • Ensure that done() is called only once and after all assertions have been made.
  • Consider using try...catch blocks within the callback to handle potential errors from fetchData.
  • Think about how to structure your test to ensure the assertions are made after the asynchronous operation completes.
// Provided function (DO NOT MODIFY)
function fetchData(callback: (data: string) => void): void {
  setTimeout(() => {
    callback("Sample Data");
  }, 50);
}
Loading editor...
typescript