【问题标题】:why mocha in browser throw global leak detected from a url but not from a unc path?为什么浏览器中的 mocha 会抛出从 url 而不是从 unc 路径检测到的全局泄漏?
【发布时间】:2012-06-14 19:12:37
【问题描述】:

我正在创建一个 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

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

当我在 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)

有人可以解释一下,更好的解决方案吗?

【问题讨论】:

    标签: javascript testing mocha.js


    【解决方案1】:

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

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

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

    参考:Mocha 1.2.0 launch notice

    【讨论】:

      【解决方案2】:

      我找到了解决 safari 中问题的解决方案... 替换

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

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

      ...但仍然在 chrome 中出现错误:-(

      【讨论】:

      • 如果你在 onload 上面的脚本标签中添加以下内容,它应该在 chrome 中修复它(全局也很有用): mocha.setup({ ui: 'bdd', globals: [ '脚本*'] })
      猜你喜欢
      • 2012-08-19
      • 2012-01-11
      • 1970-01-01
      • 2021-06-27
      • 2015-08-10
      • 1970-01-01
      • 1970-01-01
      • 2012-05-19
      • 1970-01-01
      相关资源
      最近更新 更多