TypeError: angular.element.cleanData is not a function

I get the following error in my unit test karma when trying to use inject()

  Example βœ— should wait for promise to resolve and have a result Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test. βœ— "after each" hook for "should wait for promise to resolve and have a result" TypeError: angular.element.cleanData is not a function at Function.module.$$cleanup (base/app/bower_components/angular-mocks/angular-mocks.js?aa216eb2339283c5ab5aa192023171aa202a2bbd:2776:23) at Context.module.$$afterEach (base/app/bower_components/angular-mocks/angular-mocks.js?aa216eb2339283c5ab5aa192023171aa202a2bbd:2746:14) 

Here is the code:

 describe('Example', function() { var $q; var $rootScope; var fakePromise; beforeEach(inject(function (_$q_, _$rootScope_) { $q = _$q_; $rootScope = _$rootScope_; fakePromise = function fakePromise(){ var def = $q.defer(); setTimeout(function(){ def.resolve('foo'); }, 100); return def.promise; }; })); it('should wait for promise to resolve and have a result', function(){ return fakePromise().should.eventually.equal('foo'); }); }); 

I am using angular and angular -mocks 1.5.3.

Here is my karma config:

  // Karma configuration // Generated on Tue Mar 29 2016 14:28:04 GMT-0700 (PDT) module.exports = function(config) { config.set({ // base path that will be used to resolve all patterns (eg. files, exclude) basePath: '', // frameworks to use // available frameworks: https://npmjs.org/browse/keyword/karma-adapter frameworks: ['mocha', 'chai', 'chai-as-promised', 'sinon'], // list of files / patterns to load in the browser files: [ 'app/bower_components/angular/angular.js', 'app/bower_components/angular-mocks/angular-mocks.js', 'app/bower_components/es5-shim/es5-shim.js', 'app/bower_components/jquery/dist/jquery.js', 'app/bower_components/x2js/xml2json.js', 'app/bower_components/angular/angular.js', 'app/bower_components/angular-messages/angular-messages.js', 'app/bower_components/angular-bootstrap/ui-bootstrap-tpls.js', 'app/bower_components/angular-cookies/angular-cookies.js', 'app/httpsnippet-browser/dist/restlet-angular-httpsnippet.js', 'app/bower_components/highlightjs/highlight.pack.js', 'app/bower_components/angular-highlightjs/angular-highlightjs.js', 'app/bower_components/marked/lib/marked.js', 'app/bower_components/angular-marked/angular-marked.js', 'app/bower_components/angular-pretty-xml/src/angular-pretty-xml.js', 'app/bower_components/angular-resource/angular-resource.js', 'app/bower_components/angular-sanitize/angular-sanitize.js', 'app/bower_components/ace-builds/src-noconflict/ace.js', 'app/bower_components/ace-builds/src-noconflict/mode-yaml.js', 'app/bower_components/ace-builds/src-noconflict/ext-language_tools.js', 'app/bower_components/ace-builds/src-noconflict/ext-searchbox.js', 'app/bower_components/angular-ui-ace/ui-ace.js', 'app/bower_components/angular-ui-router/release/angular-ui-router.js', 'app/bower_components/angular-ui-validate/dist/validate.js', 'app/bower_components/angular-ui-uploader/dist/uploader.js', 'app/bower_components/angular-inview/angular-inview.js', 'app/bower_components/ng-resize/index.js', 'app/bower_components/bootstrap/dist/js/bootstrap.js', 'app/bower_components/js-yaml/dist/js-yaml.js', 'app/bower_components/json3/lib/json3.js', 'app/bower_components/ngstorage/ngStorage.js', 'app/bower_components/yaml-js/yaml.js', 'app/bower_components/async/lib/async.js', 'app/bower_components/traverse/traverse.js', 'app/bower_components/path-loader/browser/path-loader.js', 'app/bower_components/json-refs/browser/json-refs.js', 'app/bower_components/lodash/lodash.js', 'app/bower_components/spark-md5/spark-md5.js', 'app/bower_components/swagger-converter/browser.js', 'app/bower_components/z-schema/dist/ZSchema-browser-min.js', 'app/bower_components/visionmedia-debug/dist/debug.js', 'app/bower_components/swagger-tools/browser/swagger-tools.js', 'app/bower_components/apply-diff/index.js', 'app/bower_components/json-editor/dist/jsoneditor.js', 'app/bower_components/schema-form/dist/schema-form.js', 'app/bower_components/ng-file-upload/ng-file-upload.js', 'app/bower_components/json-formatter/dist/json-formatter.js', 'app/bower_components/raf/index.js', 'app/bower_components/angular-ui-layout/ui-layout.js', 'app/bower_components/json-schema-view/dist/json-schema-view.js', 'app/bower_components/objectpath/lib/ObjectPath.js', 'app/bower_components/angular-native-dragdrop/draganddrop.js', 'app/bower_components/d3/d3.js', 'app/bower_components/angular-translate/angular-translate.js', 'app/bower_components/json-schema-defaults/lib/defaults.js', 'app/bower_components/tv4/tv4.js', 'app/bower_components/angular-schema-form/dist/schema-form.js', 'app/bower_components/angular-schema-form/dist/bootstrap-decorator.js', 'app/bower_components/angular-animate/angular-animate.js', 'app/bower_components/angular-aria/angular-aria.js', 'app/bower_components/angular-material/angular-material.js', 'app/bower_components/moment/min/moment-with-locales.js', 'app/bower_components/js-base64/base64.js', 'app/bower_components/sway/browser/sway.js', 'app/bower_components/ngInfiniteScroll/build/ng-infinite-scroll.js', 'app/bower_components/validator-js/validator.min.js', 'app/httpsnippet-browser/dist/restlet-angular-httpsnippet.js', 'app/ace/theme-atom_dark.js', 'app/core/**/*.js', 'app/**/*UnitSpec.js' ], // list of files to exclude exclude: [ ], // preprocess matching files before serving them to the browser // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor preprocessors: { // '**/*.js': ['coverage'] }, // test results reporter to use // possible values: 'dots', 'progress' // available reporters: https://npmjs.org/browse/keyword/karma-reporter // reporters: ['progress', 'coverage'], reporters: ['spec'], // web server port port: 9876, // enable / disable colors in the output (reporters and logs) colors: true, // level of logging // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG logLevel: config.LOG_INFO, // enable / disable watching file and executing tests whenever any file changes autoWatch: true, // start these browsers // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher browsers: ['Chrome'], // // optionally, configure the reporter // coverageReporter: { // type : 'html', // dir : 'coverage/' // }, // Continuous Integration mode // if true, Karma captures browsers, runs the tests and exits singleRun: false, // Concurrency level // how many browser should be started simultaneous concurrency: Infinity }) } 
