Reputation: 6566
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
Upvotes: 4
Views: 629
Reputation: 864
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));
}
Upvotes: 0