angularjs - karma-ng-html2js-预处理器无法正常工作gulp + angular + karma + ng-html2js

标签 angularjs karma-jasmine ng-html2js gulp-karma

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

打包Json文件:

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

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

文件在Build文件中定义,并传递给gulp-karma。以下是定义的文件:
config = {  test: {
          configFile: '.../karma.conf.js',
          depends: [
              .......
          ],
          files: [
            "app/**/*.js",
            'app/**/*.html'
          ]
       }
    }

在我的指令规范中加载模板,如下所示:
beforeEach(module('app'));
beforeEach(module('app/tem/mytemp.html'));

我收到以下错误:
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

在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();

我有什么想念的吗?如何调试并继续解决此问题?

最佳答案

这是我解决完全相同的问题的方法:

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'
]

3)由于gulp-karma会覆盖karma.conf.js的files属性,因此请为您的测试设置更改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'
    }))
  ....
});

希望这会帮助某人。

关于angularjs - karma-ng-html2js-预处理器无法正常工作gulp + angular + karma + ng-html2js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24300540/

相关文章:

angularjs - 使用 $httpBackend 的 Angular 测试失败,出现 "400 thrown"错误

javascript - Karma 没有存储提供程序,但已添加

AngularJS Karma 测试 : Unexpected request: GET. template.html

angularjs - 使用 templateUrl 的指令 - 使用 ng-html2js 进行测试

angularjs - 有没有办法使用#mdDialog 服务触发 mat-dialog 组件?

javascript - 无法更新函数angularjs内的变量值

html - 如何拉伸(stretch) md-nav-item Angular Material

javascript - Angular JS 驳回 $modalInstance : undefined is not a function

javascript - 使用 Angularjs 和 ng-model 进行数据绑定(bind),将毫米转换为英尺和英寸,反之亦然