karma-ng-html2js-preprocessor not working gulp + angular + karma + ng-html2js

4.4k views Asked by At

I can't make karma-ng-html2js-preprocessor working for external template.

Package Json file:

    .....
    "gulp-karma": "*",
    "karma-coverage": "*",
    "karma-jasmine": "*",
    "karma-ng-html2js-preprocessor": "*",
    "karma-phantomjs-launcher": "*",
    .....

Karma config file:

    config.set({
        browsers: [
             ....
        ],
        frameworks: [
            'jasmine'
        ],
        plugins: [
            'karma-jasmine',
            'karma-phantomjs-launcher',
            'karma-ng-html2js-preprocessor'
        ],
        preprocessors: {
            'app/**/*.html': 'ng-html2js'
        },
        ngHtml2JsPreprocessor: {
            stripPrefix: 'app/'
        }
    });

Files are defined in Build file and passed to gulp-karma. Here are the defined files:

config = {  test: {
          configFile: '.../karma.conf.js',
          depends: [
              .......
          ],
          files: [
            "app/**/*.js",
            'app/**/*.html'
          ]
       }
    }

Loading template in my directive spec like below:

beforeEach(module('app'));
beforeEach(module('app/tem/mytemp.html'));

I am getting the error below:

Error: [$injector:modulerr] Failed to instantiate module app/tem/mytemp.html due to:
Error: [$injector:nomod] Module 'app/tem/mytemp.html' is not available! You either misspelled the

In karma debug.html html files are loaded in link tag output:

<script type="text/javascript" src="/absoluteC:.../app/tem/comp/mydirective.js"></script>
<link href="/absoluteC:..../app/tem/mytemp.html" rel="import">
<script type="text/javascript">
window.__karma__.loaded();

Am I missing anything? How do I debug and move forward from this issue?

2

There are 2 answers

0
Nexeh On

I am new to this as well and I'm googling around for another problem but I think I'm a little further along than you are (I also realize this quest is a little old).

What I did was to make the following change to the ngHtml2JsPreprocessor section of the Karma config file

ngHtml2JsPreprocessor: {
    stripPrefix: 'app/',

    // ADDED THIS: the name of the Angular module to create
    moduleName: "my.templates"
}

Then in my test I referenced that module name instead of the HTML name.

beforeEach(module('my.templates'));

I hope that helps even if it's late. Or that someone else finds the information useful as they are searching around.

0
vekerdyb On

Here is how I solved the exact same problem:

1) npm install karma-ng-html2js-preprocessor --save-dev (you have already done that)

2) In karma.config.js:

// .... 

preprocessors: {
  '**/*.html': ['ng-html2js']
},

// ....

ngHtml2JsPreprocessor: {
  stripPrefix: 'app/',  // <-- change as needed for the project
  // include beforeEach(module('templates')) in unit tests
  moduleName: 'templates'
},

plugins : [
    'karma-phantomjs-launcher',
    'karma-jasmine',
    'karma-ng-html2js-preprocessor'
]

3) Since gulp-karma overwrites the files property of the karma.conf.js, change the gulp task config for your test setup(s) (I had two: one test that runs the tests once and another called tdd for continuous testing) to something like this:

gulp.task('test', function() {
  var bowerDeps = ...

  var testFiles = bowerDeps.js.concat([
    'app/scripts/**/*.js',
    'test/unit/**/*.js',
    'app/**/*.html' // <-- This is what you need to add to load the .html files 
  ]);

  return gulp.src(testFiles)
    .pipe($.karma({
      configFile: 'test/karma.conf.js',
      action: 'run'
    }))
  ....
});

Hope this will help someone.