Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Calculator

0

48
Views
How to test multiple console outputs with Jest in JavaScript?

I need this solution for an educational project. This unit test should check an expected console output from the function:

it('should log into the console "Victoria lifting anchor up" and "Victoria is moving"', () => {

  const consoleSpy = jest.spyOn(console, 'log');

  victoria.move();

  expect(consoleSpy).toHaveBeenCalledWith(?);

});

The problem is that the function victoria.move() runs two console logs, and I want to check both of them in one unit test. The test perfectly works with one output, but I do not know what notation should be for testing two outputs. I strugle to find the syntax on the Internet.

7 months ago · Juan Pablo Isaza
1 answers
Answer question

0

You can use the .calls property of consoleSpy. There is an example in the official documentation "using a mock function". In your case it could be done like this

expect(mockCallback.mock.calls[0][0]).toBe('your value'); // first call, first argument
expect(mockCallback.mock.calls[1][0]).toBe('your value');  // second call, first argument
7 months ago · Juan Pablo Isaza Report
Answer question
Find remote jobs