我正在使用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>但是问题所在的源文件仍然没有打印出来。是否已经失去了验证源代码/显示行号/文件的能力,例如是否存在语法错误?
我也试过跑步:
grunt test --debug 9它打印一些调试信息,但不输出有关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?
发布于 2014-02-25 00:35:31
当遇到语法错误时,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那样添加一个用于解析错误的错误处理程序。
修补咕噜声-qunit-伊斯坦布尔
抛出异常的函数是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.发布于 2014-02-24 19:23:12
我以前用过“咕噜”,但我从来没有尝试过这样的方法。您所面临的问题相当有趣,因为文档提到事件qunit.error.onError应该是由咕噜发出的,但它不会发生在您身上。
我使用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);
});只有在幻影引发错误时才会发出咕噜事件。
目前,我不知道如何在测试一个简单的JavaScript文件时得到一个幻影错误,而没有任何浏览器相关的测试。到目前为止,这是我的分析,我希望这能对你有所帮助。
https://stackoverflow.com/questions/21811719
复制相似问题