[英]How to mock mailgun.messages().send() using Jest?
I am using mailgun-js Api
to send mail.我正在使用mailgun-js Api
发送邮件。 I've written an integration test instead of unit test.我写了一个集成测试而不是单元测试。 Now i need to write unit test case for sendEmail
method wrapped inside Mailgun
class.现在我需要为封装在Mailgun
类中的sendEmail
方法编写单元测试用例。 I don't know to how to mock mailgun.messages().send()
.我不知道如何模拟mailgun.messages().send()
。 can anyone help me with this?谁能帮我这个?
You need to use jest.mock()
to mock mailgun-js
module.您需要使用jest.mock()
来模拟mailgun-js
模块。
Eg例如
mailgun.ts
: mailgun.ts
:
import mg from 'mailgun-js';
// Get database name from configuration file
const mailgunConfig: mg.ConstructorParams = {
apiKey: '123',
domain: 'example.com',
};
// Setup Mailgun Service
const mailgun = mg(mailgunConfig);
// Mailgun Service class
export class Mailgun {
/**
* Static function to send an email.
* @param {mg.messages.SendData} emailParams - parameters to sent an email.
* @returns {Promise<mg.messages.SendResponse>} - maligun response with message and id.
*/
static sendEmail = (emailParams: mg.messages.SendData): Promise<mg.messages.SendResponse> => {
return mailgun.messages().send(emailParams);
};
}
mailgun.test.ts
: mailgun.test.ts
:
import { Mailgun } from './mailgun';
import mg from 'mailgun-js';
jest.mock('mailgun-js', () => {
const mMailgun = {
messages: jest.fn().mockReturnThis(),
send: jest.fn(),
};
return jest.fn(() => mMailgun);
});
// sendEmail Test Suite
describe('Send Email', () => {
it('returns the promise of queued object with id and message keys when valid email is passed', async () => {
// Preparing
const emailParam = {
from: 'noreply@shop.techardors.com',
to: 'ecomm@api.com',
subject: 'TechArdors Shopping: One-time passcode to reset the password',
html:
'<p>Please enter the passcode <strong>5124</strong> to reset the password. It expires in 5 minutes from the time it is requested.</p>',
};
const mailgun = mg({} as any);
(mailgun.messages().send as jest.MockedFunction<any>).mockResolvedValueOnce({
id: '222',
message: 'Queued. Thank you.',
});
// Executing
const result = await Mailgun.sendEmail(emailParam);
// Verifying
expect(result.message).toBe('Queued. Thank you.');
expect(result).toHaveProperty('id');
expect(result).toHaveProperty('message');
expect(mailgun.messages).toBeCalled();
expect(mailgun.messages().send).toBeCalledWith(emailParam);
});
});
Unit test result with coverage report:带有覆盖率报告的单元测试结果:
PASS src/stackoverflow/59463875/mailgun.test.ts (7.587s)
Send Email
✓ returns the promise of queued object with id and message keys when valid email is passed (7ms)
------------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
------------|----------|----------|----------|----------|-------------------|
All files | 100 | 100 | 100 | 100 | |
mailgun.ts | 100 | 100 | 100 | 100 | |
------------|----------|----------|----------|----------|-------------------|
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 8.723s
Source code: https://github.com/mrdulin/jest-codelab/tree/master/src/stackoverflow/59463875源代码: https : //github.com/mrdulin/jest-codelab/tree/master/src/stackoverflow/59463875
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.