Write quality code: understanding TDD using Mocha and Chai.

BIENAIME COPAIN FABRICE
5 min readNov 21, 2018

Test Driven Development is an easy concept, it’s a discipline and a necessity in today’s software development. Perhaps you’ve heard of test-driven development but don’t really know what it is. Or maybe you know about it but you just don’t know why you would ever want to use it. To most of us, it’s a waste of time or we actually write test after we have already written our product code (just for the sake of requirements) right?

Well, joining the Andela bootcamp, I had to learn Test Driven Development from scratch. It wasn’t the first time that I was required to use TDD but I had always found ways to bypass it. Guess what, if you are applying for the Andela fellowship; there is no way you will go away from TDD. Your LFA will ask you to test your code, show badges that indicates the coverage and all of that… does it sound gibberish? Worry not, let’s dive in together and see what it is and later talk about some frameworks used to test codes.

Test Driven Development is the practice of writing a test for a piece of required functionality, before writing any implementation code.

Does it make sense to you now? Basically, you want to write quality code that will do what you want in the right way, by right tools and run in most environments. TDD is a discipline, and as such you must try your best to not be tempted to write tests after you’ve written code. Most developers are on pressure from school or clients, but remember that having VALUES will make your work excellent.

TDD will help you ensure that your code has met all acceptance criteria, such that all the required features are covered. It will then help you to focus on what is important for your software, keeping you on chunks of functionality at a time rather than the application as a whole. writing one test at a time helps you to keep thinking of the public interface and thinking about all dependencies. Codes for which tests have passed are safer to refactor, they have fewer bugs and they will give you a proper return.

Well I think that now you understand why it is important to write tests before the actual code. Wait! Wondering how to test?, with what? The foundation of test-driven development is automated testing, since without it, TDD would not be possible. In order to understand TDD, it is critical to also have a good understanding of automated testing. There are different types of TDD, however in this blog post, I will only talk about 3 that you will probably be required to use for your bootcamp project.

  • Unit Testing: It allows for the testing of individual units of code. Typically a unit test is written in order to execute a specific method or function within a class or modules of code.
  • Integration Testing: Integration testing is the logical progression from unit testing and it seeks to test multiple units and ensure that the units are able to produce the desired functionality when they are all brought together.
  • Performance Testing: with performance testing you typically seek to simulate a real-world usage scenario where you have a large number of users accessing and using the application simultaneously. Automated performance testing will use various tools to simulate such user behaviour and it ensures that the system can still perform under varying degrees of load.

http://agiledata.org/essays/tdd.html for more

While there are many tools for testing your code; at the bootcamp, we were required to test JavaScript codes running on Node.JS using either jasmine.js or Mocha.js & Chai. Personally, I prefer Mocha & Chai over Jasmine, but you can have a different view. They perform the same services it’s simply the matter of choice.

Mocha and Chai

  1. You can install it globally from your terminal using :

When you install an npm module globally, you aren’t limiting its use to your current project. Instead, you’re able to access and use the module like a command line tool.

$ npm install --global mocha
  • or as a development dependency for your project:
$ npm install --save-dev mocha

NB: Mocha currently requires Node.js v6.x or newer

Assuming that you have created already a project directory and that it contains a test.js file, we will then initialize it with npm init. This should allow us to create a package.json file easily and we will be asked some question to answer in the command line. The question that is most important here is ‘test command:’ — respond with ‘mocha’. This way we can run mocha by simply typing npm test.

Your package.json file should at least contain:

"scripts": {
"test": "mocha"
},

Once you have all of the above, we’re ready to test!

Remember, Mocha is a testing frameworks. That means it’s used to organize and execute tests. When writing a test, there are two basic function calls you should be aware of: describe() and it()

  • describe() is simply a way to group our tests in Mocha. We can nest our tests in groups as deep as we deem necessary. describe() takes two arguments, the first is the name of the test group, and the second is a callback function.
describe('string name', function(){
// can nest more describe()'s here, or tests go here
});
  • it() is used for an individual test case. it() should be written as if you were saying it out loud: “It should equal zero”, “It should log the user in”, etc. it() takes two arguments, a string explaining what the test should do, and a callback function which contains our actual test.
it('should blah blah blah', function(){
// Test case goes here
});

With Mocha, we use an assertion library that tests if our results are true. For the bootcamp project we were asked to use Chai.

Install chai by running $ npm install cha i in the terminal.

Note that we don’t need to use an assertion library, but they make testing easier.

Frankly the whole TDD concepts and how it works amazed me so much and most of it was new to me. I hope that this post will help all of you that needs to understand what TDD is and have an idea of some of the tools you can use to test your codes.

--

--

BIENAIME COPAIN FABRICE

Full stack Software Engineer @ Andela Kigali | Postgres | React | Express | Node. I learn by writing and sharing.