Mocking a Factory Function with Jest
This challenge focuses on effectively mocking a factory function within a Jest testing environment. Factory functions are common patterns for creating objects with varying configurations, and testing code that utilizes them can be tricky if the factory has external dependencies or complex logic. You'll need to create a mock factory and verify that it's called with the correct arguments during your tests.
Problem Description
You are tasked with testing a function createUser that relies on a factory function createUserFactory to create user objects. The createUserFactory takes a name and an age as arguments and returns a user object with those properties. The goal is to mock createUserFactory in your Jest tests to isolate the createUser function and verify that the factory is called with the expected parameters. You should write a test that asserts that createUserFactory is called with specific name and age values.
Key Requirements:
- Create a factory function
createUserFactorythat acceptsname(string) andage(number) and returns a user object withnameandageproperties. - Create a function
createUserthat takesnameandageand usescreateUserFactoryto create a user. - Mock
createUserFactoryin a Jest test. - Assert that the mocked
createUserFactoryis called with the correct arguments.
Expected Behavior:
The createUser function should call createUserFactory with the provided name and age. The test should verify that createUserFactory is indeed called with these arguments, and that the returned user object from the mocked factory is not directly asserted (we only care about the call).
Edge Cases to Consider:
- Ensure the mock is properly implemented to avoid unexpected behavior.
- Consider how to handle potential errors within the factory function (although error handling isn't the primary focus of this challenge).
Examples
Example 1:
Input: createUser("Alice", 30);
Output: { name: "Alice", age: 30 } (created by the mocked factory)
Explanation: createUser calls createUserFactory("Alice", 30). The test verifies that createUserFactory is called with "Alice" and 30.
Example 2:
Input: createUser("Bob", 25);
Output: { name: "Bob", age: 25 } (created by the mocked factory)
Explanation: createUser calls createUserFactory("Bob", 25). The test verifies that createUserFactory is called with "Bob" and 25.
Constraints
- The factory function
createUserFactorymust accept a string fornameand a number forage. - The
createUserfunction must callcreateUserFactorywith the provided arguments. - The test should use Jest's mocking capabilities to mock
createUserFactory. - The test should only assert that
createUserFactoryis called with the correct arguments, not the returned user object.
Notes
- Think about how to use
jest.fn()to create a mock function. - Use
mock.callsormock.mock.callsto verify the arguments passed to the mock function. - Focus on isolating the
createUserfunction by mocking its dependency. - This challenge is designed to test your understanding of mocking and verifying function calls in Jest.