jest-mock-extended set property to undefined

466 Views Asked by At

Hi is to possible to set a mocked property to undefined somehow?

Interface I want to mock:

export interface IEmail {
  from: string | undefined;
  body: string;
  to: string;
}

Code I want to test:

async function sendEmail(emailData: IEmail): Promise<void> {
  await this.send({
    emailBody: emailData.body,
    emailBodyFrom: emailData.from || "[email protected]",
    emailTo: emailData.to,
  });
}

The test:

import { mock } from 'jest-mock-extended';


it('should send email', async () => {
    const options = mock<IEmail>({});
  options.from = undefined
  options.to = '[email protected]'
  mockemailService.send.mockResolvedValueOnce("");
  await emailService.sendEmail(options);
  expect(mockemailService.send).toHaveBeenCalledWith({
    from: '[email protected]',
    to: '[email protected]',
  });
});

I expect this the send function to have been called with [email protected] but instead it has been called with a mock function causing the test to fail. How can I forcefully set the property from to undefined?

0

There are 0 best solutions below