Hone logo
Hone
Problems

Robust clearAllTimers Implementation and Jest Testing

Many JavaScript applications, especially those using asynchronous operations, rely on setTimeout and setInterval for scheduling tasks. Managing these timers can become complex, and it's crucial to ensure they are cleared properly during testing to prevent unexpected behavior and flaky tests. This challenge asks you to create a utility function clearAllTimers that clears all pending timers and then write Jest tests to verify its functionality under various conditions.

Problem Description

You need to implement a TypeScript function called clearAllTimers that clears all currently active timers (created using setTimeout or setInterval) in the global scope. The function should iterate through all timer IDs and call clearTimeout or clearInterval accordingly. The goal is to provide a reliable way to reset the timer environment during testing, preventing timers from interfering with test results.

Key Requirements:

  • The function must clear all timers, regardless of whether they were created with setTimeout or setInterval.
  • It should handle cases where timer IDs are invalid (e.g., already cleared or never existed).
  • The function should not throw errors if a timer ID is invalid.
  • The function should be safe to call multiple times without causing issues.
  • The function should be written in TypeScript.

Expected Behavior:

When clearAllTimers is called, all pending timers in the global scope should be cancelled. Subsequent calls to clearAllTimers should not cause errors or unexpected behavior. Tests should pass even if timers are set up before the test and cleared by clearAllTimers.

Edge Cases to Consider:

  • No timers are currently set.
  • A mix of setTimeout and setInterval timers are set.
  • Invalid timer IDs exist (e.g., due to previous clearing).
  • Timers are set in different scopes (though this function should only clear global timers).

Examples

Example 1:

Input:  Global scope with 2 setTimeout timers and 1 setInterval timer.
Output: All timers are cleared. No errors are thrown.
Explanation: The function iterates through all timer IDs and calls clearTimeout/clearInterval as appropriate.

Example 2:

Input: Global scope with no timers set.
Output: The function completes without error. No timers are cleared (as there are none).
Explanation: The function should gracefully handle the case where there are no timers to clear.

Example 3: (Edge Case)

Input: Global scope with a valid setTimeout timer and an invalid timer ID (e.g., -1).
Output: The valid timer is cleared. The invalid timer ID is ignored without error.
Explanation: The function should not throw an error when encountering an invalid timer ID. It should continue processing other timers.

Constraints

  • The function must be written in TypeScript.
  • The function should only clear timers in the global scope. It should not attempt to clear timers in other scopes.
  • The function should be performant enough for typical testing scenarios (clearing a few dozen timers should be fast).
  • The function should not rely on external libraries.

Notes

  • You can access all timers using setTimeout and setInterval properties on the global window object (or global in Node.js environments, though this challenge assumes a browser-like environment).
  • Consider using a try...catch block within the loop to handle potential errors when clearing individual timers. This will prevent one invalid timer ID from stopping the entire clearing process.
  • Think about how to test the function effectively using Jest. You'll need to set up timers and then verify that they are cleared. jest.useFakeTimers() can be helpful for controlling the timer environment during testing.
  • Remember to mock the window object if you are running tests in a Node.js environment.
Loading editor...
typescript