I have the following interface which I'm trying to mock:
export interface IEmail {
from: string;
body: string;
to: string;
}
This interface is used in a function like:
async function sendEmail(emailData: IEmail): Promise<void> {
await this.send({
emailBody: emailData.body,
emailBodyFrom: emailData.from,
emailTo: emailData.to,
});
}
async function send(email) {
verifyEmail(email)
emailprovider.send(email)
}
I tried mocking the interface with jest-mock-extended like this:
it('should send email', async () => {
const options = mock<IEmail>({
emailBody: "You got mail",
from: "marc@meta.com",
to: "jane.doe@gmail.com",
});
mockemailService.send.mockResolvedValueOnce("");
await emailService.sendEmail(options);
expect(mockemailService.send).toHaveBeenCalledWith({
emailBody: 'You got mail',
emailBodyFrom: 'marc@meta.com',
emailTo: 'jane.doe@gmail.com',
});
});
Running the test gives the following diff:
- emailBody: 'You got mail',
- emailBodyFrom: 'marc@meta.com',
- emailTo: 'jane.doe@gmail.com',
+ emailBody: undefined,
+ emailBodyFrom: undefined,
+ emailTo: undefined,
I have debugged and it seems like when I copy the vales from one object to another emailBody: emailData.body is causing the value to be undefined.
Is there a better way to create the mock perhaps?