我想使用jscover作为覆盖率报告,我遵循手册但获得例外:
TypeError:undefined不是对象(评估' phantom.args [0]')
以下是我正在使用的文件:
js-code-coverage.xml内容:
<!-- running jscover for the code coverage -->
<target name="startjscoverserver">
<java jar="${jsCover}/JSCover-all.jar" fork="true" spawn="true">
<jvmarg value="-Dfile.encoding=UTF-8" />
<arg value="-ws" />
<arg value="--document-root=${documentRoot}" />
<arg value="--report-dir=${reports}/coverage" />
<arg value="--no-instrument=${documentRoot}/resources/public"/>
<arg value="--no- instrument=${documentRoot}/resources/knockoutComponents"/>
<arg value="--no- instrument=${documentRoot}/resources/public/common"/>
<arg value="--no-function"/>
<arg value="--port=8080" />
</java>
<waitfor maxwait="5" maxwaitunit="second" checkevery="250" checkeveryunit="millisecond" timeoutproperty="failed">
<http url="http://localhost:8080/jscoverage.html" />
</waitfor>
<fail if="failed" />
<echo message="JSCover has been run. URL: http://localhost:8080/jscoverage.html" />
</target>
<!-- starting the code coverage -->
<target name="jscodecoverage">
<exec executable="${phantomJS}/${phantomPath}" failonerror="false">
<arg line="${phantomJS}/phantom-jscover.js" />
<arg line="http://localhost:8080/jscoverage.html?http://localhost:7001/myproject/JasmineSpecRunner.html" />
</exec>
</target>
和phantom-jscover.js:
(function() {
function waitFor(testFx, onReady) {
var condition = false, interval = setInterval(function() {
if (!condition) {
condition = (typeof (testFx) === 'string' ? eval(testFx)
: testFx());
} else {
if (typeof (onReady) === 'string') {
eval(onReady);
} else {
onReady();
}
clearInterval(interval);
}
}, 100);
}
var url = phantom.args[0];
var page = require('webpage').create();
var fs = require('fs');
page.onCallback = function(data) {
if ('jasminelog' === data.message) {
fs.write('/dev/stdout', data.data.message, 'w');
}
};
page.open(url, function(status) {
if (status !== "success") {
phantom.exit(1);
} else {
waitFor(function() {
return page.evaluate(function() {
var reporter = window.jsApiReporter;
if (window.frames[0]) {
reporter = window.frames[0].jsApiReporter;
}
return reporter && reporter.finished;
});
}, function() {
page.evaluate(function() {
var jscoverage_report = window.jscoverage_report;
if (window.frames[0]) {
jscoverage_report = window.frames[0].jscoverage_report;
}
if (jscoverage_report) {
jscoverage_report('phantom');
}
});
phantom.exit(0);
});
}
});
}).call(this);
答案 0 :(得分:1)
它可能是为较旧版本的PhantomJS编写的。 phantom.args
is deprecated
解决方法是使用系统模块:
var url = require('system').args[1];
注意:
第一个始终是脚本名称,然后是后续参数。