Skip to content

armgit / mocha

Public Imported from GitHub
0

☕️ Classic, reliable, trusted test framework for Node.js and the browser

Fixed global leak reporting in hooks

Tj Holowaychuk75ebe5371875
diff --git a/lib/runner.js b/lib/runner.js
index 6625b736..362c269c 100644
--- a/lib/runner.js
+++ b/lib/runner.js
@@ -24,6 +24,8 @@ module.exports = Runner;
* - `suite end` (suite) all tests (and sub-suites) have finished
* - `test` (test) test execution started
* - `test end` (test) test completed
+ * - `hook` (hook) hook execution started
+ * - `hook end` (hook) hook complete
* - `pass` (test) test passed
* - `fail` (test, err) test failed
*
@@ -36,6 +38,7 @@ function Runner(suite) {
this.total = suite.total();
this.globals = Object.keys(global).concat(['errno']);
this.on('test end', function(test){ self.checkGlobals(test); });
+ this.on('hook end', function(hook){ self.checkGlobals(hook); });
this.grep(/.*/);
}
@@ -129,12 +132,15 @@ Runner.prototype.hook = function(name, fn){
var hook = hooks[i];
if (!hook) return fn();
+ self.emit('hook', hook);
+
hook.on('error', function(err){
self.failHook(hook, err);
});
hook.run(function(err){
if (err) return self.failHook(hook, err);
+ self.emit('hook end', hook);
next(++i);
});
}
diff --git a/lib/suite.js b/lib/suite.js
index 7148f50a..3783aec2 100644
--- a/lib/suite.js
+++ b/lib/suite.js
@@ -88,7 +88,7 @@ Suite.prototype.timeout = function(ms){
*/
Suite.prototype.beforeAll = function(fn){
- var hook = new Hook('before all', fn);
+ var hook = new Hook('"before all" hook', fn);
hook.parent = this;
this._beforeAll.push(hook);
this.emit('beforeAll', hook);
@@ -104,7 +104,7 @@ Suite.prototype.beforeAll = function(fn){
*/
Suite.prototype.afterAll = function(fn){
- var hook = new Hook('after all', fn);
+ var hook = new Hook('"after all" hook', fn);
hook.parent = this;
this._afterAll.push(hook);
this.emit('afterAll', hook);
@@ -120,7 +120,7 @@ Suite.prototype.afterAll = function(fn){
*/
Suite.prototype.beforeEach = function(fn){
- var hook = new Hook('before each', fn);
+ var hook = new Hook('"before each" hook', fn);
hook.parent = this;
this._beforeEach.push(hook);
this.emit('beforeEach', hook);
@@ -136,7 +136,7 @@ Suite.prototype.beforeEach = function(fn){
*/
Suite.prototype.afterEach = function(fn){
- var hook = new Hook('after each', fn);
+ var hook = new Hook('"after each" hook', fn);
hook.parent = this;
this._afterEach.push(hook);
this.emit('afterEach', hook);
diff --git a/test/globals.js b/test/globals.js
index f9c2ef67..d96e1c71 100644
--- a/test/globals.js
+++ b/test/globals.js
@@ -1,5 +1,11 @@
describe('global leaks', function(){
+ before(function(){
+ // uncomment to test
+ // foo = 'hey';
+ // bar = 'hey';
+ })
+
beforeEach(function(){
// uncomment to test
// foo = 'bar'