global leak errors in mocha
Asked Answered
O

9

39

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)
Ozalid answered 2/12, 2011 at 17:36 Comment(0)
B
51

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']});
Blackpoll answered 6/4, 2012 at 8:19 Comment(3)
Dunno why, but mocha.setup({globals: ['hasCert']}); didn't do me any good. Jimchao's mocha.setup({ignoreLeaks: true}); suggestion did work for me though.Abutter
you'll need to replace 'hasCert' with whatever variable mocha is detecting a leak withOzalid
This error also apply if a variable was misspelled and is used in an assignment. Pretty useful, although I was puzzled at first. :)Vacation
C
24

You can also disable global leak detection by passing:

mocha --ignore-leaks

In a browser:

mocha.setup({ignoreLeaks: true});
Cards answered 26/7, 2012 at 21:14 Comment(1)
Where do I place this mocha.setup({ignoreLeaks: true});? Doesn't seem to work if I paste it into mocha.opts and neither does --ignore-leaks.Sagitta
N
13

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.

Nigger answered 2/12, 2011 at 20:21 Comment(0)
L
7

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.

Lavernalaverne answered 7/10, 2012 at 16:15 Comment(1)
For me it was triggered with a new call to constructor with no arguments (the ctor was expecting 3 args).Lyautey
K
4

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*']
});
Katrinka answered 26/12, 2013 at 22:16 Comment(0)
F
3

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: ['*']
});
Finesse answered 2/4, 2015 at 6:12 Comment(2)
At that point you might as well not check for global leaks at all -_-Bille
@Bille exactly, this is especially true when introducing browser testing in an older application.Gmur
A
0

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>
Australia answered 12/3, 2018 at 22:23 Comment(0)
W
0

Define Your stub variables before you use it.

var hasCert;

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

Writeup answered 22/6, 2018 at 8:21 Comment(0)
C
0

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

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

Cordon answered 21/7, 2020 at 2:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.