使用jslint进行语法和风格的检查,可以预防错误。
vim配置如下:
" jslint map! lint !D:\jsl-0.3.0\jsl.exe -process %
打开的JS文本中:lint便可以运行jslint。
qunit是jquery团队开发的js测试框架,使用很简单。下面是我的项目中用到的一段测试代码:
<script src="qunit-1.12.0.js"></script>
<script src="jquery-1.7.2.js"></script>
<script src="../jquery.column.picker.js"></script>
<script>
test( "test basic envirenment", function() {
ok(typeof jQuery === 'function', "jQuery is a function");
ok(typeof $("#monthTable").pickout === 'function', "$('#monthTable').pickout is a function");
});
test( "test pickout selector function", function() {
throws( function() {$("#monthTable").pickout()}, /parameter/, "raised error message contains 'parameter' when parameter is empty");
ok($.isArray( $("#monthTable").pickout([0, 1, 3]) ), "column index array parameter is ok");
ok($.isArray( $("#monthTable").pickout("th[name=January],th[name=March]") ), "string selector parameter is ok");
});
test( "test pickout show and hide function, compatible to old", function() {
$("#monthTable").pickout({picked: [0, 1, 2]});
ok($("#monthTable").find("tr th:eq(0)").is(":visible"), "pickout show and hide ok");
});
</script>