The question's in the title. With Jest it was console.log = jest.fn()
. How do I get and analyse the console output of the code I'm testing?
Asked
Active
Viewed 1,015 times
1 Answers
4
import { afterAll, describe, expect, vi } from 'vitest';
describe('should mock console.log', () => {
const consoleMock = vi.spyOn(console, 'log').mockImplementation(() => {});
afterAll(() => {
consoleMock.mockReset();
});
test('should log `sample output`', () => {
console.log('sample output');
expect(consoleMock).toHaveBeenCalledOnce();
expect(consoleMock).toHaveBeenLastCalledWith('sample output');
});
});

Yokozuna59
- 66
- 1
- 3
-
1exemplary answer – sureshvv Aug 24 '23 at 06:14