I am following this tutorial on unit testing firebase functions: https://fireship.io/lessons/testing-cloud-functions-in-firebase/
I added a unit test for a onCreate realtime database trigger and while the data arrives in it, the context.params come in as empty for the unit test.
so I have
test('checks that the user gets created', async () => {
const userId = 'aaaaaaa';
const path = `/users/${userId}`;
const data = {
"id": userId,
"firstName": "Dan",
"phoneNumber": "+11111111111",
"carInfo": {
"color": "blue",
"make": "Ford",
"model": "Focus",
"plate": "BV-11-AAA"
}
};
// Create a Database snapshot
const snap = testEnv.database.makeDataSnapshot(data, path);
// Execute it
await wrapped(snap);
const after = await admin.database().ref(path).once('value');
const afterValue = after.val();
expect(afterValue.phoneNumber).toBe('+11111111111')
});
and my actual function (index.ts) being:
export const newUserCreated = database.ref('/users/{userId}').onCreate(async (snapshot: DataSnapshot, context) => {
console.log('context params : ', context.params);
const user = snapshot.val();
await createStripeCustomerHandler(user, snapshot.key);
});
I get the actual payload in my function's handler, BUT:
There's 2 issues with it:
- It does not actually perform the save into the database, so the test itself fails (
afterValueis null) (might be intended behaviour actually. not sure) - The context params come in as empty when executing the unit test, so it prints out
context params : {}. do I need to send the params for the context manually? from thetestEnv.database.makeDataSnapshotcommand?
Am I doing something wrong or am I missing anything? Thank you in advance.