为什么浏览器中的Mocha会从URL而不是从UNC路径抛出检测到的全局泄漏? [英] why mocha in browser throw global leak detected from a url but not from a unc path?

查看:78
本文介绍了为什么浏览器中的Mocha会从URL而不是从UNC路径抛出检测到的全局泄漏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个JavaScript库,并希望使用BDD,因此我尝试了摩卡,但我无法使其正常运行.我希望在客户端上使用该库,因此我假设从可浏览的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

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

当我打开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)

有人可以解释一下,更好地解决吗?

解决方案

这是将jQuery与Mocha结合使用的问题. jQuery创建具有唯一ID的全局变量...在您的情况下为script133....最近在mocha 1.2中发布的您可以设置通配符忽略...

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

确保您是最新的,并进行适当的配置.

参考:摩卡1.2.0发布通知

I'm creating a javascript library and want to use BDD, so I'm giving a try at mocha and I can't make it work. I want that library to be used on the client, so I'm assuming that it make sense to have it running from a browsable url, to be in a context of web connection, and not just a sandbox from a unc path.

here is the dummy starting point file 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!');
    });

  });
});

and here is the html page that trigger the test, 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>

when I open in chrome or safari

file:///Users/me/dev/sandbox/test.html

it works as expected, test pass with no error

when I open in chrome or safari

http://localhost/sandbox/test.html

I get the following error and the test fail

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)

can someone have an explanation, and better a solution?

解决方案

This was an issue with using jQuery with mocha. jQuery creates global variables that have a unique id... in your case script133.... Recently released in mocha 1.2 you can set up wildcard ignores...

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

Make sure you are up to date, and configure appropriately.

Reference: Mocha 1.2.0 launch notice

这篇关于为什么浏览器中的Mocha会从URL而不是从UNC路径抛出检测到的全局泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