Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用grunt和qunit进行日志记录_Javascript_Unit Testing_Gruntjs - Fatal编程技术网

Javascript 使用grunt和qunit进行日志记录

Javascript 使用grunt和qunit进行日志记录,javascript,unit-testing,gruntjs,Javascript,Unit Testing,Gruntjs,我正在使用grunt/qunit运行javascript单元测试。有时测试失败是因为源文件中的语法错误(如果测试文件中引入了语法错误,则可以使用文件信息)。发生这种情况时,grunt只打印行号,而不是问题所在的文件 Running "qunit:all" (qunit) task Warning: Line 99: Unexpected identifier Use --force to continue. Aborted due to warnings. 这没有多大帮助,因为我有100个j

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

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

Aborted due to warnings.
这没有多大帮助,因为我有100个js文件。我研究过:

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

其中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>
它会打印一些调试信息,但不会打印有关javascript源中语法错误的任何信息

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

for i in $(find ../src -iname "*.js"); do jshint $i; done
现在我犯了很多错误,但格伦特仍然很高兴。如果我引入了一个简单的语法错误,例如:

(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“警告”


还是应该为更详细的输出配置qunit?

我过去使用过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);
});
现在,这段代码确实给了我文件中的错误消息,但它没有用,因为我无法获得stacktrace或确切的错误消息

在这之后,我想到了阅读来源,这是我发现的:

phantomjs.on('error.onError', function (msg, stackTrace) {
  grunt.event.emit('qunit.error.onError', msg, stackTrace);
});
grunt事件仅在phantomjs抛出错误时发出


目前,我不确定在测试一个简单的JavaScript文件时,如果没有任何与浏览器相关的测试,怎么会出现phantomjs错误。这是我到目前为止的分析,我希望这对您有所帮助。

grunt contrib qunit在遇到语法错误时将显示文件名。以您的
grunfile.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
中的一个bug(?)。您得到的警告是:

Warning: Line 99: Unexpected identifier Use --force to continue.
Grunt正在处理未捕获的异常。
grunt qunit istanbul
任务引发异常。您可以通过修改原始
grunfile.js
中的这一行来证明这一点:

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

这将阻止
grunt qunit伊斯坦布尔
在运行qunit之前查找和解析任何Javascript文件。如果让Qunit运行,它的错误处理程序会打印出包含语法错误的文件名,如您所愿

唯一的修复方法是我所描述的解决方法,或者修补
grunt qunit伊斯坦布尔
,像qunit那样为解析错误添加错误处理程序

伊斯坦布尔格伦特库尼酒店 引发异常的函数是,它应该执行以下操作:

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();
然后测试将继续运行(并通知您语法错误):


您正在使用
--debug 9
标志运行grunt吗?您可以在源文件上运行jshint。它将修复你的语法错误。修复?我猜你是说通知?jshint.com/docs似乎只适用于单个文件。但是,也许jslint支持对js文件的文件夹github.com/reid/node-jslint等运行语法检查。您是否有一个展示这些问题的示例项目可以尝试?在运行测试时,您如何包括源文件?作为
all_tests.html
中的
标记,还是其他地方?
src: ['../src/**/*.js.nomatch'],
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.