How to correctly mock an ES6 class in Jest?

Both solid methods! But if you don’t want to mock the entire module, a great alternative is spying on the method directly using jest.spyOn().

This lets you track calls to send without messing with the implementation:

import Mailer from '../../../../server/services/emails/mailer';

test('email sent successfully', async () => {
  const sendSpy = jest.spyOn(Mailer, 'send').mockResolvedValue('The email was sent successfully!');
  
  const result = await Mailer.send('to@example.com', 'Subject', 'Body');
  
  expect(result).toBe('The email was sent successfully!');
  expect(sendSpy).toHaveBeenCalledWith('to@example.com', 'Subject', 'Body');
  
  sendSpy.mockRestore(); // Always clean up after!
});

With this approach, you get precise control while keeping the jest mock class implementation minimal. :dart: