Explore Developer Center's New Chatbot! MongoDB AI Chatbot can be accessed at the top of your navigation to answer all your MongoDB questions.

MongoDB Developer
Atlas
plus
Sign in to follow topics
MongoDB Developer Centerchevron-right
Developer Topicschevron-right
Productschevron-right
Atlaschevron-right

How to Write Unit Tests for MongoDB Atlas Functions

Lauren Schaefer10 min read • Published Jan 28, 2022 • Updated Sep 09, 2024
ServerlessAtlasJavaScript
Facebook Icontwitter iconlinkedin icon
Rate this tutorial
star-empty
star-empty
star-empty
star-empty
star-empty
Some features mentioned below will be deprecated on Sep. 30, 2025. Learn more.
I recently built a web app for my team using Atlas Functions. I wanted to be able to iterate quickly and frequently deploy my changes. To do so, I needed to implement DevOps infrastructure that included a strong foundation of test automation. Unfortunately, I didn't know how to do any of that for apps built using Atlas Functions.
In this series, I'll walk you through what I discovered. I'll share how you can build a suite of automated tests and a CI/CD pipeline for web applications that are built on serverless functions.
Today, I'll explain how you can write automated unit tests for Atlas Functions. Below is a summary of what we'll cover:
Prefer to learn by video? Many of the concepts I cover in this series are available in this video.

About the Social Stats App

Before I jump into how I tested my app, I want to give you a little background on what the app does and how it's built.
My teammates and I needed a way to track our Twitter statistics together.
Twitter provides a way for their users to download Twitter statistics. The download is a comma-separated value (CSV) file that contains a row of statistics for each Tweet. If you want to try it out, navigate to https://analytics.twitter.com/user/insert_your_username_here/tweets and choose to export your data by Tweet.
Once my teammates and I downloaded our Tweet statistics, we needed a way to regularly combine our stats without duplicating data from previous CSV files. So I decided to build a web app.
The app is really light, and, to be completely honest, really ugly. The app currently consists of two pages.
The first page allows anyone on our team to upload their Twitter statistics CSV file.
The second page is a dashboard where we can slice and dice our data. Anyone on our team can access the dashboard to pull individual stats or grab combined stats. The dashboard is handy for both my teammates and our management chain.

App Architecture

Let's take a look at how I architected this app, so we can understand how I tested it.

Serverless Architecture and Atlas

