☕️ Classic, reliable, trusted test framework for Node.js and the browser
Added test-case context support. Closes #113
Tj Holowaychukb01a73bd9e3c
diff --git a/lib/runnable.js b/lib/runnable.jsindex d1512abd..49c35a70 100644--- a/lib/runnable.js+++ b/lib/runnable.js@@ -26,6 +26,7 @@ function Runnable(title, fn) {this.async = fn && fn.length;this.sync = ! this.async;this._timeout = 2000;+ this.context = this;}/**@@ -101,6 +102,7 @@ Runnable.prototype.run = function(fn){var self = this, ms = this.timeout(), start = new Date+ , ctx = this.context, finished, emitted;@@ -135,7 +137,7 @@ Runnable.prototype.run = function(fn){// asyncif (this.async) {try {- this.fn(function(err){+ this.fn.call(ctx, function(err){if (err instanceof Error) return done(err);if (null != err) return done(new Error('done() invoked with non-Error: ' + err));done();@@ -148,7 +150,7 @@ Runnable.prototype.run = function(fn){// synctry {- if (!this.pending) this.fn();+ if (!this.pending) this.fn.call(ctx);this.duration = new Date - start;fn();} catch (err) {diff --git a/lib/runner.js b/lib/runner.jsindex 10978b89..248dbc6f 100644--- a/lib/runner.js+++ b/lib/runner.js@@ -156,6 +156,7 @@ Runner.prototype.hook = function(name, fn){var hook = hooks[i];if (!hook) return fn();self.currentRunnable = hook;+ hook.context = self.test;self.emit('hook', hook);diff --git a/test/context.js b/test/context.jsnew file mode 100644index 00000000..b2545856--- /dev/null+++ b/test/context.js@@ -0,0 +1,46 @@++describe('Context', function(){+ var prev;++ beforeEach(function(){+ this.name = 'Tobi';+ })++ afterEach(function(){+ this.name.should.equal('Tobi');+ prev = this;+ })++ it('should be maintained between hooks', function(){+ this.name.should.equal('Tobi');+ })++ it('should be unique to the test-case', function(){+ this.should.not.equal(prev);+ })++ describe('when async', function(){+ var prev;++ beforeEach(function(done){+ this.name = 'Tobi';+ done();+ })++ afterEach(function(done){+ this.name.should.equal('Tobi');+ prev = this;+ done();+ })++ it('should be maintained between hooks', function(done){+ this.name.should.equal('Tobi');+ done();+ })++ it('should be unique to the test-case', function(done){+ this.should.not.equal(prev);+ done();+ })+ })+})\ No newline at end of file