Hone logo
Hone
Problems

Mocking a Function with Jest in TypeScript

This challenge focuses on creating and utilizing mock functions within Jest tests for TypeScript code. Mocking allows you to isolate units of code by replacing dependencies with controlled substitutes, enabling focused and reliable testing. You'll be creating a mock function and verifying its interactions within a test scenario.

Problem Description

You are given a TypeScript function fetchData that relies on an external function apiCall to retrieve data. Your task is to write a Jest test that mocks the apiCall function to control the data returned by fetchData and verify that apiCall is called with the correct arguments. The goal is to test fetchData in isolation, without actually making a real API call.

What needs to be achieved:

  1. Create a mock implementation of the apiCall function.
  2. Use jest.mock to replace the original apiCall with your mock.
  3. Call the fetchData function.
  4. Assert that apiCall was called with the expected arguments.
  5. Assert that fetchData returns the expected value based on the mocked apiCall's return value.

Key Requirements:

  • The mock function should return a predefined value during the test.
  • The test should verify that the mock function is called with the correct arguments.
  • The test should verify that the fetchData function returns the expected result based on the mock's return value.

Expected Behavior:

The test should pass if the mock function is correctly implemented, jest.mock is used properly, and the assertions verify the expected interactions and return values.

Edge Cases to Consider:

  • Ensure the mock function is properly scoped within the test file.
  • Consider how the mock function's return value affects the fetchData function's behavior.

Examples

Example 1:

Input: fetchData(123) where apiCall is mocked to return { data: "mocked data" }
Output: { data: "mocked data" }
Explanation: fetchData calls apiCall with the argument 123. The mock apiCall returns { data: "mocked data" }, which is then returned by fetchData.

Example 2:

Input: fetchData("abc") where apiCall is mocked to return { data: null }
Output: { data: null }
Explanation: fetchData calls apiCall with the argument "abc". The mock apiCall returns { data: null }, which is then returned by fetchData.

Constraints

  • The apiCall function is assumed to be imported from a separate module.
  • The fetchData function takes a single argument of type string | number.
  • The apiCall function is assumed to take a single argument of the same type as fetchData and returns an object with a data property.
  • The test should be written using Jest and TypeScript.

Notes

  • jest.mock is the primary tool for replacing modules with mock implementations.
  • The mockImplementation method of the mock function allows you to define its behavior.
  • mock.calls property can be used to inspect the arguments passed to the mock function.
  • Think about how to structure your mock function to return different values based on the input to fetchData.
// apiCall.ts (Assume this exists and is imported)
export function apiCall(id: string | number): { data: any } {
  // In reality, this would make an API call
  return { data: 'real data' };
}

// fetchData.ts (Assume this exists and is imported)
import { apiCall } from './apiCall';

export function fetchData(id: string | number): { data: any } {
  return apiCall(id);
}
Loading editor...
typescript