Skip to content

armgit / mocha

Public Imported from GitHub
0

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

Added `this.test.error(err)` support to after each hooks. Closes #287

TJ Holowaychuk8e5cc0a2abea
diff --git a/lib/context.js b/lib/context.js
index f636f1b4..f6fa396c 100644
--- a/lib/context.js
+++ b/lib/context.js
@@ -14,7 +14,7 @@ module.exports = Context;
function Context(){}
/**
- * Set the context `Runnable` to `runnable`.
+ * Set or get the context `Runnable` to `runnable`.
*
* @param {Runnable} runnable
* @return {Context}
@@ -22,7 +22,8 @@ function Context(){}
*/
Context.prototype.runnable = function(runnable){
- this._runnable = runnable;
+ if (0 == arguments.length) return this._runnable;
+ this.test = this._runnable = runnable;
return this;
};
@@ -35,7 +36,7 @@ Context.prototype.runnable = function(runnable){
*/
Context.prototype.timeout = function(ms){
- this._runnable.timeout(ms);
+ this.runnable().timeout(ms);
return this;
};
@@ -48,8 +49,8 @@ Context.prototype.timeout = function(ms){
Context.prototype.inspect = function(){
return JSON.stringify(this, function(key, val){
- return '_runnable' == key
- ? undefined
- : val;
+ if ('_runnable' == key) return;
+ if ('test' == key) return;
+ return val;
}, 2);
};
diff --git a/lib/hook.js b/lib/hook.js
index 3ed712e9..d6b45da9 100644
--- a/lib/hook.js
+++ b/lib/hook.js
@@ -29,3 +29,22 @@ function Hook(title, fn) {
*/
Hook.prototype.__proto__ = Runnable.prototype;
+
+/**
+ * Get or set the test `err`.
+ *
+ * @param {Error} err
+ * @return {Error}
+ * @api public
+ */
+
+Hook.prototype.error = function(err){
+ if (0 == arguments.length) {
+ var err = this._error;
+ this._error = null;
+ return err;
+ }
+
+ this._error = err;
+};
+
diff --git a/lib/runnable.js b/lib/runnable.js
index bcc72a9c..329e2857 100644
--- a/lib/runnable.js
+++ b/lib/runnable.js
@@ -83,6 +83,22 @@ Runnable.prototype.clearTimeout = function(){
clearTimeout(this.timer);
};
+/**
+ * Inspect the runnable void of private properties.
+ *
+ * @return {String}
+ * @api private
+ */
+
+Runnable.prototype.inspect = function(){
+ return JSON.stringify(this, function(key, val){
+ if ('_' == key[0]) return;
+ if ('parent' == key) return '#<Suite>';
+ if ('ctx' == key) return '#<Context>';
+ return val;
+ }, 2);
+};
+
/**
* Reset the timeout.
*
diff --git a/lib/runner.js b/lib/runner.js
index 5446e7e6..115cd067 100644
--- a/lib/runner.js
+++ b/lib/runner.js
@@ -189,6 +189,8 @@ Runner.prototype.hook = function(name, fn){
hook.run(function(err){
hook.removeAllListeners('error');
+ var testError = hook.error();
+ if (testError) self.fail(self.test, testError);
if (err) return self.failHook(hook, err);
self.emit('hook end', hook);
next(++i);
diff --git a/lib/test.js b/lib/test.js
index 64a2f10e..11773e0c 100644
--- a/lib/test.js
+++ b/lib/test.js
@@ -30,20 +30,3 @@ function Test(title, fn) {
*/
Test.prototype.__proto__ = Runnable.prototype;
-
-/**
- * Inspect the context void of private properties.
- *
- * @return {String}
- * @api private
- */
-
-Test.prototype.inspect = function(){
- return JSON.stringify(this, function(key, val){
- return '_' == key[0]
- ? undefined
- : 'parent' == key
- ? '#<Suite>'
- : val;
- }, 2);
-};
\ No newline at end of file