How to Write Unit Tests for MongoDB Atlas Functions
Rate this tutorial
Today, I'll explain how you can write automated unit tests for Atlas Functions. Below is a summary of what we'll cover:
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.
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.
Let's take a look at how I architected this app, so we can understand how I tested it.
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.
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.htmlin their browser.
index.htmlcould be hosted anywhere. I chose to host
index.htmlusing . 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.htmlencodes the CSV file and passes it to the
processCSVfunction decodes the CSV file and passes the results to the
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 . 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.
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:
- Are typically faster to write than other automated tests.
- Can be executed quickly and independently as they do not rely on other integrations and systems.
- Reveal bugs early in the software development lifecycle when they are cheapest to fix.
- Give developers confidence we aren't introducing regressions as we update and refactor other parts of the code.
Every Atlas Function assigns a function to the global variable
exports. Below is the code for a boilerplate Function that returns
To workaround this problem, we can add the following three lines to the bottom of Function source files:
Let's break down what's happening here. If the type of the module is an
object, the function is being executed outside of an Atlas environment, so we need to assign our function (stored in
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 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
removeBreakingCharactersfunction. This function removes emoji and other breaking characters from the Twitter statistics. Below is the source code for the
To test this function, I created a new test file named
removeBreakingCharacters.test.js. I began by importing the
Then I was ready to begin testing. I began with the simplest case: a single valid Tweet.
SingleValidTweettest creates a constant named
csvis a combination of a valid header, a new line character, and a valid Tweet. Since the Tweet is valid,
removeBreakingCharactersshouldn't remove any characters. The test checks that when
csvis passed to the
removeBreakingCharactersfunction, the function returns a String equal to
Emojis were a big problem that were breaking my app, so I decided to create a test just for them.
EmojiTweettest creates two constants:
csvBeforestores a valid header, a new line character, and stats about a Tweet that contains three emoji.
csvAfterstores 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
csvBeforeconstant to the
removeBreakingCharactersfunction, the function returns a String equal to
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.
Let's take a look at how I tested the
storeCsvInDbfunction. Below is the source code for the function.
At a high level, the
storeCsvInDbfunction is doing the following:
- Calling the
removeBreakingCharactersfunction 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.
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
removeBreakingCharactersfunction, so I created a mock for that as well.
I added the following code to
Jest runs the function before each test in the given file. I chose to put the instantiation of the mocks inside of
beforeEachso that I could add checks for how many times a particular mock is called in a given test case. Putting mocks inside of
beforeEachcan 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.
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.
csvTweetsconsists of a valid header, a newline character, and a valid Tweet.
The test then calls the
storeCsvInDbfunction, passing the
csvTweetsconstant. 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
removeBreakingCharactersfunction was called with our
Finally, the test checks that the database's
updateOnefunction was called with the arguments we expect.
After I finished this unit test, I wrote an additional test that checks the
storeCsvInDbfunction correctly handles multiple Tweets.
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.
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: