quickstart-testing icon indicating copy to clipboard operation
quickstart-testing copied to clipboard

typescript please!

Open jmagaram opened this issue 4 years ago • 5 comments

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.

jmagaram avatar Feb 24 '21 06:02 jmagaram

@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:

  • Passing data in/out is easier
  • Authentication is seamlessly handled with Firebase Auth, no need to mess with headers
  • Returning an error is simple with the HttpsError class

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

samtstern avatar Feb 26 '21 17:02 samtstern

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

jmagaram avatar Mar 10 '21 04:03 jmagaram

@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.

samtstern avatar Mar 10 '21 10:03 samtstern

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 avatar Dec 21 '23 23:12 alexshikov

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!');
  });
});

alexshikov avatar Dec 22 '23 00:12 alexshikov