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

rak*_*rak 6 angularjs ng-html2js karma-jasmine gulp-karma

我无法使karma-ng-html2js-preprocessor适用于外部模板.

包Json文件:

    .....
    "gulp-karma": "*",
    "karma-coverage": "*",
    "karma-jasmine": "*",
    "karma-ng-html2js-preprocessor": "*",
    "karma-phantomjs-launcher": "*",
    .....
Run Code Online (Sandbox Code Playgroud)

Karma配置文件:

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

文件在Build文件中定义并传递给gulp-karma.以下是已定义的文件:

config = {  test: {
          configFile: '.../karma.conf.js',
          depends: [
              .......
          ],
          files: [
            "app/**/*.js",
            'app/**/*.html'
          ]
       }
    }
Run Code Online (Sandbox Code Playgroud)

在我的指令规范中加载模板,如下所示:

beforeEach(module('app'));
beforeEach(module('app/tem/mytemp.html'));
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

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
Run Code Online (Sandbox Code Playgroud)

在karma中,debug.html html文件加载在链接标记输出中:

<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();
Run Code Online (Sandbox Code Playgroud)

我错过了什么吗?如何调试并继续解决此问题?

vek*_*dyb 11

这是我如何解决完全相同的问题:

1)npm install karma-ng-html2js-preprocessor --save-dev(你已经这样做了)

2)在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'
]
Run Code Online (Sandbox Code Playgroud)

3)由于filesgulp -karma会覆盖karma.conf.js 的属性,所以更改你的测试设置的gulp 任务配置(我有两个:一个test运行测试一个,另一个运行tdd连续测试)到类似的东西这个:

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'
    }))
  ....
});
Run Code Online (Sandbox Code Playgroud)

希望这会对某人有所帮助.


Nex*_*xeh 2

我对此也是新手,我正在谷歌搜索另一个问题,但我认为我比你更进一步(我也意识到这个任务有点老了)。

我所做的是对 Karma 配置文件的 ngHtml2JsPreprocessor 部分进行以下更改

ngHtml2JsPreprocessor: {
    stripPrefix: 'app/',

    // ADDED THIS: the name of the Angular module to create
    moduleName: "my.templates"
}
Run Code Online (Sandbox Code Playgroud)

然后在我的测试中我引用了该模块名称而不是 HTML 名称。

beforeEach(module('my.templates'));
Run Code Online (Sandbox Code Playgroud)

我希望这会有所帮助,即使已经晚了。或者其他人在搜索时发现这些信息有用。