摩卡中的全局泄漏错误

发布于 2024-12-19 06:37:29 字数 814 浏览 6 评论 0原文

当我尝试打开 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

浅听莫相离 2024-12-26 06:37:29

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

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

命令行上,

$ mocha --globals hasCert

引用文档:

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

在浏览器中:

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

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,

$ mocha --globals hasCert

To quote the documentation:

[This option] accepts a comma-delimited list of accepted global variable names. For example suppose your app deliberately exposes a global named app and YUI, you may want to add --globals app,YUI.

In a browser:

mocha.setup({globals: ['hasCert']});
看海 2024-12-26 06:37:29

您还可以通过传递以下内容来禁用全局泄漏检测:

mocha --ignore-leaks

在浏览器中:

mocha.setup({ignoreLeaks: true});

You can also disable global leak detection by passing:

mocha --ignore-leaks

In a browser:

mocha.setup({ignoreLeaks: true});
岁月苍老的讽刺 2024-12-26 06:37:29

我也遇到了这个问题,你可能像我一样忘记了一个 var 语句,这在 JS 中意味着将创建一个全局变量。

您可能必须自己寻找它,具体取决于您构建应用程序的方式,并且希望这不是导致此问题的第三方代码。 :P

您应该使用 JSLintJSHint 通过您的项目,他们应该帮助发现源代码(如果它位于代码中的任何位置)。

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.

飘然心甜 2024-12-26 06:37:29

如果您在调用构造函数时忘记了 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.

故人爱我别走 2024-12-26 06:37:29

当我试图弄清楚如何抑制 JSONP 泄漏时,我遇到了这个答案,例如:

Error: global leak detected: jQuery20305777117821853608_1388095882488

通过以下方式抑制 jQuery JSONP“泄漏”:

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

I came across this answer when I trying to figure out how to squelch JSONP leaks such as:

Error: global leak detected: jQuery20305777117821853608_1388095882488

Squelch jQuery JSONP "leaks" via:

mocha.setup({
  globals: ['jQuery*']
});
折戟 2024-12-26 06:37:29

我在许多函数中都遇到了这个错误,如下所示:

1) test "before all" hook:
 Error: global leaks detected: __timers, _document, history, addEventListener, removeEventListener, dispatchEvent, raise, __stopAllTimers, Image, _virtualConsole, run, getGlobal, dispose, top, parent, self, frames, window, _frame, $, jQuery, Handlebars, Ember, Em, MetamorphENV, Cloud, jQuery1102048038746835663915, _listeners, _length, length, document, location, close, getComputedStyle, navigator, name, innerWidth, innerHeight, outerWidth, outerHeight, pageXOffset, pageYOffset, screenX, screenY, screenLeft, screenTop, scrollX, scrollY, scrollTop, scrollLeft, alert, blur, confirm, createPopup, focus, moveBy, moveTo, open, print, prompt, resizeBy, resizeTo, scroll, scrollBy, scrollTo, screen, mapper, mapDOMNodes, visitTree, markTreeReadonly, INDEX_SIZE_ERR, DOMSTRING_SIZE_ERR, HIERARCHY_REQUEST_ERR, WRONG_DOCUMENT_ERR, INVALID_CHARACTER_ERR, NO_DATA_ALLOWED_ERR, NO_MODIFICATION_ALLOWED_ERR, NOT_FOUND_ERR, NOT_SUPPORTED_ERR, INUSE_ATTRIBUTE_ERR, INVALID_STATE_ERR, SYNTAX_ERR, INVALID_MODIFICATION_ERR, NAMESPACE_ERR, INVALID_ACCESS_ERR, exceptionMessages, DOMException, NodeList, DOMImplementation, Node, NamedNodeMap, AttributeList, Element, DocumentFragment, Document, Attr, EventException, Event, UIEvent, MouseEvent, MutationEvent, EventTarget, languageProcessors, resourceLoader, HTMLCollection, HTMLOptionsCollection, HTMLDocument, HTMLElement, HTMLFormElement, HTMLLinkElement, HTMLMetaElement, HTMLHtmlElement, HTMLHeadElement, HTMLTitleElement, HTMLBaseElement, HTMLIsIndexElement, HTMLStyleElement, HTMLBodyElement, HTMLSelectElement, HTMLOptGroupElement, HTMLOptionElement, HTMLInputElement, HTMLTextAreaElement, HTMLButtonElement, HTMLLabelElement, HTMLFieldSetElement, HTMLLegendElement, HTMLUListElement, HTMLOListElement, HTMLDListElement, HTMLDirectoryElement, HTMLMenuElement, HTMLLIElement, HTMLCanvasElement, HTMLDivElement, HTMLParagraphElement, HTMLHeadingElement, HTMLQuoteElement, HTMLPreElement, HTMLBRElement, HTMLBaseFontElement, HTMLFontElement, HTMLHRElement, HTMLModElement, HTMLAnchorElement, HTMLImageElement, HTMLObjectElement, HTMLParamElement, HTMLAppletElement, HTMLMapElement, HTMLAreaElement, HTMLScriptElement, HTMLTableElement, HTMLTableCaptionElement, HTMLTableColElement, HTMLTableSectionElement, HTMLTableRowElement, HTMLTableCellElement, HTMLFrameSetElement, HTMLFrameElement, HTMLIFrameElement, StyleSheet, MediaList, CSSStyleSheet, CSSRule, CSSStyleRule, CSSMediaRule, CSSImportRule, CSSStyleDeclaration, StyleSheetList, VALIDATION_ERR, TYPE_MISMATCH_ERR, UserDataHandler, DOMError, DOMConfiguration, DOMStringList, XPathException, XPathExpression, XPathResult, XPathEvaluator, DocumentType, CharacterData, ProcessingInstruction, Comment, Text, NodeFilter, _parser, _parsingMode, _augmented

