Tuesday, January 21, 2014

yeoman - testing with mocha and chai instead of jasmine

I've been playing around with yeoman these days. It seems like project worth a try for my free time single page application.

After the inital setup (using AngularJS generator) I decided to let my tests run with mocha + chai (with expect asertions) rather than jasmine (generated by default).

In fact it was super-simple. The diffs that matter:
diff --git a/yo/karma.conf.js b/yo/karma.conf.js
index bc0e168..385045f 100644
--- a/yo/karma.conf.js
+++ b/yo/karma.conf.js
@@ -7,7 +7,7 @@ module.exports = function(config) {
     basePath: '',
 
     // testing framework to use (jasmine/mocha/qunit/...)
-    frameworks: ['jasmine'],
+    frameworks: ['mocha', 'chai'],
 
     // list of files / patterns to load in the browser
     files: [
diff --git a/yo/package.json b/yo/package.json
index 3e3db14..9649a55 100644
--- a/yo/package.json
+++ b/yo/package.json
@@ -34,7 +34,8 @@
     "karma-html2js-preprocessor": "~0.1.0",
     "karma-firefox-launcher": "~0.1.3",
     "karma-script-launcher": "~0.1.0",
-    "karma-jasmine": "~0.1.5",
+    "karma-mocha": "~0.1.1",
+    "karma-chai": "~0.0.2",
     "karma-coffee-preprocessor": "~0.1.2",
     "requirejs": "~2.1.10",
     "karma-requirejs": "~0.2.1",
diff --git a/yo/test/spec/controllers/main.js b/yo/test/spec/controllers/main.js
index eb7cda2..f9becde 100644
--- a/yo/test/spec/controllers/main.js
+++ b/yo/test/spec/controllers/main.js
@@ -17,6 +17,6 @@ describe('Controller: MainCtrl', function () {
   }));
 
   it('should attach a list of awesomeThings to the scope', function () {
-    expect(scope.awesomeThings.length).toBe(3);
+    expect(scope.awesomeThings.length).to.equal(3);
   });
 });
Afterwards just running (to fetch missing packages):
npm install
and tests succeed:
grunt test