摩卡中的全局泄漏错误
当我尝试打开 APN 连接时遇到全局泄漏错误时,我正在尝试对 Apple 推送通知库进行单元测试。
这是我的配置错误还是node-apn或mocha中的错误?
我不确定我是否理解 checkGlobals 在做什么...它只是检查是否设置了任何全局变量?
0) Feed "before all" hook:
Error: global leak detected: hasCert
at Runner.checkGlobals (/usr/lib/node_modules/mocha/lib/runner.js:96:21)
at Runner.<anonymous> (/usr/lib/node_modules/mocha/lib/runner.js:41:44)
at Runner.emit (events.js:64:17)
at /usr/lib/node_modules/mocha/lib/runner.js:159:12
at Hook.run (/usr/lib/node_modules/mocha/lib/runnable.js:114:5)
at next (/usr/lib/node_modules/mocha/lib/runner.js:157:10)
at Array.<anonymous> (/usr/lib/node_modules/mocha/lib/runner.js:165:5)
at EventEmitter._tickCallback (node.js:126:26)
I was trying to unit test the apple push notification library when I got a global leak error trying to open up an APN connection.
Is that a configuration error on my part or an error in node-apn or mocha?
I'm not sure I understand what checkGlobals is doing... is it just checking to see if any global variable are being set?
0) Feed "before all" hook:
Error: global leak detected: hasCert
at Runner.checkGlobals (/usr/lib/node_modules/mocha/lib/runner.js:96:21)
at Runner.<anonymous> (/usr/lib/node_modules/mocha/lib/runner.js:41:44)
at Runner.emit (events.js:64:17)
at /usr/lib/node_modules/mocha/lib/runner.js:159:12
at Hook.run (/usr/lib/node_modules/mocha/lib/runnable.js:114:5)
at next (/usr/lib/node_modules/mocha/lib/runner.js:157:10)
at Array.<anonymous> (/usr/lib/node_modules/mocha/lib/runner.js:165:5)
at EventEmitter._tickCallback (node.js:126:26)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
是的,Mocha 具有全局泄漏检测机制,如果您的测试代码引入全局变量,该机制会发出警报并失败。
如果
hasCert
在库中声明,并且您无法控制其创建,则可以告诉 Mocha 忽略它。在命令行上,
引用文档:
在浏览器中:
Yes, Mocha features a global leak detection mechanism which alerts and fails if your code under test introduces global variables.
If
hasCert
is declared in a library and you have no control over its creation, you can tell Mocha to ignore it.On the command line,
To quote the documentation:
In a browser:
您还可以通过传递以下内容来禁用全局泄漏检测:
在浏览器中:
You can also disable global leak detection by passing:
In a browser:
我也遇到了这个问题,你可能像我一样忘记了一个 var 语句,这在 JS 中意味着将创建一个全局变量。
您可能必须自己寻找它,具体取决于您构建应用程序的方式,并且希望这不是导致此问题的第三方代码。 :P
您应该使用 JSLint 或 JSHint 通过您的项目,他们应该帮助发现源代码(如果它位于代码中的任何位置)。
I ran into this problem as well, you probably forgot a
var
statement somewhere like I did, which in JS means that a global variable will be created.You may have to hunt it down yourself depending on how you structured your app, and hopefully it's not a 3rd-party bit of code that's causing this. :P
You should use JSLint or JSHint through your project, they should help uncover the source if it's anywhere in your code.
如果您在调用构造函数时忘记了 new ,也可能会发生这种情况。在这种情况下,
this
是全局对象,因此构造函数中引入的任何属性都将添加到全局对象中。这个问题不应该长期被发现,但这是一个有趣的测试失败。
This can also happen if you forget
new
in a call to a constructor. In that case,this
is the global object so any properties introduced in the constructor will be added to the global object.That problem should not go undetected for long, but it's an interesting test failure.
当我试图弄清楚如何抑制 JSONP 泄漏时,我遇到了这个答案,例如:
通过以下方式抑制 jQuery JSONP“泄漏”:
I came across this answer when I trying to figure out how to squelch JSONP leaks such as:
Squelch jQuery JSONP "leaks" via:
我在许多函数中都遇到了这个错误,如下所示:
所以我在设置函数中传递了一个通配符,它解决了我的问题。
I was encountering this error for many functions as follows:
So I passed a wildcard in the setup function and it solved my issue.
我添加了“mocha.globals(['browserSync']);”下面来解决我的问题。其余代码来自 https://mochajs.org/ - 部分:在浏览器中运行 MOCHA
I added "mocha.globals(['browserSync']);" below to fix my problem. The rest of the code is from https://mochajs.org/ - section : RUNNING MOCHA IN THE BROWSER
在使用之前定义您的存根变量。
Define Your stub variables before you use it.
谨此记录,自 Mocha 7.0.0 起,
ignoreLeaks
选项已被弃用;我们应该使用
mocha.setup({ checkLeaks: false })
反而。Just for the record that
ignoreLeaks
option has been deprecated since Mocha 7.0.0;We should use
mocha.setup({ checkLeaks: false })
instead.