Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: angular.element.cleanData is not a function

I'm getting the following error in my karma unit test 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'm 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
    })
  }
like image 326
chovy Avatar asked Apr 04 '16 21:04

chovy


3 Answers

I had the same problem and what fixed it to me was to load jquery in 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 alongside Angular.

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

like image 137
yishaiz Avatar answered Nov 20 '22 14:11

yishaiz


removing the duplicate angular.js and also ensuring that all angular libs were using 1.4.x fixed my problem. I had some using 1.3.15 and others 1.4.7

like image 45
chovy Avatar answered Nov 20 '22 12:11

chovy


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

like image 6
szuuuken Avatar answered Nov 20 '22 13:11

szuuuken