Search code examples
angularjsgruntjsmomentjskarma-jasmineangular-moment

"Moment cannot be found by angular-moment" when trying to test with karma & jasmine - AngularJS


My app runs correctly and the AngularMoment works perfectly with MomentJS.

Lately I tried to implement unit-testing into my app on grunt watch, using Karma and Jasmine. All the modules seem to load fine, but only the moment can not be found by angular-moment. (error message below):

Error during loading: Uncaught Error: Moment cannot be found by 
angular-moment! Please reference to: https://github.com/urish/angular- 
moment in js/libs/angular-moment.js line 27

All the dependencies are loaded into the karma.config.js and still it doesn't work.


Solution

  • The order of the files in karma.config.js matters. We have to load certain libraries before the others, as stated below.

    files: [
        //Dependencies of the app
        'dist/js/libs/jquery.js',
        'dist/js/libs/angular.js',
        'dist/js/libs/angular-mocks.js',
        'dist/js/libs/moment.js',
        'dist/js/libs/*.js',
        .
        .
        .
    ]