0

我从 Github 下载了该ng-boilerplate应用程序,我正在通过它来帮助我了解 AngularJS 和测试。

我做的第一件事就是尝试了解 Karma 测试的工作原理。所以我在 Karma 文件夹中打开了一个命令提示符,然后输入:

karma start karma-unit.tpl.js

我希望返回完美的测试结果(因为我没有触及任何代码)。但是,相反,我收到此错误:

C:\Users\Imray\stuff\Angular JS\ng-boilerplate\karma\karma-unit.tpl.js:12
      <% scripts.forEach( function ( file ) { %>'<%= file %>',
      ^
ERROR [config]: Invalid config file!
  SyntaxError: Unexpected token <
    at Module._compile (module.js:439:25)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.parseConfig (C:\Users\Imray\stuff\Angular JS\ng-boilerplate\node_modules\karma\lib\config.js:250:22)
    at Object.exports.start (C:\Users\Imray\stuff\Angular JS\ng-boilerplate\node_modules\karma\lib\server.js:286:20)
    at Object.exports.run (C:\Users\Imray\stuff\Angular JS\ng-boilerplate\node_modules\karma\lib\cli.js:229:25)
    at requireCliAndRun (C:\Users\Imray\AppData\Roaming\npm\node_modules\karma-cli\bin\karma:24:16)

为什么会这样?我究竟做错了什么?

4

2 回答 2

0

嗯不确定,但也许你必须在运行之前运行 Grunt 任务,karma start karma-unit.tpl.js因为那里有一些未处理的字符串。

<% scripts.forEach( function ( file ) { %>'<%= file %>',看起来不像有效的 javascript 但更像是构建字符串

看看这个答案

于 2015-01-11T15:22:41.223 回答
0

您缺少 karmaconfig 任务:

/**
 * In order to avoid having to specify manually the files needed for karma to
 * run, we use grunt to manage the list for us. The `karma/*` files are
 * compiled as grunt templates for use by Karma. Yay!
 */
grunt.registerMultiTask( 'karmaconfig', 'Process karma config templates', function () {
var jsFiles = filterForJS( this.filesSrc );

grunt.file.copy( 'karma/karma-unit.tpl.js', grunt.config( 'build_location' ) + '/karma-unit.js', {
    process: function ( contents, path ) {
        return grunt.template.process( contents, {
            data: {
                scripts: jsFiles
            }
        });
    }
});

});

或者您只是删除了它,或者忘记在您的业力任务之前添加它:

grunt.registerTask( 'build', [
        'clean', 'html2js', 'jshint:source', 'jshint:test', 'karmaconfig',
        'karma:continuous', 'karma:coverage'
    ]);

(删除了一些使其更容易查看的任务。karmaconfig 任务还依赖于 filterForJs 函数。但这都包含在 ng-boilerplate 中,所以我没有在这里明确添加)

于 2015-07-08T11:50:17.427 回答