+6
source share
7 answers

removing duplicate angular.js as well as ensuring that all angular libs use 1.4.x fix my problem. I had use 1.3.15 and other 1.4.7

+6
source

I had the same problem and I needed to load jquery into my tests:

  files: [ 'bower_components/jquery/dist/jquery.js', 'bower_components/angular/angular.js', 'bower_components/angular-mocks/angular-mocks.js', ... ] 

took the idea from this comment because we also use jQuery next to Angular.

(Reducing my version of Angular from 1.5.3 to 1.5.0 also did the job, but now with my solution I can use the latest version of Angular ...)

+17
source

using the same version of angular and angular -mocks solved the problem for me

+6
source

I had the same problem and what was fixed:

Go to angular-mocks.js and replace:

 angular.element.cleanData(cleanUpNodes); 

with

 if (angular.element.cleanData) angular.element.cleanData(cleanUpNodes); 

This post suggested: https://rafikitechnology.com/2016/04/26/fixed-issue-with-angular-and-angular-mocks-do-not-agree-jasmine-is-angry/

+2
source

I think this is the same problem as https://github.com/angular/angular.js/issues/14251 (the error message looks different depending on which browser you use for Karma tests), I downgraded version of Angular from 1.5.3 to 1.5.0, and the error went away. I think we will have to wait until it is fixed in a newer version.

+1
source

I work in a very old deprecated application, and my problem was two-part:

  • The jQuery version included in the main page of the application was also old, so I had to import a newer version before angular.
  • I also moved my jQuery UI included above angular.js include (there is a bug due to which this was a problem and should be fixed in later versions of Angular).
0
source

I had the same problem, I fixed it by adding jquery denpendency from above to angular in the karma configuration file.

  files: [ { pattern: 'libs/jquery/jquery/dist/jquery.min.js', watched: false }, { pattern: 'libs/angular/angular/angular.js', watched: false }, .....] 
0
source

All Articles