From e47446112f2781bc6fbb1cd47e8bc6b21b5224f0 Mon Sep 17 00:00:00 2001 From: Tj Holowaychuk Date: Wed, 16 Nov 2011 15:50:45 -0800 Subject: [PATCH] Added suite merging. Closes #28 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit the merge.js example there with --reporter spec will result in: merge stuff one ✓ should do something two ✓ should do stuff three ✓ should do something four ✓ should do something --- lib/interfaces/bdd.js | 3 +-- lib/suite.js | 36 +++++++++++++++++++++++++++++++++++- test/merge.js | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 3 deletions(-) create mode 100644 test/merge.js diff --git a/lib/interfaces/bdd.js b/lib/interfaces/bdd.js index ab39029f64..649e2ca6b0 100644 --- a/lib/interfaces/bdd.js +++ b/lib/interfaces/bdd.js @@ -67,8 +67,7 @@ module.exports = function(suite){ */ context.describe = function(title, fn){ - var suite = new Suite(title); - suites[0].addSuite(suite); + var suite = Suite.create(suites[0], title); suites.unshift(suite); fn(); suites.shift(); diff --git a/lib/suite.js b/lib/suite.js index e1661b9d68..368f9b52ab 100644 --- a/lib/suite.js +++ b/lib/suite.js @@ -9,7 +9,35 @@ var EventEmitter = require('events').EventEmitter; * Expose `Suite`. */ -module.exports = Suite; +exports = module.exports = Suite; + +/** + * Suite map. + */ + +var map = {}; + +/** + * Create a new `Suite` with the given `title` + * and parent `Suite`. When a suite with the + * same title is already present, that suite + * is returned to provide nicer reporter + * and more flexible meta-testing. + * + * @param {Suite} parent + * @param {String} title + * @return {Suite} + * @api public + */ + +exports.create = function(parent, title){ + var suite = new Suite(title); + suite.parent = parent; + title = suite.fullTitle(); + if (map[title]) return map[title]; + parent.addSuite(suite); + return map[title] = suite; +}; /** * Initialize a new `Suite` with the given `title`. @@ -57,6 +85,7 @@ Suite.prototype.timeout = function(ms){ Suite.prototype.beforeAll = function(fn){ this.beforeAllCallbacks.push(fn); + this.emit('beforeAll', fn); return this; }; @@ -70,6 +99,7 @@ Suite.prototype.beforeAll = function(fn){ Suite.prototype.afterAll = function(fn){ this.afterAllCallbacks.push(fn); + this.emit('afterAll', fn); return this; }; @@ -83,6 +113,7 @@ Suite.prototype.afterAll = function(fn){ Suite.prototype.beforeEach = function(fn){ this.beforeEachCallbacks.push(fn); + this.emit('beforeEach', fn); return this; }; @@ -96,6 +127,7 @@ Suite.prototype.beforeEach = function(fn){ Suite.prototype.afterEach = function(fn){ this.afterEachCallbacks.push(fn); + this.emit('afterEach', fn); return this; }; @@ -111,6 +143,7 @@ Suite.prototype.addSuite = function(suite){ suite.parent = this; if (this._timeout) suite.timeout(this._timeout); this.suites.push(suite); + this.emit('suite', suite); return this; }; @@ -126,6 +159,7 @@ Suite.prototype.addTest = function(test){ test.parent = this; if (this._timeout) test.timeout(this._timeout); this.tests.push(test); + this.emit('test', test); return this; }; diff --git a/test/merge.js b/test/merge.js new file mode 100644 index 0000000000..10487febd5 --- /dev/null +++ b/test/merge.js @@ -0,0 +1,34 @@ + +describe('merge', function(){ + describe('stuff', function(){ + describe('one', function(){ + it('should do something', function(){ + + }) + }) + + describe('two', function(){ + it('should do stuff', function(){ + + }) + }) + }) +}) + +describe('merge', function(){ + describe('stuff', function(){ + describe('three', function(){ + it('should do something', function(){ + + }) + }) + }) +}) + +describe('merge stuff', function(){ + describe('four', function(){ + it('should do something', function(){ + + }) + }) +}) \ No newline at end of file