2

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?

TrueWill
  • 25,132
  • 10
  • 101
  • 150

1 Answers1

6

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.

TrueWill
  • 25,132
  • 10
  • 101
  • 150
  • I am getting my templatesCached and loading the module, but not accessing anything for the directives. I am wondering what cacheIdFromPath did for you? – Winnemucca Mar 10 '16 at 23:09
  • @stevek I believe the goal is to transform the path into one matching a `templateUrl` in a directive. There are other ways to accomplish this, such as `stripPrefix` - see https://github.com/karma-runner/karma-ng-html2js-preprocessor . You could throw a `console.log(filepath)` in to `cacheIdFromPath` to see what you're getting and compare it to your `templateUrl`s. Your function will vary from mine. – TrueWill Mar 11 '16 at 14:27
  • Thanks, that is what I am working on right now. I have a strip Prefix, and the directives seem to follow that path, but when I console.log the file path I each template file is going all the way back to the root of the entire project. I am hoping this is the problem. – Winnemucca Mar 11 '16 at 14:53
  • @stevek I am not sure, but I think `stripPrefix` and `cacheIdFromPath` are mutually exclusive. `cacheIdFromPath` is the ultimate flexibility "do whatever you want" function, and the others are conveniences. You might try removing that function (logging and all). – TrueWill Mar 11 '16 at 15:16
  • Been through and logged everything. I am wondering if you had to reference the files that were $templateCached and turned to js files inside of your files ? – Winnemucca Mar 11 '16 at 18:02
  • @stevek I did need to include the templates used by directives in the `files` list in karma.conf.js. – TrueWill Mar 11 '16 at 19:00