Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

global leak errors in mocha

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)
like image 502
MonkeyBonkey Avatar asked Dec 02 '11 17:12

MonkeyBonkey


4 Answers

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']});
like image 190
ento Avatar answered Sep 21 '22 22:09

ento


You can also disable global leak detection by passing:

mocha --ignore-leaks

In a browser:

mocha.setup({ignoreLeaks: true});
like image 45
Jimchao Avatar answered Sep 21 '22 22:09

Jimchao


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.

like image 32
Dominic Barnes Avatar answered Sep 22 '22 22:09

Dominic Barnes


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.

like image 20
Gudlaugur Egilsson Avatar answered Sep 22 '22 22:09

Gudlaugur Egilsson