Skip to content

armgit / mocha

Public Imported from GitHub
0

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

Added suite merging. Closes #28

Tj Holowaychuke47446112f27
diff --git a/lib/interfaces/bdd.js b/lib/interfaces/bdd.js
index ab39029f..649e2ca6 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 e1661b9d..368f9b52 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 00000000..10487feb
--- /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