Hone logo
Hone
Problems

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 createUserFactory that accepts name (string) and age (number) and returns a user object with name and age properties.
  • Create a function createUser that takes name and age and uses createUserFactory to create a user.
  • Mock createUserFactory in a Jest test.
  • Assert that the mocked createUserFactory is 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 createUserFactory must accept a string for name and a number for age.
  • The createUser function must call createUserFactory with the provided arguments.
  • The test should use Jest's mocking capabilities to mock createUserFactory.
  • The test should only assert that createUserFactory is 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.calls or mock.mock.calls to verify the arguments passed to the mock function.
  • Focus on isolating the createUser function by mocking its dependency.
  • This challenge is designed to test your understanding of mocking and verifying function calls in Jest.
Loading editor...
typescript