API Testing with Mocha

featured image for api testing using mocha

It took some time for me to understand how to set up tests for an API using Mocha. Finding API testing documentation about Mocha and an ORM framework working together to test APIs was a challenge. So, I want to detail what I learned setting up these tests.


An Application Programming Interface is an intermediary that allows apps (applications) to communicate. For example, your app can request data from the Twitter platform via Twitter’s API. Also, you can create your own API so others can consume some of your apps’ data. This is one of the reasons why testing is essential, so the data managed via your API is reliable.


It is a javascript testing framework that runs on NodeJs and browsers. We will use Mocha as a tool to test our API.


Object-Relational Mapping is a programming technique that converts incompatible data. Using an object-oriented programming language, the method creates “a virtual database” used within the language. These ORMs also provide the tools for database migrations where data is transferred from one database to another or even multiple databases.

How do an API, Mocha, and an ORM interact during testing?

Testing an API has a few critical steps:

  1. Build a testing database
  2. API tests

The ORM handles models for the tests database. By modeling the migration files similar to development models, we can create a database similar to development and use it for testing. The ORM’s seeder files fill the new database with the default testing data.

Test scripts, handled by Mocha, run after creating the test database. Mocha’s testing begins by calling the before() function before API testing. The before() function sets the test-specific initial states before testing the API.

After defining the initial values, Mocha tests the API. When the API testing is completed, Mocha calls the after() function to set the final state of the database after testing. This function is where usually the database is deleted or cleaned up. You can follow this link for more information about Mocha’s testing cycle.

API Testing

Let’s establish some assumptions for this exercise:

  1. Users can update data only when logged in.

Build a testing database

  1. Following your project’s ORM documentation, generate a new migration file for a model. Let’s say a User model.
  1. Modify the migration file as the User model in the development’s database.
  1. Run migration to generate the testing database based on the User model.
  1. Confirm that the User data appears in the testing database.
  1. Create and modify the ORM’s seeder files with the User model’s default values.
  1. Run seeders to add the User model’s default values to the testing database.

Repeat the above steps to finish building the testing database based on the models from the development database. It will ensure that the results reflect the current state of the development database. The API is ready for tests once the testing database is built.

API Tests

We already described the testing cycle followed by Mocha. It begins with the before() function that sets the test’s initial state. If we require a user to be logged in to update information, then before() is where you would create a user and log them before testing the API for data update. We can create a new user using the ORM and log them before the API tests.

Mocha provides methods for running tests asynchronously and synchronously. Test cases must include API requests to the server instead of the ORM. So, if a test case is evaluating updates in User data, the test should consist of an API request to create a user followed by an API request to update that newly created user.

After finishing API testing, It is expected that there should be a clean database. This expectation was something I was not aware of when learning about testing. So imagine how confused I was when reading about the after() function! You can define Mocha’s after() function to clear the database using the ORMs’ delete / destroy method.

Thanks for reading!

You can get a new post notification directly to your email by signing up at the following link.

Related Articles

The following CTRL-Y articles are related somewhat to this post. You may want to check them out!:

By the Way – A playlist for you

A friend shared this Mira Kater’s 25 Anniversary set, and I was instantly hooked! This has been my go-to music set for the last couple of months when I need to hyper-focus on several tasks. It’s mainly a chill, deep house that keeps you moving steadily.

Leave a Reply

Your email address will not be published. Required fields are marked *