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: "[email protected]",
to: "[email protected]",
});
mockemailService.send.mockResolvedValueOnce("");
await emailService.sendEmail(options);
expect(mockemailService.send).toHaveBeenCalledWith({
emailBody: 'You got mail',
emailBodyFrom: '[email protected]',
emailTo: '[email protected]',
});
});
Running the test gives the following diff:
- emailBody: 'You got mail',
- emailBodyFrom: '[email protected]',
- emailTo: '[email protected]',
+ 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?
I was able to resolve it by setting the fields like this instead: