What is firebase-functions-test?
The firebase-functions-test npm package is a testing utility for Firebase Cloud Functions. It allows developers to write unit tests for their Firebase functions, simulate function triggers, and mock Firebase services.
What are firebase-functions-test's main functionalities?
Unit Testing
This feature allows you to write unit tests for your Firebase functions. The code sample demonstrates how to mock Firestore and test a function using the firebase-functions-test package.
const firebaseFunctionsTest = require('firebase-functions-test')();
const myFunctions = require('../index');
// Mocking Firestore
const admin = require('firebase-admin');
const sinon = require('sinon');
sinon.stub(admin, 'firestore').get(() => {
return {
collection: () => {
return {
doc: () => {
return {
get: () => Promise.resolve({ data: () => ({ field: 'value' }) })
};
}
};
}
};
});
// Testing a function
const wrapped = firebaseFunctionsTest.wrap(myFunctions.myFunction);
wrapped({ data: 'test' }).then((result) => {
console.log(result);
});
Simulating Function Triggers
This feature allows you to simulate different types of function triggers, such as HTTP requests. The code sample shows how to simulate an HTTP request to test an HTTP-triggered function.
const firebaseFunctionsTest = require('firebase-functions-test')();
const myFunctions = require('../index');
// Simulating an HTTP request
const wrapped = firebaseFunctionsTest.wrap(myFunctions.myHttpFunction);
const req = { query: { text: 'input' } };
const res = { send: (message) => console.log(message) };
wrapped(req, res);
Mocking Firebase Services
This feature allows you to mock Firebase services like Firestore, Realtime Database, etc. The code sample demonstrates how to mock Firestore and test a Firestore-triggered function.
const firebaseFunctionsTest = require('firebase-functions-test')();
const myFunctions = require('../index');
// Mocking Firestore
const admin = require('firebase-admin');
const sinon = require('sinon');
sinon.stub(admin, 'firestore').get(() => {
return {
collection: () => {
return {
doc: () => {
return {
get: () => Promise.resolve({ data: () => ({ field: 'value' }) })
};
}
};
}
};
});
// Testing a Firestore-triggered function
const wrapped = firebaseFunctionsTest.wrap(myFunctions.firestoreTrigger);
wrapped({
before: { data: () => ({ field: 'oldValue' }) },
after: { data: () => ({ field: 'newValue' }) }
}).then((result) => {
console.log(result);
});
Other packages similar to firebase-functions-test
mocha
Mocha is a feature-rich JavaScript test framework running on Node.js and in the browser, making asynchronous testing simple and fun. While it is not specific to Firebase, it can be used in conjunction with other libraries to test Firebase functions.
jest
Jest is a delightful JavaScript Testing Framework with a focus on simplicity. It works with projects using Babel, TypeScript, Node.js, React, Angular, Vue.js, and Svelte. Jest can be used to test Firebase functions but requires additional setup for mocking Firebase services.
sinon
Sinon is a standalone test spies, stubs, and mocks for JavaScript. It works with any unit testing framework. While Sinon is not specific to Firebase, it is often used to mock Firebase services in tests.