【发布时间】:2014-02-16 13:27:14
【问题描述】:
我正在使用 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']);
testSuites.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
现在我遇到了很多错误,但 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“警告”?
还是应该为更详细的输出配置 qunit?
【问题讨论】:
-
你是否对
--debug 9标志感到不满? -
您可以在源文件上运行 jshint。它会修复你的语法错误。
-
修复?我猜你的意思是通知? jshint.com/docs 似乎只适用于单个文件。但也许 jslint 支持在例如 js 文件 github.com/reid/node-jslint 的文件夹上运行语法检查
-
您是否有一个展示这些问题的示例项目可以尝试?
-
在运行测试时如何包含源文件?作为
all_tests.html中的<script>标签,还是其他地方?
标签: javascript unit-testing gruntjs