Beginners Guide: Test Driven Development (TDD) with Mocha and Chai (Using Node Js)

Beginners Guide Test Driven Development (TDD) with Mocha and Chai (Using Node Js)

Welcome to another excellent blog by our DEV IT engineers. Today, we’ll talk about the process of conducting Test Driven Development with Mocha and Chai in Node JS. Let’s get started.

What is Test Driven Development (TDD)?

It is a process where you write your unit test first, watch it fail, and then implement the necessary code to the module you’re creating until the test passes. That’s it!

How should it be done?

  • Unit tests should be specific and should test particular components and algorithms of the code you wish to test
  • The data used in the testing should be static or variable and not shouldn’t be subject to change from external API calls. For example, a specific output should be expected

After writing your unit tests and implementing the code for them, if they all pass, you’re done!

Tests are one of the main steps of the Software Development Life Cycle (SDLC) before the end product can be deployed and used. Different kinds of testing are done on the product to ensure quality and confidence, like Unit Testing. There are popular frameworks that can test scripts, maintain test suites, and populate test results.

What are Mocha and Chai?

Mocha is one of the most popular & widely used JavaScript test frameworks that can run on Node.js and provide accurate test reports and stack traces for uncaught exceptions. As an alternative, Chai is an assertion library that can be used in conjunction with any JavaScript testing framework.

Chai is an assertion library. The Chai toolkit provides the Should Expect and Assert interfaces, a readable and expressive way to create tests following BDD.

Assert: –

As the last parameter, you can provide an additional message that will appear as an error message.

Expect: –

With Expect, Chai exposes the BDD style. As a result, users can chain together natural language assertions.

Let’s look at how it works by setting up one simple test in a node application

1. Let’s start by creating a node project with “npm init.” Then, let’s install the dependencies for mocha and chai using the “npm install mocha chai” command.

2. Now in package.json you need to add “mocha” in the test key of the script object and create a folder named tests to manage your tests separately, so you can create all the test files you need to perform your tests here.

3. Mocha uses hooks to organize its structure. Now, we have listed some main functions.

describe(): It’s used to group, which you can nest as deep;

it(): It’s the test case;

before(): It’s a hook to run before the first it() or describe();

beforeEach(): It’s a hook to run before each it() or describe();

after(): It’s a hook to run after it() or describe();

afterEach(): It’s a hook to run after each it() or describe();

A describe() is a function that contains a collection of tests, or you can also call it a suite of tests. It’s got two parameters, the first is a meaningful name, which describes what the method does, and the second is the description function, which basically contains one or more tests.

It() represents a function that holds the content and steps of the actual test. It also has two parameters: the name of the test and the function that contains the content.

4. Create a file for test additions in the test folder, say test.js, with the name you like. Place the following code in the file.

5. With Math Operation, you can group all child Describe functions that perform test case operations. Run it from the terminal using the ” npm run test” command which runs all your tests.

6. If you place 1+2 instead of 1+1 in expect, it will result in 3 so it won’t be equal to 3, so it will fail the test. Try to place 1+2 instead of 1+1 in expect then it will result in 3, so it won’t pass the test.

Example Output Passed Test: –

Example Output Failed Test: –

7. Let’s add some other operations for test case in math operation.

8. Check again as you did last time, and the command line will show you the number of passed or failed cases. You can customize the cases based on your requirements, for example, output must be a positive number or not zero.

Example Output :-

9. The official documentation at mochajs.org can provide you with more details and help you achieve your goal.

Conclusion

With the Test Driven Development method described above, you will have a much easier time in testing your Node applications. If you encounter any issues in the steps mentioned above, please feel free to drop a question in the comments down below.

The following two tabs change content below.

Rajdeep Vaghela

Software Developer (Open Source) at DEV Information Technology Ltd
Rajdeep Vaghela holds a Bachelor of Science in Information Technology degree by education and is a software developer by profession. With over four years of experience as a software developer, he is an expert at PHP-based frameworks such as laravel, Zend. During his time of leisure, he likes to watch movies and listening music. he believes in learning continuously.

Leave a Reply

Your email address will not be published.