所以我在设置函数中传递了一个通配符,它​​解决了我的问题。

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

I was encountering this error for many functions as follows:

1) test "before all" hook:
 Error: global leaks detected: __timers, _document, history, addEventListener, removeEventListener, dispatchEvent, raise, __stopAllTimers, Image, _virtualConsole, run, getGlobal, dispose, top, parent, self, frames, window, _frame, $, jQuery, Handlebars, Ember, Em, MetamorphENV, Cloud, jQuery1102048038746835663915, _listeners, _length, length, document, location, close, getComputedStyle, navigator, name, innerWidth, innerHeight, outerWidth, outerHeight, pageXOffset, pageYOffset, screenX, screenY, screenLeft, screenTop, scrollX, scrollY, scrollTop, scrollLeft, alert, blur, confirm, createPopup, focus, moveBy, moveTo, open, print, prompt, resizeBy, resizeTo, scroll, scrollBy, scrollTo, screen, mapper, mapDOMNodes, visitTree, markTreeReadonly, INDEX_SIZE_ERR, DOMSTRING_SIZE_ERR, HIERARCHY_REQUEST_ERR, WRONG_DOCUMENT_ERR, INVALID_CHARACTER_ERR, NO_DATA_ALLOWED_ERR, NO_MODIFICATION_ALLOWED_ERR, NOT_FOUND_ERR, NOT_SUPPORTED_ERR, INUSE_ATTRIBUTE_ERR, INVALID_STATE_ERR, SYNTAX_ERR, INVALID_MODIFICATION_ERR, NAMESPACE_ERR, INVALID_ACCESS_ERR, exceptionMessages, DOMException, NodeList, DOMImplementation, Node, NamedNodeMap, AttributeList, Element, DocumentFragment, Document, Attr, EventException, Event, UIEvent, MouseEvent, MutationEvent, EventTarget, languageProcessors, resourceLoader, HTMLCollection, HTMLOptionsCollection, HTMLDocument, HTMLElement, HTMLFormElement, HTMLLinkElement, HTMLMetaElement, HTMLHtmlElement, HTMLHeadElement, HTMLTitleElement, HTMLBaseElement, HTMLIsIndexElement, HTMLStyleElement, HTMLBodyElement, HTMLSelectElement, HTMLOptGroupElement, HTMLOptionElement, HTMLInputElement, HTMLTextAreaElement, HTMLButtonElement, HTMLLabelElement, HTMLFieldSetElement, HTMLLegendElement, HTMLUListElement, HTMLOListElement, HTMLDListElement, HTMLDirectoryElement, HTMLMenuElement, HTMLLIElement, HTMLCanvasElement, HTMLDivElement, HTMLParagraphElement, HTMLHeadingElement, HTMLQuoteElement, HTMLPreElement, HTMLBRElement, HTMLBaseFontElement, HTMLFontElement, HTMLHRElement, HTMLModElement, HTMLAnchorElement, HTMLImageElement, HTMLObjectElement, HTMLParamElement, HTMLAppletElement, HTMLMapElement, HTMLAreaElement, HTMLScriptElement, HTMLTableElement, HTMLTableCaptionElement, HTMLTableColElement, HTMLTableSectionElement, HTMLTableRowElement, HTMLTableCellElement, HTMLFrameSetElement, HTMLFrameElement, HTMLIFrameElement, StyleSheet, MediaList, CSSStyleSheet, CSSRule, CSSStyleRule, CSSMediaRule, CSSImportRule, CSSStyleDeclaration, StyleSheetList, VALIDATION_ERR, TYPE_MISMATCH_ERR, UserDataHandler, DOMError, DOMConfiguration, DOMStringList, XPathException, XPathExpression, XPathResult, XPathEvaluator, DocumentType, CharacterData, ProcessingInstruction, Comment, Text, NodeFilter, _parser, _parsingMode, _augmented

So I passed a wildcard in the setup function and it solved my issue.

mocha.setup({
  globals: ['*']
});
你在我安 2024-12-26 06:37:29

我添加了“mocha.globals(['browserSync']);”下面来解决我的问题。其余代码来自 https://mochajs.org/ - 部分:在浏览器中运行 MOCHA

<script>mocha.setup('bdd')</script>
<script src="basic-spec.js"></script>
<script>
    mocha.checkLeaks();
    mocha.globals(['jQuery']);
    mocha.globals(['___browserSync___']);  //<<== This line was added
    mocha.run();
</script>

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

<script>mocha.setup('bdd')</script>
<script src="basic-spec.js"></script>
<script>
    mocha.checkLeaks();
    mocha.globals(['jQuery']);
    mocha.globals(['___browserSync___']);  //<<== This line was added
    mocha.run();
</script>
鱼忆七猫命九 2024-12-26 06:37:29

在使用之前定义您的存根变量。

var 有证书;

var hasCert = sinon.stub(实例,方法);

Define Your stub variables before you use it.

var hasCert;

var hasCert = sinon.stub(instance, method);

心如狂蝶 2024-12-26 06:37:29

Just for the record that ignoreLeaks option has been deprecated since Mocha 7.0.0;

We should use mocha.setup({ checkLeaks: false }) instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文