javascript – mocha init超时与mocha-phantomjs

前端之家收集整理的这篇文章主要介绍了javascript – mocha init超时与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时,会收到以下错误信息:

无法启动mocha:初始化超时

我可能在这里失踪?

我也试过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时,无法启动摩卡错误.找到解决方here.

重复参考:

要运行mocha-phantomjs,更改

mocha.run();

if (mochaPhantomJS) {
  mochaPhantomJS.run();
}
else {
  mocha.run();
}
原文链接:https://www.f2er.com/js/155242.html

猜你在找的JavaScript相关文章