【问题标题】:mocha init timeout with mocha-phantomjsmocha 初始化超时与 mocha-phantomjs
【发布时间】:2013-03-27 11:12:54
【问题描述】:

我有以下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 本身(来自 mocha 样本):

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

【问题讨论】:

    标签: javascript tdd phantomjs mocha.js


    【解决方案1】:

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

    在此重复以供参考:

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

    mocha.run();
    

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

    【讨论】:

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

    This file 展示了如何使用它。

    对我来说,NodeJS 0.10.x 似乎无法使用它。切换到 NodeJS 0.8.8 后,一切正常。

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

    【讨论】:

    • Node 0.10.x 用户不要完全气馁——我确信当时确实如此。但是今天我在 0.10.13,上面使用 mochaPhantomJS.run() 的解决方案对我有用。
    • 我仍然不时收到随机超时(不是双关语)。也使用最新的 mocha-phantomjs 等,超时为 10000。
    【解决方案3】:

    感谢您提供此信息,我尝试了上述方法,但在浏览器中显示“mochaPhantomJS 未定义”失败。按照下面的快速调整,效果很好:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多