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

 刘少静mm_527 发布于 2023-01-08 15:43

我无法使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文件加载在链接标记输出中:




        
        
    
1 个回答
  • 这是我如何解决完全相同的问题:

    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)由于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'
        }))
      ....
    });
    

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

    2023-01-08 15:45 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有