Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
firebase-functions-test
Advanced tools
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.
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);
});
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 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 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.
The firebase-functions-test
is unit testing library for Cloud Functions for Firebase. It is a companion to firebase-functions.
NOTE: This library can only be used with firebase-functions
v3.20.1 or above.
wrap
your Firebase Functions. You can invoke the Firebase Function's handler by invoking the wrap
call.Eg.
import {myFirebaseFunction} from "../index"; // Your Firebase Functions
import firebaseFunctionsTest from "firebase-functions-test";
// Extracting `wrap` out of the lazy-loaded features
const {wrap} = firebaseFunctionsTest();
// `jest-ts` example
test('my unit test', () => {
const wrappedFirebaseFunction = wrap(myFirebaseFunction);
// Invoke the firebase function
wrappedFirebaseFunction();
// Invoke the firebase function with CloudEvent overrides
wrappedFirebaseFunction({data: {arbitrary: 'values'}});
});
Learn more about unit testing Cloud Functions here.
To contribute a change, check out the contributing guide.
© Google, 2018. Licensed under The MIT License.
FAQs
A testing companion to firebase-functions.
The npm package firebase-functions-test receives a total of 136,261 weekly downloads. As such, firebase-functions-test popularity was classified as popular.
We found that firebase-functions-test demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.