Jest Function Mocking Techniques Quiz Quiz

Explore essential concepts and best practices for mocking functions using Jest, a popular JavaScript testing tool. This quiz challenges your understanding of spy utilities, mock implementations, and advanced mocking behaviors within Jest's tools ecosystem.

  1. Identifying How to Mock a Module Function

    Which method allows you to mock a specific function from an imported module when writing tests in Jest?

    1. jest.mock('moduleName')
    2. jest.fn('moduleFunction')
    3. spyOn('moduleFunction')
    4. mockFunction('moduleName')

    Explanation: jest.mock('moduleName') is correct, as it enables you to replace the entire module, including its exported functions, with mocked implementations. jest.fn('moduleFunction') only creates standalone mock functions and cannot directly mock imported module functions. spyOn('moduleFunction') is not the correct syntax for Jest, and mockFunction('moduleName') is not a valid Jest method.

  2. Changing Mock Behavior Per Call

    If you need a mocked function in Jest to return different values for each call, which method should you use in your test definition?

    1. mockReturnValueOnce
    2. mockResolvedValue
    3. mockCalledWith
    4. mockOnCall

    Explanation: mockReturnValueOnce is the correct way to set up a Jest mock that returns different values on consecutive calls, ideal for testing various scenarios. mockResolvedValue is used for promises but always returns the same value. mockCalledWith is not a recognized Jest method. mockOnCall sounds similar but is not part of the Jest API.

  3. Accessing Arguments of Mock Calls

    When using Jest to mock a function, which property stores the arguments passed to the mock in each invocation?

    1. mock.calls
    2. mock.arguments
    3. mock.instances
    4. mockValues

    Explanation: mock.calls is the correct property that contains arrays of arguments used in each invocation of the mock function. mock.arguments does not exist in Jest. mock.instances stores instances created by the mock when used as a constructor. mockValues is not a recognized property in Jest.

  4. Resetting and Clearing Mocks

    In which scenario should you use jest.clearAllMocks() instead of jest.resetAllMocks() to manage mocks between tests?

    1. When you want to clear usage data but keep mock implementation unchanged
    2. When you want to restore all original implementations
    3. When you want to remove all mock functions entirely
    4. When you want to mock a function only one time

    Explanation: jest.clearAllMocks() clears usage information like call count and arguments, but retains the mock implementation. jest.resetAllMocks() resets both usage data and implementation to the default state. Removing mock functions entirely or one-time mocking is not the purpose of either method.

  5. Mocking Return Values of Promises

    What is the correct way in Jest to make a mocked function resolve to a specific value when it is awaited?

    1. mockResolvedValue
    2. mockReturnThisValue
    3. mockPromiseValue
    4. resolveValueMock

    Explanation: mockResolvedValue sets up the mock to return a promise that resolves to the given value, which is suitable for async tests. mockReturnThisValue is not a Jest method. mockPromiseValue and resolveValueMock are also incorrect, as they do not exist in Jest's API.