简体   繁体   English

如何对预定的 firebase 功能进行单元测试?

[英]How to unit test scheduled firebase function?

I have written a scheduled function like described here: https://firebase.google.com/docs/functions/schedule-functions我写了一个像这里描述的预定函数: https ://firebase.google.com/docs/functions/schedule-functions

How can I test this function now?我现在如何测试这个功能? When I wrap this function (as I would with any other cloud function), there is an error that says: "Property 'wrap' does not exist on type 'TestFunction'."当我包装此函数时(就像我对任何其他云函数所做的那样),出现一条错误消息:“属性‘wrap’在类型‘TestFunction’上不存在。”

const functionWrapper = test.wrap(function);

Is there any other way to test these functions?还有其他方法可以测试这些功能吗?

One workaround I found is isolating my code in a function and calling that function from the scheduled function.我发现的一种解决方法是将我的代码隔离在一个函数中,然后从预定函数中调用该函数。 When I test, instead of calling the scheduled function, I call the isolated function directly.我在测试的时候,没有调用调度函数,而是直接调用隔离函数。

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()

Not sure since when - but this is my way of handling the scheduler function.不确定从什么时候开始 - 但这是我处理调度程序功能的方式。 The problem I have is I do not have a context nor I do not know how to pass params to these functions.我遇到的问题是我没有上下文,也不知道如何将参数传递给这些函数。

WIP but at least I can easily manually run it on my local env. WIP,但至少我可以轻松地在我的本地环境中手动运行它。

You can write like this你可以这样写

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');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM