Closed noelmansour closed 5 years ago
After some further investigation I've managed to find a workaround.
Looking at the function declarations for both:
export declare function onRequest(handler: (req: Request, resp: express.Response) => void): HttpsFunction;
export declare function onCall(handler: (data: any, context: CallableContext) => any | Promise<any>): HttpsFunction & Runnable<any>;
I can see that onCall
returns a Promise
whereas onRequest
returns void
. This would explain the behavior noted above.
In fact, if the onRequest
implementation uses promise/then (no async/await) the same issue can be observed. i.e.
export const foo = functions.https.onRequest((req, resp) => {
delayedPromise().then(() => resp.send(200));
});
Also, according to https://firebase.google.com/docs/functions/unit-testing#testing_http_functions, "Testing of HTTP Callable Functions is not yet supported" so using a https.onCall triggered function is not a very useful comparison.
One approach is to call done()
in the Response.send()
mocked implementation. Alternatively, supertest can be used as per https://cloud.google.com/functions/docs/bestpractices/testing#integration_tests_2
Version info
firebase-functions-test: ^0.1.6
firebase-functions: ^3.0.2
firebase-admin: ^8.2.0
Test case
functions/test/index.test.ts:
functions/src/debug.ts:
Steps to reproduce
Run the tests. I'm using the
npm run test
script from my package.json:Expected behavior
The test should await until all awaits from the function are done (for all function trigger types).
Actual behavior
The test does not wait for the await call from the https.onRequest trigger. This is the output from running the tests.
For the https.onRequest triggered function, the
https: done await
appears after thetest: done await
(not expected). However, for the https.onCall triggered function, thehttps: done await
log appears before thetest: done await
(expected).