What you’ll learn?

  • What are the criterias to consider and how to choose unit testing framework for your project.
  • How to write unit tests for cloud functions(with  google-firebase/firestore used as datastore) written in Node.js with JEST.
  • Sample code to be unit tested in offline mode.
  • Code coverage in JEST.
  • Debugging JEST unit test cases.


  1. Install Node.js & NPM (By default, installing Node.js installs NPM too)
  2. Install Visual Studio Code
  3. Project on Google Firebase to store data to be interacted with cloud functions.

Type of Cloud functions to test:

  • Cloud HTTP functions
  • Cloud Firestore Triggers
  • Cloud Pub/Sub Triggers

Most popular JavaScript unit testing frameworks available:

Criteria to choose best testing framework:

  • Support for full end to end testing.
  • Support for mocking abilities and other test tools.
  • Snapshot testing.
  • Compatibility with other javascript languages(like angular, nodeJs, React..).
  • Performance.
  • Configuration.
  • Documentation.
  • Popularity.

In short,

  • If you want to “just get started” or are looking for a fast framework for large
    projects, you can’t go wrong with Jest.
  • If you want a very flexible and extendable configuration, go with Mocha.
  • If you are looking for simplicity go with Ava.
  • If you want to be really low-level, go with tape.

JEST – Testing Framework:

Jest is a JavaScript test runner, that is, a JavaScript library for creating, running, and structuring tests.

Jest is a universal testing platform, with the ability to adapt to any JavaScript library or framework. Used and recommended by Facebook alongside a variety of React applications, Jest is well supported. Jest also reports a very fast testing library due to its clever parallel testing.

For smaller projects you might not worry about this too much initially, having increased performance is great for larger projects wanting to continuously deploy their app throughout the day.

  • While developers primarily use Jest to test React applications, Jest can easily integrate into other applications allowing you to use it’s more unique features elsewhere.
  • Snapshot testing is a great tool to ensure that your application’s UI doesn’t unexpectedly change between releases. Although more specifically designed and used in React, it does work with other frameworks if you can find the correct plugins.
  • Unlike other libraries on the list, Jest comes with a wide API, not requiring you to include additional libraries unless you really need to.
  • Jest continues to improve considerably with every update they make.

So I chose JEST to write my unit test cases as it was fulfilling my requirements.

Below is my current project structure in Visual Studio Code. Here, I have created a folder called ‘test’ parallel to ‘src’ folder and created test files as shown in the below screenshot.

Test Setup:

  1. npm install –global jest
  2. npm install –save-dev ts-jest
  3. npm install –save-dev @types/jest
  4. Update ‘package.json’ mentioning regex to accept test files and acceptable moduleFileExtensions, something like below :

to run tests in background run

Let’s get started with coding :
Firebase Cloud Functions can run in Online and Offline modes.

  • Online mode means it will interact with your Firebase account, create/destroy data.
  • Offline mode will result in stubbing our calls, and this is the preferred option in my opinion for this writing.

Importing your functions and mocking the dependencies:

To import your functions, use ‘import’ to import your main functions file as a module.

If  you have admin.initializeApp() in your functions code, then you need to stub it before importing your functions:

If you have initialized admin.firestore() in the file to be unit tested, let’s say I want to mock an update call on a document.


Lets consider this code to unit test in offline mode :

  • Here, we are testing utility which checks the firestore document if exists or not on the passed document locationas parameter. Store name is being passed as parameter and ‘firestoreHelper’ gets the document based on that and the function will ultimately return merchant name from the document.

We will be testing a utility function mentioned above in ‘Code’ section with Jest, merchantUtils.test.ts


  1. We are testing utility which checks the document if exists or not on the passed (as parameter) document location. Store name is being passed as parameter and ‘firestoreHelper’ gets the document based on that and the function will ultimately return merchant name from the document.
  2. jest-when‘ library is used to get flexibility in mocking dependencies based on different use-cases.
  3. Mocking the  ‘firestoreHelper’ gets the document based on store name passed.
  4. Unit testing the both cases where merchant document actually exists for store name passed and where it is not.
  5. This is how we can test asynchronous functions.
    For asynchronous tests (assuming node 8 or above) you can simply add the async and await keywords

Code coverage in JEST:

Run the test with coverage:

Here’s what you get:

If you want to keep code coverage always active configure Jest in package.json like so:

You can also pass the flag to the test script:

If you’re a visual person there’s also a way to have an HTML report for code coverage, it’s simply as configuring Jest like so:

Now every time you run npm test you can access a new folder called coverage in your project folder.

Inside this folder you’ll find a bunch of files, with /coverage/index.html as a complete HTML summary of the coverage for your code:

If you click on the function name you’ll also see the exact untested line of code if any.

Debugging JEST unit test cases:

If you are developing is Visual Studio Code, the Jest extension allows you to set breakpoints to debug your tests, which can be very helpful sometimes.

Install the extension, then in the vscode debug view, choose Add configuration and select Jest: Default jest configuration. If your functions code is in the functions folder, amend the program and cwd paths as below:


So this is a short guide to use JEST for unit testing cloud functions, hope it helped you.
Thanks for reading till here and feel free to ask questions if any in the below comment section.