Hone logo
Hone
Problems

Mocking console.clear() in Jest

Jest provides powerful tools for testing asynchronous operations and interactions with the environment. One common scenario is needing to test functions that interact with the console, such as those that might clear it. This challenge focuses on creating a mock for console.clear() to verify its usage within your tests.

Problem Description

Your task is to create a Jest mock for the global console.clear() function. This mock should allow you to assert whether console.clear() was called and, optionally, how many times it was called. This is useful for testing code that is designed to tidy up the console output, ensuring that these cleanup operations are indeed executed when expected.

Key Requirements:

  • Create a Jest mock for console.clear().
  • The mock should be able to track if it has been called.
  • The mock should be resettable for different test cases.

Expected Behavior: When a function under test calls console.clear(), your mock should record this call. You should then be able to use Jest's assertion library (e.g., expect(mockConsoleClear).toHaveBeenCalled()) to verify this.

Edge Cases:

  • What happens if console.clear() is called multiple times?
  • What happens if console.clear() is called within different test suites or test files?

Examples

Example 1:

  • Input: A function clearConsoleOnTimeout that calls console.clear() after a delay.
  • Test Code Snippet:
    const mockConsoleClear = jest.fn();
    jest.spyOn(console, 'clear').mockImplementation(mockConsoleClear);
    
    // Assume a function like this exists:
    // function clearConsoleOnTimeout() {
    //   setTimeout(() => {
    //     console.clear();
    //   }, 100);
    // }
    
    clearConsoleOnTimeout(); // Hypothetically called
    
    // After a sufficient delay for the timeout to trigger
    await new Promise(resolve => setTimeout(resolve, 150));
    
    expect(mockConsoleClear).toHaveBeenCalledTimes(1);
    console.clear.mockRestore(); // Clean up the spy
    
  • Output: The assertion expect(mockConsoleClear).toHaveBeenCalledTimes(1) passes.
  • Explanation: The jest.spyOn creates a mock that replaces the original console.clear temporarily. The mockImplementation ensures our custom mock function is used. After the timeout, we assert that our mock was indeed called once.

Example 2:

  • Input: A function maybeClearConsole that conditionally calls console.clear().
  • Test Code Snippet:
    const mockConsoleClear = jest.fn();
    jest.spyOn(console, 'clear').mockImplementation(mockConsoleClear);
    
    // Assume a function like this exists:
    // function maybeClearConsole(shouldClear: boolean) {
    //   if (shouldClear) {
    //     console.clear();
    //   }
    // }
    
    maybeClearConsole(false); // Call without clearing
    expect(mockConsoleClear).not.toHaveBeenCalled();
    
    maybeClearConsole(true); // Call with clearing
    expect(mockConsoleClear).toHaveBeenCalledTimes(1);
    
    console.clear.mockRestore();
    
  • Output: Both assertions pass.
  • Explanation: We first test the case where console.clear() should not be called, verifying it wasn't. Then, we test the case where it should be called and confirm it was.

Constraints

  • Your solution must use Jest for mocking.
  • The solution should be written in TypeScript.
  • The mock should be correctly restored after each test or test suite to avoid interfering with other tests.
  • Focus on creating the mock setup and assertion strategy rather than implementing complex console-clearing logic in the function under test.

Notes

Consider using jest.spyOn to replace the console.clear method. This allows you to keep the original implementation accessible if needed, but more importantly, it lets you track calls to the mocked function. Remember to always restore your spies after they are no longer needed, typically in an afterEach or afterAll hook.

Loading editor...
typescript