firebase-functions-test icon indicating copy to clipboard operation
firebase-functions-test copied to clipboard

Params not created from refPath

Open ahaverty opened this issue 7 years ago • 9 comments
trafficstars

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? 🤔

ahaverty avatar Apr 08 '18 16:04 ahaverty

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.

lukepighetti avatar Sep 25 '18 01:09 lukepighetti

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

lukepighetti avatar Sep 25 '18 01:09 lukepighetti

Please fix :(

jeremylorino avatar Oct 02 '18 22:10 jeremylorino

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

abeisgoat avatar Nov 27 '18 21:11 abeisgoat

Sweet, thanks!

Keep up the good work!

jeremylorino avatar Nov 27 '18 22:11 jeremylorino

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.

alekslario avatar Mar 26 '19 17:03 alekslario

Any updates on this ? I still run into the issue today. Happy to help if needed :)

tzvc avatar Jun 17 '20 16:06 tzvc

Any updates on this ?

akarabach avatar Aug 05 '20 14:08 akarabach