Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to test a trigger #133

Closed
MorenoMdz opened this issue Feb 3, 2022 · 2 comments
Closed

How to test a trigger #133

MorenoMdz opened this issue Feb 3, 2022 · 2 comments

Comments

@MorenoMdz
Copy link

MorenoMdz commented Feb 3, 2022

Summary

Basically, we have some integration tests that run the triggers against the emulator, but I wanted to have those running against the mocked db instead. Trying to edit a mocked db document doesn't seem to trigger those, the updates are happening correctly but the internal code in the trigger never executes.

Relevant information

I was trying to call the function we have on the trigger, some onUpdate for example, like onUserUpdate is one we want to test, but to directly call those we need to provide the snap and context objects, the snap is defined as a Change which unfortunately I was not able to mock, but the context seems fine as long as you have an object with an EventType it accepts, but complains about trying to parse the Snap as a JSON:|

image

Qs:

  • Is it possible to mock the triggers so we can verify if our trigger helpers are being properly called?
  • Is possible to directly call those triggers somehow?

Once again thank you very much for the hard work done here.

@MorenoMdz
Copy link
Author

MorenoMdz commented Feb 3, 2022

Update, this seems to work, using the FB test module:

    const before = {
      data: () => ({
        instantPayEligible: false
      })
    };
    const after = {
      data: () => ({
        instantPayEligible: true
      })
    };
    const change = test.makeChange(before, after);
    const ctx = {
      params: { workerId: worker.uid }
    };

    const onUpdateTrigger = test.wrap(onWorkerStatusUpdate);

    onUpdateTrigger(change, ctx);

Update 2: It does not, as the firebase-functions-test has to wrap the onUpdateTrigger function, it overwrites the mocked db instance. Need some other way to wrap the onUpdateTrigger methods.

@MorenoMdz
Copy link
Author

Ended up extracting the onUpdate methods into external helpers that i can call directly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant