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

Equality of HttpsError: AssertionError: expected promise to be rejected with 'Error: uid-is-missing' but it was rejected with 'Error: uid-is-missing' #920

Closed
nilsreichardt opened this issue Jul 23, 2021 · 5 comments

Comments

@nilsreichardt
Copy link

Related issues

[REQUIRED] Version info

node: v14.15.4

firebase-functions: 3.14.1

firebase-tools: -

firebase-admin: 9.9.0

[REQUIRED] Test case

function.ts

import * as functions from "firebase-functions";
import { HttpsError } from "firebase-functions/lib/providers/https";
import { region } from "../../../firebase_globals";

export function myFunction() {
    return functions.region(region).https.onCall(async (data, context) => {
        throw new HttpsError('unauthenticated', 'uid-is-missing');
    });
}

function_test.ts

import { expect, use } from "chai";
import * as chai from "chai-as-promised";
import { HttpsError } from "firebase-functions/lib/providers/https";
import { describe, it } from "mocha";
import { myFunction } from '../../../../src/application/myFunction';
import { testEnvironment } from "../../../test_environment";
use(chai);

describe.only("MyFunction", () => {
    describe("parameters", () => {
        describe("unauthenticated", () => {
            it("throws an error, if user is not authenticated", async () => {
                const onCall = testEnvironment.wrap(sendPreLobbyInvitation());
                await expect(onCall({})).to.be.rejectedWith(new HttpsError('unauthenticated', 'uid-is-missing'));
            });
        });
    })
});

[REQUIRED] Steps to reproduce

  1. Create Project
  2. Copy functions_test.ts
  3. Copy functions.ts
  4. Try to run tests with mocha --timeout 20000 --require ts-node/register --recursive \"test_e2e/**/*_test.ts\" --reporter spec -u tdd

[REQUIRED] Expected behavior

The test should pass, because the two HttpsErrors are the same.

[REQUIRED] Actual behavior

AssertionError: expected promise to be rejected with 'Error: uid-is-missing' but it was rejected with 'Error: uid-is-missing'
      + expected - actual

The problem is the equality of HttpsError. If I store the HttpsError as a global variable, I can reuse it my tests. But this is only a workaround. Is there a way to achieve equality for HttpsError or to test HttpsError better?

Were you able to successfully deploy your functions?

@google-oss-bot
Copy link
Collaborator

I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.

@taeold
Copy link
Contributor

taeold commented Jul 23, 2021

@nilsreichardt I don't think there are specific helper functions in Firebase Functions SDK that might help in your case.

I saw similar issue and suggested workaround on chaijs/chai#1065. Can you give those a try and let us know if it helps with your case?

@taeold taeold added Needs: Author Feedback Issues awaiting author feedback and removed needs-triage labels Jul 23, 2021
@nilsreichardt
Copy link
Author

No, I couldn't get a workaround to work. Either the test always failed or the test always passed (even with wrong data).

@google-oss-bot google-oss-bot added Needs: Attention and removed Needs: Author Feedback Issues awaiting author feedback labels Jul 24, 2021
@taeold
Copy link
Contributor

taeold commented Jul 26, 2021

(I'm going to close the issue, as I don't think this issue is related to code written in this repository. Feel free to re-open if you disagree)

Few more things I notice on second pass:

  1. Is onCall() function returning a promise? If so, try including eventually.be. cluase (check out Chai doc for more examples):
const onCall = testEnvironment.wrap(sendPreLobbyInvitation());
await expect(onCall({})).to.be.eventually.be.rejectedWith(...);
  1. Instead of instantiating a new HttpError when comparing, give this a try:
...rejectedWith(HttpError, "'uid-is-missing'");

@taeold taeold closed this as completed Jul 26, 2021
@nilsreichardt
Copy link
Author

This seems to work 🎉

...rejectedWith(HttpsError, new RegExp("^uid-is-missing$"));

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

No branches or pull requests

3 participants