简体   繁体   中英

How to send context parameter in unit test to a firebase functions running on the functions emulator

I manage to start the firebase emulators and load a cloud function. Now I want to write a test.

PROBLEM I use chai-http to call the function in the emulator but I do not succeed in sending a context to the function.

when calling the function using chai-http , I see the following warning and error :

{"severity":"WARNING","message":"Request body has extra fields: context"} {"severity":"ERROR","message":"Invalid request, unable to process."}

Here is the test code snippet :

    it("function_call", async() => {
        await new Promise((resolve, reject) => {
            chai.request(url)
                .post("")
                .send({
                    data: {
                        id: FILE_ID
                    },
                    context: {
                        auth: {
                            uid: USER_ID,
                            token: USER_TOKEN
                        }
                    }
                })
                .end(function(err, res) {
                    console.info(JSON.stringify(res));
                    const payload = JSON.parse(res.text);
                    chai.expect(payload.error).not.null;
                    resolve();
                });

        });
        // expect some data from firestore emulator to be deleted
        const afterCAll = await firestore.collection(`users/${USER_ID}/files/${FILE_ID}`).get();
        chai.expect(afterCAll.empty).is.true;
    });

And here is the function code :

export const doSomething = async(data, context) => {

    console.log("context=" + JSON.stringify(context));
    console.log("context.auth=" + JSON.stringify(context.auth));

}


export const CLOUD_FUNCTION = functions
    .runWith(runtimeOpts)
    .region("REGION")
    .https
    .onCall(doSomething);

And to execute the test, i run :

firebase emulators:exec --project dev-export 'npm test --prefix functions --verbose --debug'

Leaving out the context in the parameter :

            chai.request(url)
                .post("")
                .send({
                    data: {
                        id: FILE_ID
                    }
                })

and the function call to the emulator works just fine

Looking for similar cases with the error you provided, I have a found a question that tries to get user credentials from context, and I noticed the functions they were using were not asynchronous, you might want to check context , and authData . You can try:

exports.doSomething = functions.https.onCall((data, context) => {
    const result = context;
    const result= context.auth;
    console.log("context=" + JSON.stringify(result));
    console.log("context.auth=" + JSON.stringify(resultAuth));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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