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();
}

相关文章

前言 做过web项目开发的人对layer弹层组件肯定不陌生,作为l...
前言 前端表单校验是过滤无效数据、假数据、有毒数据的第一步...
前言 图片上传是web项目常见的需求,我基于之前的博客的代码...
前言 导出Excel文件这个功能,通常都是在后端实现返回前端一...
前言 众所周知,js是单线程的,从上往下,从左往右依次执行,...
前言 项目开发中,我们可能会碰到这样的需求:select标签,禁...