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

标签 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!';
  }
};

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

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

引用:Mocha 1.2.0 launch notice

关于javascript - 为什么浏览器中的 mocha 抛出从 url 而不是从 unc 路径检测到的全局泄漏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11039811/

相关文章:

javascript - jQuery 选择器返回重复元素

javascript - 正确使用 MutationObserver

javascript - 如何将 URL 从图像的 src 属性复制到 jQuery 中的 href 属性?

haskell - 如何运行作为已安装/正在安装的 Cabal 包的一部分的测试?

node.js - 运行 docker 容器进行测试

javascript - AngularJS:日期输入仅在 Chrome 中显示

Java 测试 : Massively generate mock objects by running your code

testing - ember-simple-auth,验收测试和等待异步操作

node.js - 如何使用 mocha 在 ReactJS 中测试此异步方法调用

node.js - 在 node.js 服务器上使用 supertest/superagent 读取响应输出缓冲区/流