First Web Application made easy with NodeJS Part 3

From our last part, we wrote code without test, it’s UNACCEPTABLE!!

Prepare to test!

In this part, we’re going to test our API using Chakram.

Chakram is a REST API testing framework which offer us a Promise and BDD testing style.

install Chakram and save it into your package.json

If you never heard of Mocha or don’t have it, you have to install it too.

if you don’t have Mocha yet, please install it

Testing time!!

  1. Create a directory tests and create a sub-directory called api 
    And make a courses.js in that sub-directory.
it will look like this

2. Put the following code

Lines 6 : we give a description about our tests.
Lines 8 : we can initialize some data that we need before all tests start.
Lines 11 : we give a brief description about our test case.
Lines 12–17 : we use Chakram to make a HTTP GET request to our site and we expect that the actual result will match the Lines 15–17.
Line 22 : if we want something to run after all test are done. we can write it here.

3. Run it!

we use — recursive so that our test(s) in sub-directory will be executed
Well done!

4. Add more tests

I don’t think I need to explain about this file because this file is easy to read, someone may think that it is the same as English language (This is the POWER of BDD style!!)