firebase / quickstart-testing

Samples demonstrating how to test your Firebase app
Apache License 2.0
335 stars 92 forks source link

typescript please! #107

Open jmagaram opened 3 years ago

jmagaram commented 3 years ago

Desperately needing samples in TypeScript. I'm trying to test functions now and don't have a clue what parameters are possible on Request and Response objects. Have Googled for hours and can't find what I need. Would love to see some really basic function tests in Mocha and Typescript against the emulator. Tests for...

HTTP If function is supposed to return an uppercase version of what is passed in, check that Function grabs parameters out of the query string If function is supposed to return an error code, like forbidden, show that If function is supposed to write to a document Fail if not authenticated, return proper status code Check claims, fail if not met

INTERNAL SERVER On user create, create a document On user delete, delete a document On document changes, do something

===

Digging through your code a bit I see references to "core" and Express. Can I even get these types into my code? I'd like to strongly type my request and response objects. Where do I import these from?

===

Investigated some more and see it is Express web server under the covers? I'm going to assume that testing this in Typescript is going to be tough because there are so many possible properties on the Request object - headers and such - and so many ways to build the response and I'd need test helpers to construct these things in such a way that they'd type check. Also don't know how this interacts with the Auth stuff - do I get the token from the headers or from the Auth API. This is really beyond me - too much to learn - probably will just skip the unit testing. Can't do it without some getting started info from you with common cases in code.

samtstern commented 3 years ago

@jmagaram thanks for the feedback! Yes you're right, all Node.js Google Cloud Functions (and therefore Firebase functions) which use HTTP triggers use Express for their request/response. That's why we don't have documentation on those types on our site, they're not actually part of our code.

By default there's no Authentication state included with a request to an HTTPS function. You can pass whatever headers you want.

If you want to use a more "standardized" approach to HTTPS functions check out Callable Functions: https://firebase.google.com/docs/functions/callable

These functions have a more opinionated structure so:

I think you'll find things much better typed with callable functions, if you try them let me know what you think.

jmagaram commented 3 years ago

Wrote one and called it from my app. Worked great. Haven't tried calling it from a unit test.

samtstern commented 3 years ago

@jmagaram glad to hear you're making progress! Once you try unit testing please let me know if you think there are some patterns which need more explanation here.

alexshikov commented 10 months ago

Could anyone please share a short example of TypeScript unit-test for the onCall function? For some reason it neither easy to find an example nor write a working unit-test for the simplest function like:

import {onCall} from "firebase-functions/v2/https";
export const helloWorld = onCall((request) => {
  return "Hello!";
});
alexshikov commented 10 months ago

Alright, I believe I've found it:

import 'mocha';
import { expect } from 'chai';
import { helloWorld } from '../src/index';

describe('Hello World HTTP Function', () => {
  it('should return hello', () => {
    const res = helloWorld.run({} as any);
    expect(res).to.equal('Hello!');
  });
});