Implementing Auto-Mocking in Jest with TypeScript
Auto-mocking in Jest simplifies testing by automatically creating mock functions for imported modules when those modules are not actually present in the test environment. This is particularly useful for isolating units of code and avoiding dependencies on external services or complex setup. This challenge will guide you through implementing and utilizing auto-mocking to test a TypeScript module.
Problem Description
You are tasked with creating a test suite for a module named api.ts that interacts with a hypothetical external API. The api.ts module exports a function fetchData which is responsible for fetching data from the API. You need to write a Jest test suite that uses auto-mocking to mock the fetchData function without explicitly defining a mock implementation. The goal is to verify that the fetchData function is called with the correct arguments and that the code under test handles the mocked response appropriately.
The api.ts module looks like this:
// api.ts
export interface ApiResponse {
data: string;
}
export async function fetchData(url: string): Promise<ApiResponse> {
// In a real scenario, this would make an API call.
return new Promise((resolve) => {
setTimeout(() => {
resolve({ data: `Data from ${url}` });
}, 50);
});
}
You have another module, processor.ts, which uses api.ts:
// processor.ts
import { fetchData } from './api';
export async function processData(url: string): Promise<string> {
const response = await fetchData(url);
return `Processed: ${response.data}`;
}
Your test suite should test the processData function from processor.ts using auto-mocking. You need to ensure that fetchData is mocked, that it's called with the expected URL, and that the processData function returns the expected processed string based on the mocked API response.
Examples
Example 1:
Input: url = 'https://example.com/data'
Output: 'Processed: Data from https://example.com/data'
Explanation: The `fetchData` function is automatically mocked. The test verifies that `fetchData` is called with 'https://example.com/data' and that `processData` returns a string containing the mocked data.
Example 2:
Input: url = 'https://api.example.org/items'
Output: 'Processed: Data from https://api.example.org/items'
Explanation: Demonstrates that the URL passed to `processData` is correctly passed to the mocked `fetchData` function.
Constraints
- The test suite must use Jest's auto-mocking feature.
- The test suite must be written in TypeScript.
- The test suite must verify that
fetchDatais called with the correct URL. - The test suite must verify that
processDatareturns the expected processed string. - The test suite should be concise and readable.
- No explicit mock implementation of
fetchDatashould be provided.
Notes
- Jest's auto-mocking feature is enabled by default when importing modules in test files.
- You can access the mock function using the
mockproperty of the imported module. - Consider using
mockImplementationormockReturnValueon the mock function to control its behavior. - Pay close attention to the asynchronous nature of the
fetchDataandprocessDatafunctions. Useasync/awaitorthento handle promises correctly. - The
api.tsandprocessor.tsfiles are provided for context and should not be modified. Your solution should focus solely on the test file.