Unit testing in JavaScript – Jasmine 2.0 + RequireJS + JSCover + PhantomJS

It took me a while to setup unit testing in JavaScript project I’m involved in. I must say the task was so difficult to me that I’ve decided to share my experience and final setup. Not that unit testing itself is hard. PHP, Ruby, Java… never had any problems. JavaScript was a long ride.

My setup consist of the following tools:

  • Jasmine 2.0 (previously 1.3) – unit testing library
  • RequireJS – AMD library
  • JSCover – test coverage tool, supporting lines of code, branches and functions coverage metrices
  • PhantomJS – headless browser

Folders structure separates the tools used, unit tests, project source code and libraries project depends on from each other, so that adapting it to any project should be straight forward:

The actual setup can be cloned from github:

https://github.com/wojciechszela/jasmine-requirejs-jscover

and it comes with few JavaScript classes under test and their corresponding unit tests. Please excuse the quality of tests and classes. They serve just as proof of setup.

To use it as is Ant is needed. Just run

 

without any other parameters and it will print you short help. To run unit tests run “unittest” target:

Generating coverage report requires having JSCover running:

The report will be stored in “build/coverage” folder.

Ant build script is mostly about starting/stopping JSCover and running PhantomJS (unit tests via PhantomJS) – it should be fairly simple to port it to Maven, Grunt or any other build tool. There is only one thing that needs to be taken care when porting, that is, generating application.js file. This file loads all application code using require method of RequireJS. Again, generating this file using any other build tool should be easy.

I hope that you will find this setup useful and it will save you time trying to get Jasmine, RequireJS, PhantomJS and JSCover to work together.

Microservices architecture presentations

Friend of mine, Paweł, has recently shared few very interesting recordings of presentations about microservices architecture. Follow to his blog posts to learn from the best what microservices architecture is about and how it can make your team be even more agile:

I’ve found the first one particularly interesting as it goes beyond technical side only, touching topics like how microservices architecture can reshape your team/organization. The one by Fred George got me into too – part of it is the story of project Paweł and I we were involved in.

Object oriented programming in JavaScript

OOP paradigm is probably most discussed topic in JavaScript and most popular type of library extending the language. I believe that first approach to make JS object model more usable was made by Douglas Crockford, in 2002. 11 years have passed and honestly nothing remotely close to common OOP toolset has been offered. Inheritance, interfaces, visibility scope… all these tiny features that make OOP easier are still no there or are hacked in very peculiar way. I was already thinking we’ll have to wait another 10 or so years for popular browser to fully implement ECMA 2, and 3, and 5 to get these abstracts and private and so on to be generally available. Wait no more. Someone did amazing job bringing all the missing “keywords” into JavaScript way and in rather clean way. And wrapped as AMD. And faster than vanilla classes. Get ready to experience dejavu.

Dejavu package comes in two versions. Strict and loose, or better call them development and production. The development one does all the heavy checks of method signatures and full interface implementations. It makes library slow but hey, if you do not implement a method of an interface your tool should warn you. The loose version is suited for production. It’s extremely fast but the speed is gained by removing all the heaving checking. Thus if your class is missing a method of an interface it is implementing and you run your code with loose version of dejavu you won’t be warned. Code will execute and will work… or not.

Feature list is extensive. Dejavu provides syntax for:

  • concrete, abstract and final classes as well interfaces
  • working JavaScript instanceof for classes and new function instanceOf for interfaces
  • private and protected (visibility)
  • static members and constants
  • “super” for calling super (parent) implementation of methods, including constructor
  • verification of method signatures
  • context binding
  • freezing and unfreezing objects

It also lets you define singletons and do classes mixins but I hope no one uses these features.

Since dejavu homepage provides complete examples of all features I’m not going to duplicate the effort and write practically copy/paste tutorial. I’ll just borrow one of example classes:

Convinced? Go to dejavu homepage and improve your JavaScript OOP coding experience.