Open ulisseshen opened 1 year ago
@ulisseshen
I don't believe wrap
is supported for onRequest
methods for firebase functions. So all helloWorld
is in your test is a function you can trigger. Using TS you can "make it work" using whatever test framework you choose.
In this example I'm using vitest
a fast test runner leveraging vite.
import { helloWorld } from '../src/index';
import * as firebaseFunctionsTest from 'firebase-functions-test';
import { describe, expect, it, vi, afterEach } from 'vitest';
import { onRequest } from 'firebase-functions/v2/https';
// So you aren't cluttering your console during tests I will
// also recommend mocking out the logger method(s)
vi.mock('firebase-functions/logger');
type FbRequestFunction = Parameters<typeof onRequest>[0];
type FbRequest = Parameters<FbRequestFunction>[0];
type FbResponse = Parameters<FbRequestFunction>[1];
const testEnv = firebaseFunctionsTest();
afterEach(() => {
// Depois de escrever seus testes, lembre-se de limpar o ambiente de teste
testEnv.cleanup();
});
describe('helloWorld', () => {
it('Deve retornar "Hello from Firebase!"', async () => {
const mockedSendFunction = vi.fn();
// Cast here as Firebase doesn't share any "mock response"
const fakeResponse = {
// Here within the response we must
// mock out any `response` methods that are
// called within the test itself such as
// - status
// - sendStatus
// - send
// - json
// - contentType
// - type
// - redirect
send: mockedSendFunction,
} as unknown as FbResponse;
// Cast here as Firebase doesn't share any "mock request"
const fakeRequest = {
// Here we can add anything that we would trigger on
// the `request` such as
// - headers
// - method
// - url
// - statusCode
// - statusMessage
} as unknown as FbRequest;
// Now we can trigger the function
// Chame a função simulada usando o contexto simulado
await helloWorld(fakeRequest, fakeResponse);
// Note there is no return value of this method, because it exists
// on the methods we mocked on the `fakeResponse` above.
console.log(mockedSendFunction);
// I don't know the specifics of chai, however using something
// like vitest or jest you can look at the mocks.
console.log(mockedSendFunction.mock.lastCall);
// Verifique se o resultado está correto
// then with vitest and jest based testing you can assert
// against the mock
expect(mockedSendFunction).toBeCalledTimes(1);
expect(mockedSendFunction).toBeCalledWith('Hello from Firebase!');
// Or if you are expecting an object you can use something like
// expect(mockedSendFunction).toBeCalledWith(
// expect.objectContaining({
// message: 'Hello from Firebase!',
// })
// );
});
});
If you have any additional questions please feel free to let me know. I am not a maintainer of firebase, just ran into this same problem. Sadly it seems their docs and example are out of date and a lot of the links on their website point to repositories that no longer exist on github or have been moved. EX: https://firebase.google.com/docs/functions/unit-testing#testing_http_functions -> addMessage() example function
link is broken.
Version info
firebase-functions-test:
firebase-functions: "firebase-functions": "^4.4.1",
firebase-admin: "firebase-admin": "^11.8.0",
Test case
Steps to reproduce
This line
const wrapped = testEnv.wrap(helloWorld);
in the ´index.test.ts´ file give me a error that I not allow me continue with my tests.Expected behavior
Let me run my test with http onResquest cloud functions (v2?)
Actual behavior
It doesn't allow me make tests on the http onResquest cloud functions