@ngrx Angular RxJS Testing

Setup Karma For Testing Angular 1.x Components With ES2015 & Browserify

Converting AngularJS.x code to use ES2015 is one thing. It’s an iterative process that should be taken with great care. However, aside from converting the code, you should also convert the tests to the setup environment. In this article I share the setup I use for running Echoes Player tests with karma & browserify.

Tests Setup

To simply put – I believe in writing tests for code. It promotes robustness of the code that you write, healthy mentality as a developer and assurance that your code does what it’s supposed to do, covering edge cases as well.

For my open source project, Echoes Player, I use a testing environment setup that includes:

  • karma runner – for running tests and loading testing environments
  • Chrome – as the js engine to run the tests on
  • Phantomjs – a js engine ro run the tests in the terminal
  • jasmine – for writing tests
  • Babel – for writing ES2015 in tests

Angular 1.x with ES2015 & Browserify

In a previous article, I wrote about writing AngularJS.x with ES2015 and preparing AngularJS.x code to Angular (+2). Yet, browsers don’t know to parse some ES2015 features – for that – the code needs to be parsed and compiled to ES5.

For Echoes Player, I’m currently using browserify to achieve the above (webpack is an alternative and out of the scope of this article). I’m using a modified gulp task that I’ve found in various projects:

In a birds eye-view, the “browserify” gulp task handles several duties:

  1. Module loading by Loader spec (previously ES2015 Spec)
  2. ES2015 syntax with babel
  3. Annotating Modules with ng-annotate (browserify version) properly to ES2015 classes and to ES5 functions
  4. Loading html files and converting it to strings
  5. In development mode, “watching” for changes and recompiles with the 4 steps above

The final product of this task is a ES5 javascript file (browserified) bundled with all the code needed for the app to run. That includes 3rd party libraries as well.

Setup Karma to Work With Browserify & ES2015

There are few steps needed for running the new “browserified” bundled code in karma and running the tests with it.

Since browserify creates a private scope for each module and since we’re using ES2015 modules, tests need to be run inside a module aware environment and be “modularized” with browserify as well.

Fortunately, this process is easily achieved with configuring the karma.conf.js file with new plugins.

First, we need to add karma-browserify plugin, which allows to run the specs (tests) inside browserify world. After installing this plugin with npm, make sure you do the following in karma.conf.js file:

  1. add “browserify” to the “framworks” array property.
  2. add “karma-browserify” to the “plugins” array.
  3. configure the pre-processor to browserify the specs files in the “preprocessors” property.
  4. add a new property, “browserify” which holds the configuration setting to allow source maps and compile specs with babel – so you can write specs with ES2015.

i.e., This is the setup that I use in echoes:

That’s it.

After setting up karma with these, specs will run with the ES2015.

Final Thoughts

The full configuration of karma with browserify and babel, can be found in github.

Although the tests run with ES2015 and the new code of AngularJS.x ES2015 based components, the old tests need to be updated according to the changes I made to the code. It should be a simpler task for testing controllers and services – since the tests will simply check functions. As for testing directives/components that render to the DOM – that’s for another future post.

I hope to write about converting tests to tests Angular 1.x components written with ES2015 which covers all aspects by the style guide I wrote writing AngularJS.x components with ES2015 – please feel free to collaborate.