firebase-functions-test
firebase-functions-test copied to clipboard
Params not created from refPath
Version info
firebase-functions-test: v0.1.1 firebase-functions: v1.0.1 firebase-admin: v5.12.0
Steps to reproduce
let data = test.database.makeDataSnapshot({
valProp1: "hello",
valProp2: 12345
}, "my/path/param1/param2");
await test.wrap(myFunctions.func1)(data);
Expected behavior
export let func1 = functions.database.ref('my/path/{param1}/{param2}').onCreate(async (snapshot, context) => {
let param1: string = context.params.param1; //"param1"
let param2: string = context.params.param2; //"param2"
let val1: string = snapshot.val().val1; //"hello"
let val2: number = snapshot.val().val2; //12345
)};
Actual behavior
export let func1 = functions.database.ref('my/path/{param1}/{param2}').onCreate(async (snapshot, context) => {
let param1: string = context.params.param1; //undefined
let param2: string = context.params.param2; //undefined
let val1: string = snapshot.val().val1; //"hello"
let val2: number = snapshot.val().val2; //12345
)};
Workaround
let data = test.database.makeDataSnapshot({
valProp1: "hello",
valProp2: 12345
}, "my/path/param1/param2");
await test.wrap(myFunctions.func1)(data,
{
params: {
param1: userId,
param2: orderId
}
});
It took a few debugging attempts to discover that the pathRef wasn't auto parsing into the context's params without the options in wrap().
Re-reading the docs, it looks like this is by design and similar to what firebase functions:shell was doing
However, If it's possible to automatically parse the refPath into context, please take this as a feature request 🙇.
Perhaps the docs could highlight that parsing params into context isn't a thing? 🤔
I just ran into this and its a total headscratcher. Makes my assertions a lot more complex and brittle and it seems like it would be default behavior for context to work with the path we provide.
Found a way to make it feel normal for now, although not ideal.
const snap = fft.database.makeDataSnapshot(
chatMessage,
"/chats/{chat_id}/{message_id}"
);
const params = {
chat_id: "existing_chat",
message_id: "new_message"
}
test("that the correct association is made", async () => {
await wrapped(snap, { params });
///blahblah
Please fix :(
Hey folks,
I agree this is definitely confusing and it's high on my list of fixes.
In the meantime, you can automatically construct your path from a string of wildcards like this...
const pathTemplate = "/a/{wild}"
const params = {
wild: "value"
};
const pathSnapshot = Object.keys(params).reduce((s, k) => {
return s.replace(`{${k}}`, options.params[k]);
}, pathTemplate);
const snap = fft.database.makeDataSnapshot(val, pathSnapshot);
Sweet, thanks!
Keep up the good work!
const pathSnapshot = Object.keys(params).reduce((s, k) => { return s.replace({${k}}, options.params[k]); }, pathTemplate);
Where do options come from in options.params?
Also is it a realtime db only solution? None of it works for me when trying to test functions+Firestore.
Any updates on this ? I still run into the issue today. Happy to help if needed :)
Any updates on this ?