Javascript 为什么浏览器中的mocha会抛出从url而不是unc路径检测到的全局泄漏?

Javascript 为什么浏览器中的mocha会抛出从url而不是unc路径检测到的全局泄漏?,javascript,testing,mocha.js,Javascript,Testing,Mocha.js,我正在创建一个javascript库,希望使用BDD,所以我在mocha尝试了一下,但我无法让它工作。我希望在客户端上使用该库,因此我假设它可以从可浏览的url运行,在web连接上下文中运行,而不仅仅是从unc路径运行沙箱 以下是虚拟起点文件test/test.foobar.js var assert = chai.assert; var foobar = { sayHello: function() { return 'Hello World!'; } }; describ

我正在创建一个javascript库,希望使用BDD,所以我在mocha尝试了一下,但我无法让它工作。我希望在客户端上使用该库,因此我假设它可以从可浏览的url运行,在web连接上下文中运行,而不仅仅是从unc路径运行沙箱

以下是虚拟起点文件test/test.foobar.js

var assert = chai.assert;

var foobar = {
  sayHello: function() {
    return 'Hello World!';
  }
};

describe('Foobar', function() {
  describe('#sayHello()', function() {
      it('should work with assert', function() {
      assert.equal(foobar.sayHello(), 'Hello World!');
    });

  });
});
下面是触发测试的html页面test.html

<html>
<head>
  <meta charset="utf-8">
  <title>Mocha Tests</title>
  <link rel="stylesheet" href="testing/mocha.css" />
  <script src="testing/jquery.js"></script>
  <script src="testing/mocha.js"></script>
  <script>mocha.setup('bdd')</script>
  <script src="testing/chai.js"></script>
  <script src="test/test.foobar.js"></script>
  <script> $(function() { mocha.run(); }) </script>
</head>
<body>
  <div id="mocha"></div>
</body>
</html>
它按预期工作,测试通过,没有错误

当我在chrome或safari中打开时

file:///Users/me/dev/sandbox/test.html
http://localhost/sandbox/test.html
我得到以下错误,测试失败

Error: global leak detected: script1339700707078
    at Runner.checkGlobals (http://localhost/sandbox/testing/mocha.js:3139:21)
    at Runner.<anonymous> (http://localhost/sandbox/testing/mocha.js:3054:44)
    at Runner.emit (http://localhost/sandbox/testing/mocha.js:235:20)
    at http://localhost/sandbox/testing/mocha.js:3360:14
    at Test.run (http://localhost/sandbox/testing/mocha.js:3003:5)
    at Runner.runTest (http://localhost/sandbox/testing/mocha.js:3305:10)
    at http://localhost/sandbox/testing/mocha.js:3349:12
    at next (http://localhost/sandbox/testing/mocha.js:3233:14)
    at http://localhost/sandbox/testing/mocha.js:3242:7
    at next (http://localhost/sandbox/testing/mocha.js:3192:23)
错误:检测到全局泄漏:script1339707078
在Runner.checkGlobals(http://localhost/sandbox/testing/mocha.js:3139:21)
在跑步者。(http://localhost/sandbox/testing/mocha.js:3054:44)
至少(http://localhost/sandbox/testing/mocha.js:235:20)
在http://localhost/sandbox/testing/mocha.js:3360:14
在测试运行时(http://localhost/sandbox/testing/mocha.js:3003:5)
在Runner.runTest(http://localhost/sandbox/testing/mocha.js:3305:10)
在http://localhost/sandbox/testing/mocha.js:3349:12
接下来(http://localhost/sandbox/testing/mocha.js:3233:14)
在http://localhost/sandbox/testing/mocha.js:3242:7
接下来(http://localhost/sandbox/testing/mocha.js:3192:23)

有人能给出一个解释和更好的解决方案吗

我在safari中找到了一个解决方案,解决了这个问题。。。 替换

<script> $(function() { mocha.run(); }) </script>
$(函数(){mocha.run();})


onload=函数(){
var runner=mocha.run();
};

…但仍然在chrome中得到错误:-(

这是将jQuery与mocha一起使用的问题。jQuery创建具有唯一id的全局变量…在您的示例中,
script133…
。最近在mocha 1.2中发布,您可以设置通配符忽略

$(function(){
  mocha
    .globals([ 'script*' ]) // acceptable globals
    .run();
});
确保您是最新的,并进行适当的配置


参考:

如果在onload标签上方的脚本标签中添加以下内容,它应该在chrome中修复它(globals通常也很有用):mocha.setup({ui:'bdd',globals:['script*']})
$(function(){
  mocha
    .globals([ 'script*' ]) // acceptable globals
    .run();
});