The app is built using a serverless architecture. The term "serverless" can be a bit misleading. Serverless doesn't mean the app uses no servers. Serverless means that developers don't have to manage the servers themselves. (That's a major win in my book!)
When you use a serverless architecture, you write the code for a function. The cloud provider handles executing the function on its own servers whenever the function needs to be run.
Serverless architectures have big advantages over traditional, monolithic applications:
  • Focus on what matters. Developers don't have to worry about servers, containers, or infrastructure. Instead, we get to focus on the application code, which could lead to reduced development time and/or more innovation.
  • Pay only for what you use. In serverless architectures, you typically pay for the compute power you use and the data you're transferring. You don't typically pay for the servers when they are sitting idle. This can result in big cost savings.
  • Scale easily. The cloud provider handles scaling your functions. If your app goes viral, the development and operations teams don't need to stress.
I've never been a fan of managing infrastructure, so I decided to build the Social Stats app using a serverless architecture.
MongoDB Atlas offers several serverless cloud services – including Atlas Data API, Atlas GraphQL API, and Atlas Triggers – that make building serverless apps easy.

Social Stats Architecture

Let's take a look at how the Social Stats app is architected. Below is a flow diagram of how the pieces of the app work together.
When a user wants to upload their Twitter statistics CSV file, they navigate to index.html in their browser. index.html could be hosted anywhere. I chose to host index.html using Static Hosting. I like the simplicity of keeping my hosted files and serverless functions in one project that is hosted on one platform.
When a user chooses to upload their Twitter statistics CSV file,index.html encodes the CSV file and passes it to the processCSV Atlas Function.
The processCSV function decodes the CSV file and passes the results to the storeCsvInDb Atlas Function.
The storeCsvInDb function calls the removeBreakingCharacters Atlas Function that removes any emoji or other breaking characters from the data. Then the storeCsvInDb function converts the cleaned data to JSON (JavaScript Object Notation) documents and stores those documents in a MongoDB database hosted by Atlas.
The results of storing the data in the database are passed up the function chain.
The dashboard that displays the charts with the Twitter statistics is hosted by MongoDB Charts. The great thing about this dashboard is that I didn't have to do any programming to create it. I granted Charts access to my database, and then I was able to use the Charts UI to create charts with customizable filters.
(Sidenote: Linking to a full Charts dashboard worked fine for my app, but I know that isn't always ideal. Charts also allows you to embed individual charts in your app through an iframe or SDK.)

Unit Testing Atlas Functions

Now that I've explained what I had to test, let's explore how I tested it. Today, we'll talk about the tests that form the base of the testing pyramid:unit tests.
Unit tests are designed to test the small units of your application. In this case, the units we want to test are serverless functions. Unit tests should have a clear input and output. They should not test how the units interact with each other.
Unit tests are valuable because they:
  1. Are typically faster to write than other automated tests.
  2. Can be executed quickly and independently as they do not rely on other integrations and systems.
  3. Reveal bugs early in the software development lifecycle when they are cheapest to fix.
  4. Give developers confidence we aren't introducing regressions as we update and refactor other parts of the code.
Many JavaScript testing frameworks exist. I chose to use Jest for building my unit tests as it's a popular choice in the JavaScript community. The examples below use Jest, but you can apply the principles described in the examples below to any testing framework.

Modifying Atlas Functions to be Testable

Every Atlas Function assigns a function to the global variable exports. Below is the code for a boilerplate Function that returns "Hello, world!"
1exports = function() {
2 return "Hello, world!";
3};
This function format is problematic for unit testing: calling this function from another JavaScript file is impossible.
To workaround this problem, we can add the following three lines to the bottom of Function source files:
1if (typeof module === 'object') {
2 module.exports = exports;
3}
Let's break down what's happening here. If the type of the module is anobject, the function is being executed outside of an Atlas environment, so we need to assign our function (stored in exports) to module.exports. If the type of the module is not an object, we can safely assume the function is being executed in a Atlas environment, so we don't need to do anything special.
Once we've added these three lines to our serverless functions, we are ready to start writing unit tests.

Unit Testing Self-Contained Functions

Unit testing functions is easiest when the functions are self-contained, meaning that the functions don't call any other functions or utilize any services like a database. So let's start there.
Let's begin by testing the removeBreakingCharacters function. This function removes emoji and other breaking characters from the Twitter statistics. Below is the source code for the removeBreakingCharacters function.
1exports = function (csvTweets) {
2 csvTweets = csvTweets.replace(/[^a-zA-Z0-9\, "\/\\\n\`~!@#$%^&*()\-_—+=[\]{}|:;\'"<>,.?/']/g, '');
3 return csvTweets;
4};
5
6if (typeof module === 'object') {
7 module.exports = exports;
8}
To test this function, I created a new test file named removeBreakingCharacters.test.js. I began by importing theremoveBreakingCharacters function.
1const removeBreakingCharacters = require('../../../functions/removeBreakingCharacters/source.js');
Next I imported several constants from constants.js. Each constant represents a row of data in a Twitter statistics CSV file.
1const { header, validTweetCsv, emojiTweetCsv, emojiTweetCsvClean, specialCharactersTweetCsv } = require('../../constants.js');
Then I was ready to begin testing. I began with the simplest case: a single valid Tweet.
1test('SingleValidTweet', () => {
2 const csv = header + "\n" + validTweetCsv;
3 expect(removeBreakingCharacters(csv)).toBe(csv);
4})
The SingleValidTweet test creates a constant named csv. csv is a combination of a valid header, a new line character, and a valid Tweet. Since the Tweet is valid, removeBreakingCharacters shouldn't remove any characters. The test checks that when csv is passed to the removeBreakingCharacters function, the function returns a String equal to csv.
Emojis were a big problem that were breaking my app, so I decided to create a test just for them.
1test('EmojiTweet', () => {
2 const csvBefore = header + "\n" + emojiTweetCsv;
3 const csvAfter = header + "\n" + emojiTweetCsvClean;
4 expect(removeBreakingCharacters(csvBefore)).toBe(csvAfter);
5})
The EmojiTweet test creates two constants:
  • csvBefore stores a valid header, a new line character, and stats about a Tweet that contains three emoji.
  • csvAfter stores the same valid header, a new line character, and stats about the same Tweet except the three emojis have been removed.
The test then checks that when I pass the csvBefore constant to the removeBreakingCharacters function, the function returns a String equal to csvAfter.
I created other unit tests for the removeBreakingCharacters function. You can find the complete set of unit tests in removeBreakingCharacters.test.js.

Unit Testing Functions Using Mocks

Unfortunately, unit testing most serverless functions will not be as straightforward as the example above. Serverless functions tend to rely on other functions and services.
The goal of unit testing is to test individual units—not how the units interact with each other.
When a function relies on another function or service, we can simulate the function or service with a mock object. Mock objects allow developers to "mock" what a function or service is doing. The mocks allows us to test individual units.
Let's take a look at how I tested the storeCsvInDb function. Below is the source code for the function.
1exports = async function (csvTweets) {
2 const CSV = require("comma-separated-values");
3
4 csvTweets = context.functions.execute("removeBreakingCharacters", csvTweets);
5
6 // Convert the CSV Tweets to JSON Tweets
7 jsonTweets = new CSV(csvTweets, { header: true }).parse();
8
9 // Prepare the results object that we will return
10 var results = {
11 newTweets: [],
12 updatedTweets: [],
13 tweetsNotInsertedOrUpdated: []
14 }
15
16 // Clean each Tweet and store it in the DB
17 jsonTweets.forEach(async (tweet) => {
18
19 // The Tweet ID from the CSV is being rounded, so we'll manually pull it out of the Tweet link instead
20 delete tweet["Tweet id"];
21
22 // Pull the author and Tweet id out of the Tweet permalink
23 const link = tweet["Tweet permalink"];
24 const pattern = /https?:\/\/twitter.com\/([^\/]+)\/status\/(.*)/i;
25 const regexResults = pattern.exec(link);
26 tweet.author = regexResults[1];
27 tweet._id = regexResults[2]
28
29 // Generate a date from the time string
30 tweet.date = new Date(tweet.time.substring(0, 10));
31
32 // Upsert the Tweet, so we can update stats for existing Tweets
33 const result = await context.services.get("mongodb-atlas").db("TwitterStats").collection("stats").updateOne(
34 { _id: tweet._id },
35 { $set: tweet },
36 { upsert: true });
37
38 if (result.upsertedId) {
39 results.newTweets.push(tweet._id);
40 } else if (result.modifiedCount > 0) {
41 results.updatedTweets.push(tweet._id);
42 } else {
43 results.tweetsNotInsertedOrUpdated.push(tweet._id);
44 }
45 });
46 return results;
47};
48
49if (typeof module === 'object') {
50 module.exports = exports;
51}
At a high level, the storeCsvInDb function is doing the following:
  • Calling the removeBreakingCharacters function to remove breaking characters.
  • Converting the Tweets in the CSV to JSON documents.
  • Looping through the JSON documents to clean and store each one in the database.
  • Returning an object that contains a list of Tweets that were inserted, updated, or unable to be inserted or updated.
To unit test this function, I created a new file named storeCsvInDB.test.js. The top of the file is very similar to the top of removeBreakingCharacters.test.js: I imported the function I wanted to test and imported constants.
1const storeCsvInDb = require('../../../functions/storeCsvInDb/source.js');
2
3const { header, validTweetCsv, validTweetJson, validTweetId, validTweet2Csv, validTweet2Id, validTweet2Json, validTweetKenId, validTweetKenCsv, validTweetKenJson } = require('../../constants.js');
Then I began creating mocks. The function interacts with the database, so I knew I needed to create mocks to support those interactions. The function also calls the removeBreakingCharacters function, so I created a mock for that as well.
I added the following code to storeCsvInDB.test.js.
1let updateOne;
2
3beforeEach(() => {
4 // Mock functions to support context.services.get().db().collection().updateOne()
5 updateOne = jest.fn(() => {
6 return result = {
7 upsertedId: validTweetId
8 }
9 });
10
11 const collection = jest.fn().mockReturnValue({ updateOne });
12 const db = jest.fn().mockReturnValue({ collection });
13 const get = jest.fn().mockReturnValue({ db });
14
15 collection.updateOne = updateOne;
16 db.collection = collection;
17 get.db = db;
18
19 // Mock the removeBreakingCharacters function to return whatever is passed to it
20 // Setup global.context.services
21 global.context = {
22 functions: {
23 execute: jest.fn((functionName, csvTweets) => { return csvTweets; })
24 },
25 services: {
26 get
27 }
28 }
29});
Jest runs the beforeEach function before each test in the given file. I chose to put the instantiation of the mocks inside of beforeEach so that I could add checks for how many times a particular mock is called in a given test case. Putting mocks inside of beforeEach can also be handy when we want to change what the mock returns the first time it is called versus the second.
Once I had created my mocks, I was ready to begin testing. I created a test for the simplest case: a single tweet.
1test('Single tweet', async () => {
2
3 const csvTweets = header + "\n" + validTweetCsv;
4
5 expect(await storeCsvInDb(csvTweets)).toStrictEqual({
6 newTweets: [validTweetId],
7 tweetsNotInsertedOrUpdated: [],
8 updatedTweets: []
9 });
10
11 expect(context.functions.execute).toHaveBeenCalledWith("removeBreakingCharacters", csvTweets);
12 expect(context.services.get.db.collection.updateOne).toHaveBeenCalledWith(
13 { _id: validTweetId },
14 {
15 $set: validTweetJson
16 },
17 { upsert: true });
18})
Let's walk through what this test is doing.
Just as we saw in earlier tests in this post, I began by creating a constant to represent the CSV Tweets. csvTweets consists of a valid header, a newline character, and a valid Tweet.
The test then calls the storeCsvInDb function, passing the csvTweets constant. The test asserts that the function returns an object that shows that the Tweet we passed was successfully stored in the database.
Next, the test checks that the mock of the removeBreakingCharacters function was called with our csvTweets constant.
Finally, the test checks that the database's updateOne function was called with the arguments we expect.
After I finished this unit test, I wrote an additional test that checks the storeCsvInDb function correctly handles multiple Tweets.
You can find the complete set of unit tests in storeCsvInDB.test.js.

Wrapping Up

Unit tests can be incredibly valuable. They are one of the best ways to find bugs early in the software development lifecycle. They also lay a strong foundation for CI/CD.
Keep in mind the following two tips as you write unit tests for Atlas Functions:
  • Modify the module exports in the source file of each Function, so you will be able to call the Functions from your test files.
  • Use mocks to simulate interactions with other functions, databases, and other services.
The Social Stats application source code and associated test files are available in a GitHub repo: https://github.com/mongodb-developer/SocialStats. The repo's readme has detailed instructions on how to execute the test files.
Be on the lookout for the next post in this series where I'll walk you through how to write integration tests for serverless apps.
Check out the following resources for more information:

Facebook Icontwitter iconlinkedin icon
Rate this tutorial
star-empty
star-empty
star-empty
star-empty
star-empty
Related
Article

Capturing and Storing Real-World Optics With MongoDB Atlas, OpenAI GPT-4o, and PyMongo


Sep 04, 2024 | 7 min read
Article

Querying the MongoDB Atlas Price Book with Atlas Data Federation


Jun 15, 2023 | 4 min read
Tutorial

How to Deploy Vector Search, Atlas Search, and Search Nodes With the Atlas Kubernetes Operator


Dec 20, 2024 | 10 min read
Tutorial

Building a Real-Time, Dynamic Seller Dashboard on MongoDB


Aug 05, 2024 | 7 min read
Table of Contents