How to Check for Log Errors With Jest

There are two ways to check for log errors with the JavaScript testing framework Jest: jest.fn() or jest.spyOn(). Here’s how to do it, with code.  

Written by Chris Cook
Published on Oct. 09, 2024
developer debugging code with Jest
Image: Shutterstock / Built In
Brand Studio Logo

In Jest, there are two ways to verify that console.log was correctly called: You can mock the function with jest.fn() or use jest.spyOn()

This is important because we all love console.log to debug our applications. If something is not working as expected, we throw in a console log to see what’s going on. Besides for debugging purposes, we can use this information for our tests.

Checking for Log Errors Using Jest Explained

Jest is a JavaScript testing framework that can be used to test the console.log and check for errors. It’s important for debugging an application and identifying what went wrong. Here’s how to do it using jest.spyOn() to intercept the function calls and track them in the console log:

// function to be tested and imported from somewhere else  
function someComplexFunction() {
  console.log("some important message");
  console.log("some other message");

  return 42;
}

// test case for someComplexFunction() 
describe('Test someComplexFunction', () => {
  test('Console log should have been called', () => {
    const logSpy = jest.spyOn(global.console, 'log');

    someComplexFunction();

    expect(logSpy).toHaveBeenCalled();
    expect(logSpy).toHaveBeenCalledTimes(2);
    expect(logSpy).toHaveBeenCalledWith('some important message');
    expect(logSpy.mock.calls).toContainEqual(['some other message']);

    logSpy.mockRestore();
  });
});

In testing, we usually call a function with a certain input and expect a certain output. Sometimes it can be useful to also test the intermediate steps that happened between the start and the end of the function, for example to check if the warnings and errors were logged correctly.

Here’s how to test a console log using Jest.

More on Software EngineeringHow to Stop a ForEach Loop in JavaScript

 

How to Check for Log Error Using Jest

There are two ways to verify that console log was called correctly in Jest.  First, we can mock the function with jest.fn(), which temporarily replaces the original console log function with a dummy function. 

However, I prefer the second option of using jest.spyOn() to intercept function calls to the console log and track them. This doesn’t change the original behavior of console log, which means we still see the messages printed to the console.

// function to be tested and imported from somewhere else  
function someComplexFunction() {
  console.log("some important message");
  console.log("some other message");

  return 42;
}

// test case for someComplexFunction() 
describe('Test someComplexFunction', () => {
  test('Console log should have been called', () => {
    const logSpy = jest.spyOn(global.console, 'log');

    someComplexFunction();

    expect(logSpy).toHaveBeenCalled();
    expect(logSpy).toHaveBeenCalledTimes(2);
    expect(logSpy).toHaveBeenCalledWith('some important message');
    expect(logSpy.mock.calls).toContainEqual(['some other message']);

    logSpy.mockRestore();
  });
});

With the mock object returned by jest.spyOn() we can perform various checks. For example, we can check whether the console log was called, how many times it was called and with what message it was called.

A tutorial on how to debug in Jest. | Video: Software Testing Help

More on JavaScriptHow to Auto Import Vue Components in JavaScript

Also, we can use the logSpy.mock.calls object to check each individual call to console log and in what order it was made. At the end of the test, we need to restore the original console log function with logSpy.mockRestore(). This will ensure that there are no side effects in other tests.

Finally, when we run the tests with Jest, we see that all tests passed.

Jest Test Summary

Additionally, we see the output of the console log statements of our someComplexFunction test. If we want to hide these messages from the test summary, we can completely replace the console log function with an empty mock by adding .mockImplementation(() => { })

// replace console log with an empty mock function
const logSpy = jest.spyOn(global.console, 'log')
    .mockImplementation(() => { });

// do some tests...    
// no message will be printed to the console

// restore original console log
logSpy.mockRestore();

Now, no messages will appear on the console.

Frequently Asked Questions

Jest is a testing framework in JavaScript that can be used to check the console log and debug any errors in an application. 

There are two ways to test a console log for errors with Jest: jest.fn() and jest.spyOn(). Here’s how to do it:

// function to be tested and imported from somewhere else  
function someComplexFunction() {
  console.log("some important message");
  console.log("some other message");

  return 42;
}

// test case for someComplexFunction() 
describe('Test someComplexFunction', () => {
  test('Console log should have been called', () => {
    const logSpy = jest.spyOn(global.console, 'log');

    someComplexFunction();

    expect(logSpy).toHaveBeenCalled();
    expect(logSpy).toHaveBeenCalledTimes(2);
    expect(logSpy).toHaveBeenCalledWith('some important message');
    expect(logSpy.mock.calls).toContainEqual(['some other message']);

    logSpy.mockRestore();
  });
});
Explore Job Matches.