node.js mocha 中的全局泄漏错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8360318/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 14:50:22  来源:igfitidea点击:

global leak errors in mocha

node.jsunit-testingpush-notificationapple-push-notificationsmocha

提问by MonkeyBonkey

I was trying to unit test the apple push notification librarywhen I got a global leak error trying to open up an APN connection.

当我在尝试打开 APN 连接时遇到全局泄漏错误时,我试图对苹果推送通知库进行单元测试。

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

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

I'm not sure I understand what checkGlobals is doing... is it just checking to see if any global variable are being set?

我不确定我是否理解 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)

回答by ento

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

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

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

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

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.

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

In a browser:

在浏览器中:

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

回答by Jimchao

You can also disable global leak detection by passing:

您还可以通过以下方式禁用全局泄漏检测:

mocha --ignore-leaks

In a browser:

在浏览器中:

mocha.setup({ignoreLeaks: true});

回答by Dominic Barnes

I ran into this problem as well, you probably forgot a varstatement somewhere like I did, which in JS means that a global variable will be created.

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

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

根据您构建应用程序的方式,您可能必须自己寻找它,希望这不是导致这种情况的第 3 方代码。:P

You should use JSLintor JSHintthrough your project, they should help uncover the source if it's anywhere in your code.

您应该使用的JSLintJSHint通过你的项目,他们应该帮助发现源,如果它在你的代码的任何地方。

回答by Gudlaugur Egilsson

This can also happen if you forget newin a call to a constructor. In that case, thisis the global object so any properties introduced in the constructor will be added to the global object.

如果您忘记new调用构造函数,也会发生这种情况。在这种情况下,this是全局对象,因此构造函数中引入的任何属性都将添加到全局对象中。

That problem should not go undetected for long, but it's an interesting test failure.

这个问题应该不会长时间未被发现,但它是一个有趣的测试失败。

回答by muffs

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

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

Error: global leak detected: jQuery20305777117821853608_1388095882488

Squelch jQuery JSONP "leaks" via:

静噪 jQuery JSONP 通过以下方式“泄漏”:

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

回答by Saba Hassan

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.

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

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

回答by Ari Singh

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

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

<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>

回答by jsduniya

Define Your stub variables before you use it.

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

var hasCert;

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

var hasCert;

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