Yevhen Zhemchuzhnykov
Yevhen Zhemchuzhnykov

Reputation: 53

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.

Upvotes: 0

Views: 1316

Answers (1)

emartini
emartini

Reputation: 173

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

Upvotes: 2

Related Questions