During setup of Jasmine 2.0 for AngularJS 1.0.7 application it turned out that they are not compatible. The reason is that Angular 1.0.7 is compatible with the older version of Jasmine – 1.3. And there were several breaking changes in Jasmine private API which was used by AngularJS.
There were two ways to fix this issue:
- Upgrade AngularJS to 1.2.x
- Fix Jasmine-related stuff in AngularJS 1.0.7
The first path was reasonable, but required much more efforts than the second one. So, we found a way to make it work for old version of AngularJS.
Solution
To make a fix we need to change only “src/ngMock/angular-mocks.js” file. It contains all the code responsible for interaction with Jasmine.
This gist contains required patch:
diff --git a/src/ngMock/angular-mocks.js b/src/ngMock/angular-mocks.js index 41217c7..fe09bc6 100644 --- a/src/ngMock/angular-mocks.js +++ b/src/ngMock/angular-mocks.js @@ -1635,13 +1635,28 @@ window.jstestdriver && (function(window) { window.jasmine && (function(window) { + var currentSpec = null, + isSpecRunning = function() { + return !!currentSpec; + }; + - afterEach(function() { - var spec = getCurrentSpec(); - var injector = spec.$injector; + (window.beforeEach || window.setup)(function() { + currentSpec = this; + }); - spec.$injector = null; - spec.$modules = null; + (window.afterEach || window.teardown)(function() { + var injector = currentSpec.$injector; + + angular.forEach(currentSpec.$modules, function(module) { + if (module && module.$hashKey) { + module.$hashKey = undefined; + } + }); + + currentSpec.$injector = null; + currentSpec.$modules = null; + currentSpec = null; if (injector) { injector.get('$rootElement').unbind(); @@ -1664,12 +1679,7 @@ window.jasmine && (function(window) { }); function getCurrentSpec() { - return jasmine.getEnv().currentSpec; - } - - function isSpecRunning() { - var spec = getCurrentSpec(); - return spec && spec.queue.running; + return currentSpec; } /**
Note that patch works only for “angular.js” repository. For your project you should replace all paths “src/ngMock/angular-mocks.js” with correct path in your repository. For example, in our project it is located in “spec/javascripts/helpers/angular-mocks.js”.
Put this gist into file “patch-angular-mocks.diff” in the root of your git repository. Fix paths for “angular-mocks.js” as noticed above and apply patch with:
$ git apply patch-angular-mocks.diff
Now it should work with new Jasmine! This patch works also for Angular 1.0.8.
Notice for Jasmine upgrade
If you want to upgrade Jasmine from 1.x to 2.0, don’t forget to read about breaking changes.