Mocha for TypeScript Testing: How to Get Started

Mocha is a popular JavaScript test framework that's often used for testing Node.js applications. It's a widely used testing tool…

By Erin,

Mocha is a popular JavaScript test framework that’s often used for testing Node.js applications. It’s a widely used testing tool for both JavaScript and TypeScript projects. However, using Mocha with TypeScript is a little different than JavaScript because TypeScript is a typed superset of JavaScript and, thus, requires type definitions for many of its features. In this post, we’ll show you how to get started using Mocha for TypeScript testing.

Prerequisites

To follow along with this tutorial, you should have the following:

  • a basic understanding of TypeScript
  • Node.js (version 14.0.0 or newer) and npm installed on your machine
  • a code editor of your choice (we’ll be using Visual Studio Code)

Create a TypeScript Project

To create a new project named mocha-ts-demo, type the following command in your terminal or command prompt:

This creates a new directory called mocha-ts-demo and takes you inside this newly created directory. Now, initialize this as a new npm project by running the following command:

This creates a package.json file with default values. Next, to initialize TypeScript, run the following command:

This creates a tsconfig.json file inside the mocha-ts-demo directory with the default TypeScript configuration.

Install Development Dependencies for Mocha

First, make sure that you have Mocha installed. You can do this by running the following command from the terminal or command prompt:

The above command installs the mocha npm package globally on your machine. After installation is completed successfully, you’ll see the following screen:

Installing Mocha
Installing the mocha npm package using terminal

And if Mocha is installed correctly, you should be able to run the following Mocha command from the terminal:

After running that command, you should see the following output:

This verifies that Mocha was installed successfully. Next, let’s create a new project.

The next step is to install some dependencies that we’ll be using in this project. For this post, we’ll be using the following two dependencies:

  1. Chai: It’s an assertion library used with Mocha. It provides various functions to compare the actual and expected output.
  2. Sinon: It provides functions to create spies, stubs, and mocks.

To install types associated with Chai, run the following command:

And, similarly, to install Sinon and its associated types, run the following command:

The following command installs the types associated with Mocha.

Next, install TypeScript by running the following command:

The above command installs TypeScript and ts-node as your development dependencies on your machine.

You can open the folder in Visual Studio Code or any other code editor of your choice.

Write Tests in TypeScript Using Mocha

In this section, we’ll see how to write Mocha tests to test a simple calculator function in TypeScript. The function takes two input parameters and returns the addition of those two numbers.

First, let’s create a new file named src/calculator.ts inside the mocha-ts-demo directory and add the following code to it.

Now that we have the calculator function ready, let’s write the Mocha test for it. Create a new file named src/calculator.spec.ts inside mocha-ts-demo and add the following code to it:

In the above code, we’ve imported assert from Chai and the additional function from calculator.ts. The assert module provides various functions to compare the actual and expected output. In our case, we’re using the equal function to compare the result of the addition function with the expected value of 5.

Your folder structure should look like this:


Now update the package.json file as below:

Execute the Tests Within an IDE

You can run the tests by executing the following command:

If the tests are successful, you should get the following output:

Mocha Run Test
Running Mocha tests in Visual Studio Code

Create Mocks Using Sinon

You can use the Sinon library to create mocks and stubs in your TypeScript tests. In this section, we’ll see how to use Sinon to create mocks for a simple function. First, let’s create a new file named src/user.ts inside the mocha-ts-demo directory and add the following code to it:


In the user.ts file, we have a class called User. The class has two properties: id and name. We’ve also defined a constructor for the class that takes two parameters: id and name. The file also contains a static method called findUser. The function takes an ID as a parameter and returns a promise. The promise resolves to an object that contains the user data fetched from back-end User Service REST API. The code for calling the REST API is purposefully omitted for brevity.

Test Asynchronous Code With Mocks

Now that we have the findUser(id: number) function ready, let’s write the Mocha test for it. Create a new file named /test/user-service.spec.ts inside the same mocha-ts-demo project folder and add the following code to it:


In the above code, we have imported assert object from chai and sinon object from the Sinon library. We’ve also imported the User class from the /src/user.ts file. Next we’ve created a test group called “User Service Tests” to group all test related to User Service. The it() function is used to define a test. In this case, we’re testing that the findUser function returns the correct user data when we pass it an ID of 1.

Then, the sinon.mock() function is used to create a mock for the User class. This means that we’re creating a fake User object that we can use for our tests. After that, we tell our mock what to do when its findUser function is called with an ID of 1. We tell it to return a new User object with an ID as 1 and name as John. This is done using expext().resolves(). Finally, we assert that the user ID is 1 using the assert.equal() function. If it is, then our test passes. Otherwise, it fails.

You can run your test once again using the following command (same as before):

After your test executes successfully, you should get the following output:

Mocha Run Test View 2
Executing Mocha tests containing a Sinon mock

Conclusion

Now that you’ve learned how to write and run tests for your TypeScript code using Mocha, we encourage you to start writing tests for all of your projects. By doing so, you’ll be able to catch bugs early on and ensure that your code is working as expected. In addition, using a testing framework like Mocha will help you make sure that your code remains testable and maintainable in the long run.

Testim's latest articles, right in your inbox.

From our latest feature releases, to the way it impacts the businesses of our clients, follow the evolution of our product

Blog Subscribe