如何单元测试计划的燃烧函数



我已经写了一个计划的函数,如下所述:https://firebase.google.com/docs/functions/schedule-functions

现在如何测试此功能?当我包装此函数(就像其他任何其他云功能一样(时,有一个错误,上面写着:"属性'wrap'在类型'testfunction'上不存在。"

const functionWrapper = test.wrap(function);

还有其他测试这些功能的方法吗?

我发现的一个解决方法是在函数中隔离我的代码,并将该函数从计划函数中调用。当我测试时,我不是调用计划的函数,而是直接调用隔离函数。

ex:

export const dailyJob = functions.pubsub
        .schedule('0 0 * * *')
        .onRun(async context => {
    return isolatedFunction();
})
export function isolatedFunction() {
    ...
}
> firebase functions:shell  
> firebase> RUN_NAME_OF_THE_FUCTION()

不确定何时 - 但这是我处理调度程序函数的方式。我遇到的问题是我没有上下文,也不知道如何将参数传递到这些功能。

wip,但至少我可以轻松地在本地env中手动运行

您可以像这样写

exports.scheduledFunction = () => functions.pubsub.schedule('every 1 minutes').onRun((context) => {
    console.log('Start scheduledFunction every 1 minutes');
    sendEmail();
    return null;
});

async function sendEmail() {
    console.log('Start sendEmail');
}

相关内容

最新更新