Monday, October 20, 2014

CodePen, AngularJS, and Jasmine!

Online Web editors, like JSbin, plnkr, and CodePen, are awesome. They allow you to easily work on an idea without all of the muss and fuss of having to spin up a server. You can also share the idea with friends or the entire Internet easily. 

Unit test is something that all developers, especially AngularJS ones, are encouraged to do. I work on a lot of angular ideas initially in CodePen. I also write unit test in Jasmine, but getting the two to play nice together in CodePen wasn't as easy as I thought it should be. 

In the pen below I show how to get the two working together. The main trouble I was having was the order of the files. It has to be as follows:


  1. jasmine.css
  2. jasmine.js
  3. jasmine-html.js
  4. boot.js 
  5. angular.js
  6. angular-mocks.js
If you get the order wrong, you may get errors like: ReferenceError: inject is not defined or ReferenceError: module is not defined. These can make you think something is wrong with your code when there isn't. 

The pen below is written using Jasmine 2.0 and AngularJS 1.26.





See the Pen finished-jasmine-unit-test by Troy Miles (@Rockncoder) on CodePen.