Search code examples
angularjsunit-testingangularjs-directivekarma-runnerng-html2js

ng-html2js doesn't work with Phantom, does work with Chrome


I'm using cg-angular to scaffold out my project.

I'm testing a directive that uses templateUr, and ng-html2js to load my templates into a module. When I run my test using Chrome as the browser it passes no problem, but when I run it with Phantom I get:

Error: [$injector:modulerr] Failed to instantiate module templates due to:

Here's the relevant part of my Gruntfile:

karma:      {
     options:     {
         frameworks:           ['jasmine'],
         preprocessors:        {'directive/**/*.html':['ng-html2js']},
         files:                [  //this files data is also updated in the watch handler, if updated change there too
             '<%= dom_munger.data.appjs %>',
             'bower_components/angular-mocks/angular-mocks.js',
             'directive/**/*.html',
             createFolderGlobs(['*-spec.js'])
         ],
         ngHtml2JsPreprocessor:{
             moduleName:'templates'
         },
         logLevel:             'ERROR',
         reporters:            ['mocha'],
         autoWatch:            false, //watching is handled by grunt-contrib-watch
     },
     all_tests:   {
         browsers: ['PhantomJS', 'Chrome'],
         singleRun:false
     },
     during_watch:{
         browsers: ['PhantomJS'],
         singleRun:true
     }
 }

So running grunt test which runs karma:all_tests, works like a champ. grunt serve which runs karma:during_watch fails.

Is there a way to check if html2js is actually being run?

Edit It's not Phantom. If I run Chrome in the 'during_watch' task it also fails.


Solution

  • Figured it out. I'm using generator-cg-angular to scaffold out this project. It uses the grunt watch event to set up some of the options. This event is only used during the serve task and not during test or build. I didn't notice that it is overwriting karma.options so html2js was never being called. When I appended this to the event section of my Gruntfile html2js runs as it should. Below is the relevant section of my Gruntfile showing the changes.

    if (grunt.file.exists(spec)) {
        var files = [].concat(grunt.config('dom_munger.data.appjs'));
        files.push('bower_components/angular-mocks/angular-mocks.js');
        files.push(spec);
        files.push('directive/**/*.html'); // This one
        grunt.config('karma.options.files', files);
        grunt.config('karma.options.ngHtml2JsPreprocessor.moduleName', 'templates'); // And this one
        tasksToRun.push('karma:during_watch');
    }