Testing Node.js,Express.js API with Mocha and Chai – Typescript

April 30, 2019
Contents
>What is Mocha >what is Chai >what is Chai HTTP >Installing dependencies and getting started >Writing tests in the test files

What is Mocha

Mocha

Mocha is a Javascript testing framework working on both Node.js and in the browser. We can use mocha to test APIs written to run in Node.js, created using frameworks like Express. Mocha runs the test cases serially and provides very accurate reporting. Mocha makes testing asynchronous testing easy and efficient.

The latest version of Mocha available to use is 6.1.4. To install Mocha to be used with Node.js, use

Mocha is used in the development and testing phases and ideally not used in a production environment. So you use –save-dev while installing Mocha.

What is Chai

Chai is an assertion library used with testing frameworks like Mocha. An assertion in testing is an expression that will be true at a specific state of the application if there is no bug in the program. While testing Node.JS APIs, we can use this assertion library to check whether the response received from an API endpoint for a specific request is the expected one or not.

To the latest version of Chai available to use is 4.2.0. To install Chai to be used with Node.js use

what is Chai HTTP

Chai-Http is an addon for assertion library Chai which helps in testing Http Requests. This addon is helpful in testing APIs as it helps to test different types of requests like get, post, etc.

The latest version of Chai available to use is 4.3.0. To install chai-http to be used with Node.js use

Installing dependencies and getting started

The dependencies required to do testing with Mocha and Chai on an Express API are as follows. Copy the following lines to devDependencies in package.json and do

Note that the @type modules are for typings. Since what we use is external Javascript modules the typings files are required to describe the types of that code to use them with typescript. These type that we require are already written and are available using @types/

Now we create a folder called test. We create our test files in this folder

 

See the sample folder structure here. The lib folder contains API files and test folder contains test files.

Our Express.JS API have a route like

which will have a request body like

and will have a response like

Since the controller name testController, we will create a test file in the test folder like testController.test.ts

As we write our test files in typescript, we use ts-node to run our tests. Write the following line in the scripts part of package.json

This will run all the test files in the test folder.

Writing tests in the test files

in testController.test.ts file write code like below

In the earlier section

Installing dependencies and getting started

we have already seen how the request and response objects will be like. We send the same request object using send

and expect the same response object.

You can create folder “fixtures”  in the test folder and keep the request and response data in them in controllername.fixtures.ts files. Import the fixture file in the test files and use them. This will make the code look much cleaner.

On running the tests using

we get a result like the following

On successful running

 

If there is a bug it will look like this

No Comments

Leave a Reply

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