cts  ](/docs/projects/learn-more)
  * [ Supported platforms & frameworks  ](/docs/libraries)
  * [ Use Emulator Suite  ](/docs/emulator-suite)
  * [ AI assistance with Gemini in Firebase  ](/docs/gemini-in-firebase)

  * [ Emulator Suite  ](/docs/emulator-suite)
  * [ Authentication  ](/docs/auth)
  * [ Realtime Database  ](/docs/database)
  * [ Firestore  ](/docs/firestore)
  * [ Storage  ](/docs/storage)
  * [ ML  ](/docs/ml)
  * [ Hosting  ](/docs/hosting)
  * [ Cloud Functions  ](/docs/functions)
  * [ Security Rules  ](/docs/rules)
  * [ App Check  ](/docs/app-check)
  * [ Extensions  ](/docs/extensions)

  * [ Crashlytics  ](/docs/crashlytics)
  * [ Performance Monitoring  ](/docs/perf-mon)
  * [ Test Lab  ](/docs/test-lab)
  * [ App Distribution  ](/docs/app-distribution)

  * [ Analytics  ](/docs/analytics)
  * [ Remote Config  ](/docs/remote-config)
  * [ A/B Testing  ](/docs/ab-testing)
  * [ Cloud Messaging  ](/docs/cloud-messaging)
  * [ In-App Messaging  ](/docs/in-app-messaging)
  * [ Dynamic Links  ](/docs/dynamic-links)
  * [ Google AdMob  ](/docs/admob)
  * [ Google Ads  ](/docs/ads)
  * [ App Indexing  ](/docs/app-indexing)

Firebase is back at Google I/O on May 14! [ Register now.
](https://io.google/2024/?utm_source=firebase&utm_medium=embedded_marketing&utm_campaign=&utm_content=)

  * [ Firebase ](https://firebase.google.com/)
  * [ Documentation ](https://firebase.google.com/docs)
  * [ Cloud Functions ](https://firebase.google.com/docs/functions)
  * [ Build ](https://firebase.google.com/docs/build)

Send feedback

#  Unit testing of Cloud Functions

Stay organized with collections  Save and categorize content based on your
preferences.

This page describes best practices and tools for writing unit tests for your
functions, such as tests that would be a part of a Continuous Integration (CI)
system. To make testing easier, Firebase provides the Firebase Test SDK for
Cloud Functions. It is distributed on npm as ` firebase-functions-test ` , and
is a companion test SDK to ` firebase-functions ` . The Firebase Test SDK for
Cloud Functions:

  * Takes care of the appropriate setup and teardown for your tests, such as setting and unsetting environment variables needed by ` firebase-functions ` . 
  * Generates sample data and event context, so that you only have to specify the fields that are relevant to your test. 

**Note:** ` firebase-functions-test ` can only be used with functions written
with ` firebase-functions ` 2.0.0 or above.

##  Test setup

Install both ` firebase-functions-test ` and [ Mocha ](https://mochajs.org) ,
a testing framework, by running the following commands in your functions
folder:

    
    
    npm install --save-dev firebase-functions-test
    npm install --save-dev mocha
    

Next create a ` test ` folder inside the functions folder, create a new file
inside it for your test code, and name it something like ` index.test.js ` .

Finally, modify ` functions/package.json ` to add the following:

    
    
    "scripts": {
      "test": "mocha --reporter spec"
    }
    

Once you have written the tests, you can run them by running ` npm test `
inside your functions directory.

###  Initializing Firebase Test SDK for Cloud Functions

There are two ways to use ` firebase-functions-test ` :

  1. **Online mode (recommended):** Write tests that interact with a Firebase project dedicated to testing so that database writes, user creates, etc. would actually happen, and your test code can inspect the results. This also means that other Google SDKs used in your functions will work as well. 
  2. **Offline mode:** Write siloed and offline unit tests with no side effects. This means that any method calls that interact with a Firebase product (e.g. writing to the database or creating a user) need to be stubbed. Using offline mode is generally not recommended if you have Cloud Firestore or Realtime Database functions, since it greatly increases the complexity of your test code. 

####  Initi