问题 使用mocha-phantomjs进行mocha初始化超时


我有以下内容 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一切都按预期工作。 跑步时 mocha-phantomjs client/specs/testrunner.html 在OS X上,我收到以下错误:

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

7199
2018-03-27 11:12


起源



答案:


我也是这样 Failed to start mocha 运行时出错 mocha-phantomjs 通过 grunt-mocha-phantomjs npm包。找到了解决方案 这里

在此重复参考:

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

mocha.run();

if (mochaPhantomJS) {
  mochaPhantomJS.run();
}
else {
  mocha.run();
}

9
2017-08-06 21:12



嗨,我也看到了这个问题。但我只是使用npm安装了Mocha-phantomjs和phantomjs。我不知道在哪里可以找到SpecRunner.js以及它到底发生了什么。你能解释一下吗? - EternallyCurious
@EternallyCurious,我不确定你的问题是否属于这一范围。打开一个新的? - mnoble01
链接现在 gist.github.com/michaelcox/3800736/#gistcomment-859304 (#comment已更改为#gistcomment) - John-Philip


答案:


我也是这样 Failed to start mocha 运行时出错 mocha-phantomjs 通过 grunt-mocha-phantomjs npm包。找到了解决方案 这里

在此重复参考:

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

mocha.run();

if (mochaPhantomJS) {
  mochaPhantomJS.run();
}
else {
  mocha.run();
}

9
2017-08-06 21:12



嗨,我也看到了这个问题。但我只是使用npm安装了Mocha-phantomjs和phantomjs。我不知道在哪里可以找到SpecRunner.js以及它到底发生了什么。你能解释一下吗? - EternallyCurious
@EternallyCurious,我不确定你的问题是否属于这一范围。打开一个新的? - mnoble01
链接现在 gist.github.com/michaelcox/3800736/#gistcomment-859304 (#comment已更改为#gistcomment) - John-Philip


这个文件 展示了如何使用它。

对我来说,NodeJS 0.10.x似乎无法使用它。切换到NodeJS 0.8.8后,一切都按预期工作。

使用当前版本的mocha-phantomjs和PhantomJS现在一切正常。


1
2018-04-06 09:20



节点0.10.x用户并不完全气馁 - 我确信当时这是真的。但今天我在0.10.13并且上面的解决方案使用mochaPhantomJS.run()为我工作。 - laurelnaiad
我仍然会不时获得随机超时(没有双关语意)。使用最新的mocha-phantomjs等,超时10000。 - Till


感谢您提供此信息,我尝试了上述内容,但在浏览器中说“mochaPhantomJS未定义”失败了。根据下面的快速调整,它运作良好:

if(typeof(mochaPhantomJS)!=="undefined")
{
  mochaPhantomJS.run();
}
else
{
  mocha.run();
}

0
2018-05-11 20:39