繁体   English   中英

用咕噜声和qunit记录

[英]Logging with grunt and qunit

我正在使用grunt / qunit运行javascript单元测试。 有时测试失败是因为源文件中存在语法错误(如果在测试文件中引入了语法错误,则可以正常使用文件信息)。 当发生这种情况时,grunt只会打印行号而不是问题所在的文件。

Running "qunit:all" (qunit) task
Warning: Line 99: Unexpected identifier Use --force to continue.

Aborted due to warnings.

这没有多大帮助,因为我有100个js文件。 我调查过:

https://github.com/gruntjs/grunt-contrib-qunit

并尝试将以下内容添加到我的Gruntfile.js(grunt.event.on):

module.exports = function(grunt) {
    "use:strict";
    var reportDir = "output/reports/"+(new Date()).getTime().toString();
    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),
        qunit: {
            options: {
                '--web-security': 'no',
                coverage: {
                    src: ['../src/**/*.js'],
                    instrumentedFiles: 'output/instrument/',
                    htmlReport: 'output/coverage',
                    coberturaReport: 'output/',
                    linesTresholdPct: 85
                }
            },
            all: ["testsSuites.html"]
        }
    });


    // Has no effect
    grunt.event.on('qunit.error.onError', function (msg, stack) {
        grunt.util._.each(stack, function (entry) {
            grunt.log.writeln(entry.file + ':' + entry.line);
        });
        grunt.warn(msg);
    });     

    grunt.loadNpmTasks('grunt-contrib-qunit');
    grunt.loadNpmTasks('grunt-qunit-istanbul');
    grunt.registerTask('test', ['qunit']);

testsSuites.html包含的位置:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="qunit/qunit.css">
    <script src="qunit/qunit.js"></script>
    <script src="sinonjs/sinon-1.7.3.js"></script>
    <script src="sinonjs/sinon-qunit-1.0.0.js"></script>

    <!-- Sources -->
    <script src="../src/sample.js"></script>

    <!-- Test-->
    <script src="test/sample-test.js"></script>

  </head>
  <body>
    <div id="qunit"></div>
    <div id="qunit-fixture"></div>
    <script>
    </script>
  </body>
</html>

但是仍然没有打印问题所在的源文件。 是不是Grunts手中验证源代码/显示行号/文件,例如语法错误位于何处?

我也试过跑:

grunt test --debug 9

它会打印一些调试信息,但不会显示有关javascript源语法错误的任何信息。

我试图安装JSHint并在我所有的javascript源文件上调用它:

for i in $(find ../src -iname "*.js"); do jshint $i; done

现在我得到了很多错误,但Grunt仍然很高兴。 如果我引入一个简单的语法错误,例如:

(function(){
   var sampleVar 32;

}

在Grunt中引发错误:

Running "qunit:all" (qunit) task
Warning: Line 2: Unexpected number Use --force to continue.

Aborted due to warnings.

它只是在JSHint生成的错误流中消失。 如何从实际使Grunt失败的关键错误中过滤JSHint“警告”?

或者是否应该配置为更详细的输出?

当遇到语法错误时, grunt-contrib-qunit将显示文件名。 拿这个Gruntfile.js简化版本:

module.exports = function(grunt) {
    "use:strict";
    grunt.initConfig({
        qunit: {
            options: { '--web-security': 'no' },
            all: ["testsSuites.html"]
        }
    });

    grunt.loadNpmTasks('grunt-contrib-qunit');
};

运行它会给出您正在寻找的错误:

$ grunt qunit
Running "qunit:all" (qunit) task
Testing testsSuites.html F.
>> global failure
>> Message: SyntaxError: Parse error
>> file:///tmp/src/sample.js:2

Warning: 1/2 assertions failed (17ms) Use --force to continue.

Aborted due to warnings.

您遇到的问题看起来像是grunt-qunit-istanbul的错误(?)。 你得到的警告:

Warning: Line 99: Unexpected identifier Use --force to continue.

是Grunt处理未捕获的异常。 grunt-qunit-istanbul任务正在提出异常。 您可以通过修改原始Gruntfile.js这一行来证明它:

src: ['../src/**/*.js'],

至:

src: ['../src/**/*.js.nomatch'],

这将阻止grunt-qunit-istanbul在Qunit运行之前查找和解析任何Javascript文件。 如果你让Qunit运行,它的错误处理程序会打印出你想要的语法错误的文件名。

唯一的解决方法是我所描述的解决方法,或修补grunt-qunit-istanbul为像Qunit这样的解析错误添加错误处理程序。

修补grunt-qunit-istanbul

抛出异常的函数是Instrumenter.instrumentSync ,它应该这样做:

instrumentSync ( code, filename )

Defined in lib/instrumenter.js:380

synchronous instrumentation method. Throws when illegal code is passed to it

您可以通过包装函数调用来修复它:

diff -r 14008db115ff node_modules/grunt-qunit-istanbul/tasks/qunit.js
--- a/node_modules/grunt-qunit-istanbul/tasks/qunit.js  Tue Feb 25 12:14:48 2014 -0500
+++ b/node_modules/grunt-qunit-istanbul/tasks/qunit.js  Tue Feb 25 12:19:58 2014 -0500
@@ -209,7 +209,11 @@

       // instrument the files that should be processed by istanbul
       if (options.coverage && options.coverage.instrumentedFiles) {
-        instrumentedFiles[fileStorage] = instrumenter.instrumentSync(String(fs.readFileSync(filepath)), filepath);
+        try {
+          instrumentedFiles[fileStorage] = instrumenter.instrumentSync(String(fs.readFileSync(filepath)), filepath);
+        } catch (e) {
+          grunt.log.error(filepath + ': ' + e);
+        }
       }

       cb();

然后测试将继续运行(并通知您语法错误):

$ grunt qunit
Running "qunit:all" (qunit) task
>> /tmp/src/sample.js: Error: Line 2: Unexpected number
Testing testsSuites.html F.
>> global failure
>> Message: SyntaxError: Parse error
>> file:///tmp/src/sample.js:2

Warning: 1/2 assertions failed (19ms) Use --force to continue.

Aborted due to warnings.

我过去曾经使用过grunt-contrib-qunit,但我从未尝试过这样的事情。 你面临的问题是相当有趣的,因为文档提到事件qunit.error.onError应该由grunt发出,但它不会发生在你身上。

我使用jquery模板创建了一个新项目并更改了代码,以便我的测试失败。 之后我写了下面的代码:

grunt.event.on('qunit.error.onError', function(message, stackTrace) {
  grunt.file.write('log/qunit-error.log', message);
});

当我运行命令grunt ,我没有收到文件中的输出。 为了检查这一点,我对事件进行了更改:

grunt.event.on('qunit.log', function(result, actual, expected, message, source) {
  grunt.file.write('log/qunit-error.log', message);
});

现在,这段代码确实在我的文件中给了我错误消息,但它没用,因为我无法得到堆栈跟踪或确切的错误消息。

在此之后,我虽然阅读了源代码,但这是我发现的:

phantomjs.on('error.onError', function (msg, stackTrace) {
  grunt.event.emit('qunit.error.onError', msg, stackTrace);
});

仅当phantomjs抛出错误时才会发出grunt事件。

目前我不确定如何在没有任何浏览器相关测试的情况下测试简单的JavaScript文件时出现phantomjs错误。 这是我迄今为止的分析,我希望这对你有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM