Advanced Jest Mocking: Timers and Module Techniques Quiz

Explore your understanding of advanced mocking techniques in Jest, focusing on timer control and module mocking for robust JavaScript testing. This quiz covers concepts such as simulating asynchronous behavior, replacing dependencies, and advanced use of timer mocks, ideal for developers refining their test suites.

  1. Mocking Timers Correctly

    Which method should you use in Jest to replace native timer functions like setTimeout and setInterval in your test environment for simulating timer behavior?

    1. jest.useFakeTimers()
    2. jest.clearAllMocks()
    3. jest.fn()
    4. jest.disableAutomock()

    Explanation: jest.useFakeTimers() is the method used for swapping out real timer functions with mocks, allowing you to precisely control asynchronous timing in tests. jest.clearAllMocks() is intended for resetting mock state, not timer functions. jest.fn() creates mock functions but doesn't affect timers. jest.disableAutomock() disables automatic mocking of modules but doesn't specifically deal with timer functions.

  2. Advancing Timer Mocks

    During a test involving a delayed callback, which Jest function advances fake timers so that timers like setTimeout complete instantly?

    1. jest.advanceTimersByTime()
    2. jest.toHaveBeenCalled()
    3. jest.mockTimers()
    4. jest.restoreTimers()

    Explanation: jest.advanceTimersByTime() fast-forwards the virtual clock by a specified amount, causing queued timers to trigger. jest.toHaveBeenCalled() is a matcher for checking calls and doesn't manipulate timers. jest.mockTimers() is not a part of the standard API and may be a confusion with actual timer mocking. jest.restoreTimers() is not in the official set; restoring timers would be done using jest.useRealTimers().

  3. Manual Module Mocking

    If you want to provide a custom mock implementation for a specific imported module in your test file, which Jest method allows you to do this explicitly?

    1. jest.mock()
    2. jest.fnManual()
    3. jest.setModuleMock()
    4. jest.manualMock()

    Explanation: jest.mock() is the standard method for explicitly replacing a module with a custom or automatic mock. jest.fnManual() is not a valid method and is likely a misremembered option. jest.setModuleMock() does not exist in standard APIs. jest.manualMock() may seem plausible but is not a Jest method; jest.mock() is the correct choice.

  4. Resetting Mock State

    After running multiple tests that use the same mocked module, which method should you call to ensure that all mocks are reset to their original, unmocked state?

    1. jest.resetModules()
    2. jest.clearTimers()
    3. jest.cleanAllMocks()
    4. jest.fakeTimersReset()

    Explanation: jest.resetModules() resets the module registry, ensuring that any previously mocked modules are reloaded as their actual implementations for the next require. jest.clearTimers() is not an actual Jest function; it's easy to confuse with timer clearing. jest.cleanAllMocks() is a typo of jest.clearAllMocks(), which only clears usage data but doesn't reset implementation. jest.fakeTimersReset() does not exist as an official method.

  5. Restoring Real Timer Functions

    What is the correct Jest method to revert timer functions like setInterval and setTimeout to their real implementations after using fake timers?

    1. jest.useRealTimers()
    2. jest.removeFakeTimers()
    3. jest.resetTimers()
    4. jest.enableRealTimers()

    Explanation: jest.useRealTimers() restores the original timer implementations after fake timers are in use, allowing timing-dependent code to run as normal. jest.removeFakeTimers() and jest.enableRealTimers() are not actual Jest methods, despite sounding plausible. jest.resetTimers() does not exist in the API, and other options are commonly confused with the correct method.