为什么在摩卡扔浏览器从URL而不是从一个UNC路径检测全球泄漏?(why mocha in brow

2019-07-29 22:11发布

我创建一个JavaScript库,并且希望使用BDD,所以我给的摩卡一个尝试,我不能使它发挥作用。 我想在客户端上使用该库,所以我假设它有意义,我们将会从一个可浏览网址运行,将在网络连接的情况下,而不是仅仅从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!');
    });

  });
});

这里是触发测试,在test.html的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

它按预期工作,测试通过,没有错误

当我在Chrome或Safari打开

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)

有人可以有一个解释,以及更好的解决方案?

Answer 1:

这与使用jQuery与摩卡的问题。 jQuery的创建具有一个唯一的ID全局变量...在你的情况script133... 。 最近公布的摩卡1.2你可以设置通配符忽略...

$(function(){
  mocha
    .globals([ 'script*' ]) // acceptable globals
    .run();
});

确保您是最新的,并进行适当的设置。

参考: 摩卡1.2.0发布通知



Answer 2:

我发现,修复Safari中的问题的解决方案...取代

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

通过

<script>
      onload = function(){
        var runner = mocha.run();
      };
</script>

...但仍获得铬错误:-(



文章来源: why mocha in browser throw global leak detected from a url but not from a unc path?