Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

firebase-functions-test

Package Overview
Dependencies
Maintainers
2
Versions
28
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

firebase-functions-test

A testing companion to firebase-functions.

  • 2.1.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
160K
decreased by-14.07%
Maintainers
2
Weekly downloads
 
Created

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

Keywords

FAQs

Package last updated on 19 May 2022

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc