global leak errors in mocha

2019-03-22 12:36发布

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)

8条回答
唯我独甜
2楼-- · 2019-03-22 12:45

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']});
查看更多
Animai°情兽
3楼-- · 2019-03-22 12:46

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*']
});
查看更多
我欲成王,谁敢阻挡
4楼-- · 2019-03-22 12:59

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.

查看更多
仙女界的扛把子
5楼-- · 2019-03-22 13:01

You can also disable global leak detection by passing:

mocha --ignore-leaks

In a browser:

mocha.setup({ignoreLeaks: true});
查看更多
可以哭但决不认输i
6楼-- · 2019-03-22 13:01

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: ['*']
});
查看更多
爷、活的狠高调
7楼-- · 2019-03-22 13:01

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>
查看更多
登录 后发表回答