[英]mocha init timeout with mocha-phantomjs
我有以下testrunner.html
:
<html>
<head>
<title>Specs</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="/content/css/mocha.css" />
<script>
function assert(expr, msg) {
if (!expr) throw new Error(msg || 'failed');
}
</script>
<script src="/client/lib/require.js" type="text/javascript" data-main="/client/specs/_runner.js"></script>
</head>
<body>
<div id="mocha"></div>
</body>
</html>
_runner.js
看起來像這樣:
// Configure RequireJS
require.config({
baseUrl: '/client',
urlArgs: "v=" + (new Date()).getTime()
});
// Require libraries
require(['require', 'lib/chai', 'lib/mocha'], function (require, chai) {
// Chai
assert = chai.assert;
should = chai.should();
expect = chai.expect;
// Mocha
mocha.setup('bdd');
// Require base tests before starting
require(['specs/stringcalculator.specs'], function (person) {
mocha.setup({ globals: ['hasCert'] });
// Start runner
if (window.mochaPhantomJS) {
mochaPhantomJS.run();
}
else { mocha.run(); }
});
});
StringCalculator.specs.js
是這樣的:
define(['app/model/StringCalculator'], function () {
describe("StringCalculator", function () {
describe("when an empty string is passed in", function () {
it("returns 0", function () {
var result = StringCalculator.add("");
assert(result === 0);
});
});
describe("when a number is passed in", function () {
it("returns the number", function () {
var result = StringCalculator.add("2");
assert(result === 2);
});
});
describe("when string is passed in", function () {
it("returns NaN", function () {
var result = StringCalculator.add("a");
assert(isNaN(result));
});
});
describe("when '1,2' is passed in", function () {
it("returns 3", function () {
var result = StringCalculator.add("1,2");
assert(result === 3);
});
});
});
});
這是StringCalculator.js
本身(來自摩卡樣本):
define([], function() {
window.StringCalculator = StringCalculator = {
add: function(inputString) {
if (inputString === '') {
return 0;
}
var result = 0;
var inputStrings = inputString.split(',');
for (var i = 0; i < inputStrings.length; i++) {
result += parseInt(inputStrings[i]);
}
return result;
}
}
});
在調用testrunner.html
的瀏覽器中運行規范時,一切都按預期工作。 在OS X上運行mocha-phantomjs client/specs/testrunner.html
時,出現以下錯誤:
Failed to start mocha: Init timeout
我可能在這里失蹤了什么?
我也嘗試了mocha-phantomjs http://httpjs.herokuapp.com
失敗並出現同樣的錯誤。
更新:如果我正在調用mocha-phantomjs http://localhost:81/client/specs/testrunner.html
我也在控制台上收到以下錯誤:
RangeError: Maximum call stack size exceeded.
http://localhost:81/client/lib/chai.js?v=123423553533535:2601
Failed to start mocha: Init timeout
我通過grunt-mocha-phantomjs
npm包運行mocha-phantomjs
時遇到了同樣Failed to start mocha
錯誤。 在這里找到解決方案。
在此重復以供參考:
要使用mocha-phantomjs運行,請更改
mocha.run();
至
if (mochaPhantomJS) {
mochaPhantomJS.run();
}
else {
mocha.run();
}
該文件顯示了如何使用它。
對我來說,NodeJS 0.10.x似乎無法使用它。
切換到NodeJS 0.8.8后,一切都按預期工作。
使用當前版本的mocha-phantomjs和PhantomJS現在一切正常。
感謝您提供此信息,我嘗試了上述內容,但在瀏覽器中說“mochaPhantomJS未定義”失敗了。 根據下面的快速調整,它運作良好:
if(typeof(mochaPhantomJS)!=="undefined")
{
mochaPhantomJS.run();
}
else
{
mocha.run();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.