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)
Eran
  • 387,369
  • 54
  • 702
  • 768
MonkeyBonkey
  • 46,433
  • 78
  • 254
  • 460

9 Answers9

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']});
ento
  • 5,801
  • 6
  • 51
  • 69
  • 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. – machineghost Feb 09 '13 at 01:53
  • 1
    you'll need to replace 'hasCert' with whatever variable mocha is detecting a leak with – MonkeyBonkey Mar 16 '13 at 14:44
  • This error also apply if a variable was misspelled and is used in an assignment. Pretty useful, although I was puzzled at first. :) – Yanick Rochon Feb 25 '14 at 20:21
24

You can also disable global leak detection by passing:

mocha --ignore-leaks

In a browser:

mocha.setup({ignoreLeaks: true});
Jimchao
  • 1,468
  • 1
  • 12
  • 18
  • 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`. – Janosh Feb 27 '18 at 10:35
12

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.

Dominic Barnes
  • 28,083
  • 8
  • 65
  • 90
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.

Gabber
  • 5,152
  • 6
  • 35
  • 49
Gudlaugur Egilsson
  • 2,420
  • 2
  • 24
  • 23
  • For me it was triggered with a `new` call to constructor with no arguments (the ctor was expecting 3 args). – Mrchief Jun 21 '13 at 14:46
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*']
});
muffs
  • 805
  • 10
  • 14
4

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: ['*']
});
Saba
  • 3,418
  • 2
  • 21
  • 34
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>
Ari Singh
  • 1,228
  • 7
  • 12
0

Define Your stub variables before you use it.

var hasCert;

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

jsduniya
  • 2,464
  • 7
  • 30
  • 45
0

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

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

AJ H
  • 687
  • 6
  • 8