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?
import { afterAll, describe, it, expect, vi } from 'vitest';
describe('should mock console.log', () => {
const consoleMock = vi.spyOn(console, 'log').mockImplementation(() => undefined);
afterAll(() => {
consoleMock.mockReset();
});
it('should log `sample output`', () => {
console.log('sample output');
expect(consoleMock).toHaveBeenCalledOnce();
expect(consoleMock).toHaveBeenLastCalledWith('sample output');
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With