Cool Geek Cool Geek - 1 month ago 7
Node.js Question

Unit Testing for JavaScript and Mocha

I have written a JavaScript library/module that makes API calls to a restful web service and provide methods to the consumers of this library. This library is built in such a way that the same code is used for both as a front end library or as a npm module (converted code using browserify).

However, I now plan to write unit tests for the same and this is the first time I have written an entire library. So, I have looked at libraries like mocha and Jasmine which seems like the good choices for it. I am thinking to write the tests in Mocha. But, I am not quite sure how to start with these tests.

Also, should the tests which test the functionality like a sample consumer be called as integration tests or should those be combined with the unit tests?

I would be happy if anyone can recommend some sample tests on github or some kind of tutorial for a Mocha beginner. I would be happy to consider other unit testing frameworks as well.

Thanks in advance

Answer

I am thinking to write the tests in Mocha. But, I am not quite sure how to start with these tests.

Unit test usually only includes function test, excluding integration and regression test, which means you should test every functions as possible as you can.

A basic example is like this.

function alwaysReturnTrue(){
   return True;
}
assertTrue(alwaysReturnTrue());

About unit test framework, such as mocha, Jasmine, they are only for initiating a framework to run your test. Most of them will provide basic assert function, such as assertTrue, assertFalse, assertEqual...etc.

Apparently, those assert methods are not enough to test all corner cases.

  • callback function
  • function called sequence
  • function called parameters
  • trigger exception
  • Fake XHR or Server

For handling those stuffs, it could be completed by a popular solution, which is called sinon.


Also, should the tests which test the functionality like a sample consumer be called as integration tests or should those be combined with the unit tests?

No, Integration test usually repeat the whole sequence of your software, not a single function. Those thing are also able to be done by sinon. (Fake XHR or Server)

For example: without UI
Login() -> Send exit request to serverlet -> Logout()
Login() -> Send exit request with save="true" data to serverlet -> Logout()

As I mentioned above, we still have another test which has not been discussed yet. It is called Regression test which is the most complex test. There are two famous UI testing libraries to do this, selenium and phantomjs.

For example: with UI
Login() -> Click Button Exit() -> Logout()
Login() -> Click Checkbox A() -> Click Button Exit() -> Logout()

Overall, a completed software testing process have three processes. However, in the real world, time is limited. If your project size is small, than you may skip regression or integration test.

step: unit test -> integration test -> regression test
time: (short)      (longer)            (very long)
Comments