u12*_*123 5 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.
Run Code Online (Sandbox Code Playgroud)
这没有多大帮助,因为我有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']);
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
但是仍然没有打印问题所在的源文件.是不是Grunts手中验证源代码/显示行号/文件,例如语法错误位于何处?
我也试过跑:
grunt test --debug 9
Run Code Online (Sandbox Code Playgroud)
它会打印一些调试信息,但不会显示有关javascript源语法错误的任何信息.
我试图安装JSHint并在我所有的javascript源文件上调用它:
for i in $(find ../src -iname "*.js"); do jshint $i; done
Run Code Online (Sandbox Code Playgroud)
现在我得到了很多错误,但Grunt仍然很高兴.如果我引入一个简单的语法错误,例如:
(function(){
var sampleVar 32;
}
Run Code Online (Sandbox Code Playgroud)
在Grunt中引发错误:
Running "qunit:all" (qunit) task
Warning: Line 2: Unexpected number Use --force to continue.
Aborted due to warnings.
Run Code Online (Sandbox Code Playgroud)
它只是在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');
};
Run Code Online (Sandbox Code Playgroud)
运行它会给出您要查找的错误:
$ 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.
Run Code Online (Sandbox Code Playgroud)
您遇到的问题看起来是grunt-qunit-istanbul. 您收到的警告:
Warning: Line 99: Unexpected identifier Use --force to continue.
Run Code Online (Sandbox Code Playgroud)
Grunt 正在处理未捕获的异常。任务引发异常grunt-qunit-istanbul。您可以通过修改原始文件中的这一行来证明这一点Gruntfile.js:
src: ['../src/**/*.js'],
Run Code Online (Sandbox Code Playgroud)
到:
src: ['../src/**/*.js.nomatch'],
Run Code Online (Sandbox Code Playgroud)
这将阻止grunt-qunit-istanbul在 Qunit 运行之前查找和解析任何 Javascript 文件。如果您让 Qunit 运行,它的错误处理程序会打印出带有您想要的语法错误的文件名。
唯一的解决方法是我所描述的解决方法,或者修补grunt-qunit-istanbul以添加错误处理程序来处理解析错误,就像 Qunit 所做的那样。
引发异常的函数是Instrumenter.instrumentSync,它应该执行以下操作:
instrumentSync ( code, filename )
Defined in lib/instrumenter.js:380
synchronous instrumentation method. Throws when illegal code is passed to it
Run Code Online (Sandbox Code Playgroud)
您可以通过包装函数调用来修复它:
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();
Run Code Online (Sandbox Code Playgroud)
然后测试将继续运行(并通知您语法错误):
$ 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.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1367 次 |
| 最近记录: |