Skip to content

armgit / mocha

Public Imported from GitHub
0

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

utilizing new Hooks

Tj Holowaychuk9d7fd2753be9
diff --git a/lib/runner.js b/lib/runner.js
index 9c83f7ab..f748c8f7 100644
--- a/lib/runner.js
+++ b/lib/runner.js
@@ -117,43 +117,18 @@ Runner.prototype.failHook = function(hook, err){
Runner.prototype.hook = function(name, fn){
var suite = this.suite
- , callbacks = suite[name + 'Callbacks']
+ , hooks = suite['_' + name]
, ms = suite._timeout
, timer;
function next(i) {
- var callback = callbacks[i];
- if (!callback) return fn();
-
- // async
- if (1 == callback.length) {
- // timeout
- timer = setTimeout(function(){
- fn(new Error('timeout of ' + ms + 'ms exceeded'));
- }, ms);
-
- // async
- try {
- callback(function(err){
- clearTimeout(timer);
- if (err) return fn(err);
- next(++i);
- });
- } catch (err) {
- fn(err);
- }
- return;
- }
-
- // serial
- try {
- callback();
- process.nextTick(function(){
- next(++i);
- });
- } catch (err) {
- fn(err);
- }
+ var hook = hooks[i];
+ if (!hook) return fn();
+ hook.on('error', fn);
+ hook.run(function(err){
+ if (err) return fn(err);
+ next(++i);
+ });
}
process.nextTick(function(){
diff --git a/lib/suite.js b/lib/suite.js
index f0472e9f..7148f50a 100644
--- a/lib/suite.js
+++ b/lib/suite.js
@@ -90,7 +90,7 @@ Suite.prototype.timeout = function(ms){
Suite.prototype.beforeAll = function(fn){
var hook = new Hook('before all', fn);
hook.parent = this;
- this._beforeAll.push(fn);
+ this._beforeAll.push(hook);
this.emit('beforeAll', hook);
return this;
};