Hone logo
Hone
Problems

Testing Transformations with Jest: A Mock Function Challenge

Jest's mock and mockImplementation features are powerful tools for isolating units of code during testing. This challenge focuses on using these features to effectively test functions that rely on external transformations. You'll be creating a Jest test suite to verify the behavior of a function that applies a transformation to an input value, using a mock function to control the transformation process.

Problem Description

You are given a function processData that takes an input value and applies a transformation to it using a function called transform. Your task is to write a Jest test suite that verifies the behavior of processData by mocking the transform function. The goal is to ensure that processData correctly calls transform with the appropriate input and then uses the result of transform to produce the expected output. This allows you to test processData in isolation, without relying on the actual implementation of transform.

What needs to be achieved:

  1. Create a mock function using Jest's jest.fn().
  2. Use mockImplementation to define the behavior of the mock function.
  3. Verify that processData calls the mock function with the correct arguments.
  4. Verify that processData uses the return value of the mock function to produce the correct output.

Key Requirements:

  • The processData function should accept an input value and a transform function as arguments.
  • processData should call the transform function with the input value.
  • processData should return the result of the transform function.
  • The test suite should use a mock function to replace the original transform function.
  • The test suite should verify that the mock function is called with the correct arguments and that processData returns the expected value based on the mock's return value.

Expected Behavior:

The test suite should pass if the mock function is correctly implemented and processData behaves as expected. The tests should cover scenarios where the mock function returns different values to ensure processData handles these variations correctly.

Edge Cases to Consider:

  • What happens if transform is not a function? (While not explicitly required to test, consider how processData should behave in this case - it's good practice to think about robustness).
  • What happens if transform throws an error? (Again, not required to test, but good to consider).

Examples

Example 1:

Input: processData(5, (x) => x * 2)
Output: 10
Explanation: processData calls the transform function with 5, and the transform function returns 5 * 2 = 10. processData then returns 10.

Example 2:

Input: processData(10, (x) => x - 5)
Output: 5
Explanation: processData calls the transform function with 10, and the transform function returns 10 - 5 = 5. processData then returns 5.

Example 3: (Mocking Scenario)

Input: processData(3, transformFunction)
Output: (Depends on the mock implementation)
Explanation: We mock transformFunction to return a specific value (e.g., 7). processData calls the mock with 3, and returns the mock's return value (7).

Constraints

  • The solution must be written in TypeScript.
  • The solution must use Jest for testing.
  • The processData function is provided (see below). You should not modify it.
  • The test suite should include at least two test cases to cover different scenarios.
  • Performance is not a primary concern for this challenge. Focus on correctness and clarity.

Notes

  • Think about how to use mockImplementation to control the return value of the mock function.
  • Use Jest's assertion methods (e.g., toHaveBeenCalledWith, mockReturnValue, toBe) to verify the behavior of processData.
  • Consider using mockResolvedValue if your transform function returns a Promise.
// Provided function - DO NOT MODIFY
function processData(input: number, transform: (x: number) => number): number {
  return transform(input);
}
Loading editor...
typescript