Tag: karma

Web Development

Angular Unit-Testing: TypeError ‘angular.element.cleanData is not a function’

Fact: When angular and angular-mocks do not agree, Jasmine gets angry

Today’s post is more like a “Note to self.” When angular and angular-mocks to not agree, go ahead and make sure they are of the same version*. Otherwise PhantomJS, Firefox, Chrome, etc.. will fail your tests and that’s bad karma** and a bad relationship with Jasmine.

Ok, onto the problem: TypeError angular.element.cleanData is not a function… Did you just see that when trying to run your little nice karma: karma start karma.conf.js (that’s the name we chose for our test configuration file, but your name is most likely different but still a JavaScript file)?

Don’t panic! The guys at Github have something for you. They say it is a known issue (as of when the article was written of course). While some choose to downgrade their angular and angular-mocks to avoid the problem, others just upgrade angular and angular-mocks with the one-liner:

npm update angular angular-mocks -g

Please remember to use sudo if you are on Mac or Linux for this global scope (-g) operation.

With that done, if the tests continue to fail and you have made sure your test scripts are clean, the problem will most likely be resolved with this trick suggested by @KeithPepin in the github solution: go to your angular-mocks.js file (e.g. ~/bower_components/angular-mocks/angular-mocks.js) and replace angular.element.cleanData(cleanUpNodes); with if (angular.element.cleanData) angular.element.cleanData(cleanUpNodes);

And there you go until the people at angular get their act together and fix the issue!

Run your karma file again to see what happens and leave a comment here if you do not get green checks for success!

* You can check versions with npm angular –version and then npm angular-mocks –version

**Don’t get me wrong, this karma here is just for the same of the pun.

Please read more here about unit testing with Angular.

Advertisements