global leak errors in mocha
Solution 1
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']});
Solution 2
You can also disable global leak detection by passing:
mocha --ignore-leaks
In a browser:
mocha.setup({ignoreLeaks: true});
Solution 3
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.
Solution 4
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.
Solution 5
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*']
});
MonkeyBonkey
CTO of Pictorious.com, a mobile app for turning photo sharing into a fun meme-game.
Updated on July 21, 2020Comments
-
MonkeyBonkey almost 4 years
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)
-
machineghost over 11 yearsDunno why, but
mocha.setup({globals: ['hasCert']});
didn't do me any good. Jimchao'smocha.setup({ignoreLeaks: true});
suggestion did work for me though. -
MonkeyBonkey over 11 yearsyou'll need to replace 'hasCert' with whatever variable mocha is detecting a leak with
-
Mrchief about 11 yearsFor me it was triggered with a
new
call to constructor with no arguments (the ctor was expecting 3 args). -
Yanick Rochon over 10 yearsThis error also apply if a variable was misspelled and is used in an assignment. Pretty useful, although I was puzzled at first. :)
-
Lobabob about 8 yearsAt that point you might as well not check for global leaks at all -_-
-
Casimir over 6 yearsWhere do I place this
mocha.setup({ignoreLeaks: true});
? Doesn't seem to work if I paste it intomocha.opts
and neither does--ignore-leaks
. -
lasec0203 about 6 years@Lobabob exactly, this is especially true when introducing browser testing in an older application.