From 6c705cd4d4bde87d735fcf6d8726af720bf392a5 Mon Sep 17 00:00:00 2001 From: Jonathan Park Date: Tue, 18 Mar 2014 19:22:55 -0700 Subject: [PATCH] Force sibling describes to have separate contexts --- lib/suite.js | 6 ++++-- mocha.js | 6 ++++-- test/acceptance/context.js | 43 +++++++++++++++++++++++++++++++++++++- 3 files changed, 50 insertions(+), 5 deletions(-) diff --git a/lib/suite.js b/lib/suite.js index d403a83f38..b06645b596 100644 --- a/lib/suite.js +++ b/lib/suite.js @@ -46,9 +46,11 @@ exports.create = function(parent, title){ * @api private */ -function Suite(title, ctx) { +function Suite(title, parentContext) { this.title = title; - this.ctx = ctx; + var context = function() {}; + context.prototype = parentContext; + this.ctx = new context(); this.suites = []; this.tests = []; this.pending = false; diff --git a/mocha.js b/mocha.js index 9c85199fa1..19edc87d97 100644 --- a/mocha.js +++ b/mocha.js @@ -5065,9 +5065,11 @@ exports.create = function(parent, title){ * @api private */ -function Suite(title, ctx) { +function Suite(title, parentContext) { this.title = title; - this.ctx = ctx; + var context = function () {}; + context.prototype = parentContext; + this.ctx = new context(); this.suites = []; this.tests = []; this.pending = false; diff --git a/test/acceptance/context.js b/test/acceptance/context.js index e2af9d5eae..0d39f14194 100644 --- a/test/acceptance/context.js +++ b/test/acceptance/context.js @@ -23,4 +23,45 @@ describe('Context', function(){ after(function(){ this.calls.should.eql(['before', 'before two', 'test', 'after two']); }) -}) \ No newline at end of file +}) + +describe('Context Siblings', function(){ + beforeEach(function(){ + this.calls = ['before']; + }) + + describe('sequestered sibling', function(){ + beforeEach(function(){ + this.calls.push('before two'); + this.hiddenFromSibling = 'This should be hidden'; + }) + + it('should work', function(){ + this.hiddenFromSibling.should.eql('This should be hidden') + }) + }) + + describe('sibling verifiction', function(){ + beforeEach(function(){ + this.calls.push('before sibling'); + }) + + it('should not have value set within a sibling describe', function(){ + 'This should be hidden'.should.not.eql(this.hiddenFromSibling); + this.visibleFromTestSibling = 'Visible from test sibling'; + }) + + it('should allow test siblings to modify shared context', function(){ + 'Visible from test sibling'.should.eql(this.visibleFromTestSibling); + }) + + it('should have reset this.calls before describe', function(){ + this.calls.should.eql(['before', 'before sibling']); + }) + }) + + after(function(){ + this.calls.should.eql(['before', 'before sibling']); + }) + +})