如何测试sendgrid-nodejs函数?

时间:2019-04-04 08:42:22

标签: node.js firebase google-cloud-functions sendgrid

我创建了一个firebase函数来使用sendgrid发送邮件,但是不知道如何测试我的函数。下面是我的代码:

import * as sgMail from '@sendgrid/mail';
import * as functions from "firebase-functions";

const SENDGRID_API_KEY = functions.config().sendgrid.key;
const SENDGRID_TEMPLATE_ID = functions.config().sendgrid.template_id;
const SENDGRID_FROM_EMAIL = functions.config().sendgrid.sender.email;
const SENDGRID_FROM_NAME = functions.config().sendgrid.sender.name;

sgMail.setApiKey(SENDGRID_API_KEY);

interface MailData {
    subject: String;
    name: String;
    otherField: String;
}

interface Mailer {
    email: String;
    name?: String;
}

interface MailObject {
    to: Mailer;
    dynamic_template_data: MailData;
}

function sendEmail (mailObject: MailObject) {
    const msg = {
        to: mailObject.to,
        from: {
            email: SENDGRID_FROM_EMAIL,
            name: SENDGRID_FROM_NAME
        },
        templateId: SENDGRID_TEMPLATE_ID,
        substitutionWrappers: ['{{', '}}'],
        dynamic_template_data: mailObject.dynamic_template_data
    }
    console.log(msg);
    return sgMail.send(msg);
}

export default sendEmail;

如何测试sendEmail功能(编写单元测试或相同的测试,不发送并打开邮箱并检查邮件)?

0 个答案:

没有答案