Node.js - why do I get leaks when testing with moc

2019-06-22 16:42发布

问题:

I've tried to make zombie work with mocha, but unless I use the mocha --ignore-leaks command options, my test always fails with the error:

Error: global leaks detected: k, i, name, chars, char

My test looks exactly like the one explained in this thread: Mocha and ZombieJS

I wish I could have posted my question there, but as a newbie, I cannot comment on the thread, only ask a new question.

Do you have any idea why I get these leaks? I'm using mocha 1.0.3 and zombie 1.0.0.

回答1:

The leaks can come either from your own code or from node_modules that you use. Mocha should give some hints on where the leaks are, such as forgetting to declare local variable with var.

// global leaks
a = 1;

// no leaks
var a = 1;

You might also be interested writing Node.js app in coffeescript since it helps you avoid mistakes like that. (It automatically initializes variables, using var) http://coffeescript.org/

There is a template that helps you get started here https://github.com/twilson63/express-coffee