本文介绍了mocha 中的全局泄漏错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试打开一个全局泄漏错误时,我试图对 apple 推送通知库 进行单元测试APN 连接.

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.

这是我的配置错误还是 node-apn 或 mocha 中的错误?

Is that a configuration error on my part or an error in node-apn or mocha?

我不确定我是否理解 checkGlobals 正在做什么...它只是检查是否正在设置任何全局变量?

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)

推荐答案

是的,Mocha 具有全局泄漏检测机制,如果您的被测代码引入全局变量,该机制会发出警报并失败.

Yes, Mocha features a global leak detection mechanism which alerts and fails if your code under test introduces global variables.

如果在库中声明了 hasCert 并且您无法控制其创建,您可以告诉 Mocha 忽略它.

If hasCert is declared in a library and you have no control over its creation, you can tell Mocha to ignore it.

命令行上,

$ mocha --globals hasCert

引用文档:

[此选项] 接受以逗号分隔的已接受全局变量名称列表.例如,假设您的应用程序故意公开一个全局命名的应用程序和 YUI,您可能需要添加 --globals app,YUI.

在浏览器中:

mocha.setup({globals: ['hasCert']});

这篇关于mocha 中的全局泄漏错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 06:13