这是虚拟起点文件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.jsvar 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 safarifile:///Users/me/dev/sandbox/test.htmlit works as expected, test pass with no errorwhen I open in chrome or safarihttp://localhost/sandbox/test.htmlI get the following error and the test failError: 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路径抛出检测到的全局泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-28 22:18