首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用mocha-phantomjs的mocha初始化超时

使用mocha-phantomjs的mocha初始化超时
EN

Stack Overflow用户
提问于 2013-03-27 19:12:55
回答 2查看 5.3K关注 0票数 11

我有以下testrunner.html

代码语言:javascript
复制
<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如下所示:

代码语言:javascript
复制
// 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是这样的:

代码语言:javascript
复制
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本身(来自摩卡示例):

代码语言:javascript
复制
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,控制台上也会出现以下错误:

代码语言:javascript
复制
RangeError: Maximum call stack size exceeded.

http://localhost:81/client/lib/chai.js?v=123423553533535:2601
Failed to start mocha: Init timeout
EN

回答 2

Stack Overflow用户

发布于 2013-08-07 05:12:32

当通过grunt-mocha-phantomjs npm包运行mocha-phantomjs时,我得到了相同的Failed to start mocha错误。找到解决方案here

在这里重复一下以供参考:

要使用mocha-phantomjs运行,请更改

代码语言:javascript
复制
mocha.run();

代码语言:javascript
复制
if (mochaPhantomJS) {
  mochaPhantomJS.run();
}
else {
  mocha.run();
}
票数 9
EN

Stack Overflow用户

发布于 2015-05-12 04:39:24

感谢这些信息,我尝试了上面的方法,但在浏览器中显示"mochaPhantomJS is undefined“时失败了。一个快速调整,如下所示,它很好用:

代码语言:javascript
复制
if(typeof(mochaPhantomJS)!=="undefined")
{
  mochaPhantomJS.run();
}
else
{
  mocha.run();
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15657458

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档