Search code examples
karma-runnerkarma-jasmineng-html2js

Karma preprocessor not running


My karma.conf.js includes:

plugins: [
    'karma-jasmine',
    'karma-phantomjs-launcher',
    'karma-ng-html2js-preprocessor'
],
preprocessors: {
    '../../mypath/*.html': ['ng-html2js']
},
ngHtml2JsPreprocessor: {
    moduleName: 'templates'
},

(I've tried without specifying any plugins, too.)

My devDependencies include:

"karma-ng-html2js-preprocessor": "^0.2.0"`

My tests include:

beforeEach(module('templates'));

These give the error:

Module 'templates' is not available!

Running karma with --log-level debug, I do not see any [preprocessor.html2js] entries. (I do get Loading plugin karma-ng-html2js-preprocessor.)

What am I doing wrong?


Solution

  • The issues were that the templates must be listed under files as well, and that the glob pattern in preprocessors must match. This is implied by the documentation.

    files: [
      '../../Scripts/angular-app/directives/*.html',
      // .js files
    ],
    
    preprocessors: {
      '../../Scripts/angular-app/**/*.html': ['ng-html2js']
    },
    

    Note that **/*.html does not match parent directories of the basePath.

    karma start --log-level debug will display DEBUG [preprocessor.html2js] entries when everything is correct.

    I was also able to remove the plugins section.

    To get the correct cache ID, I used:

    ngHtml2JsPreprocessor: {
        // Load this module in your tests of directives that have a templateUrl.
        moduleName: 'templates',
    
        cacheIdFromPath: function (filepath) {
            return filepath.substring(filepath.indexOf('/Scripts/angular-app/'));
        }
    },
    

    If a template references a custom filter, the filter must be loaded in files and the filter's module must be loaded in your directive tests.