Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 200
rak Avatar asked Jun 19 '14 06:06

rak


1 Answers

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.

like image 85
vekerdyb Avatar answered Sep 28 '22 07:09

vekerdyb