diff --git a/lib/mocha.js b/lib/mocha.js index c703e09dee..687f5c77a3 100644 --- a/lib/mocha.js +++ b/lib/mocha.js @@ -92,7 +92,7 @@ function Mocha(options) { this.files = []; this.options = options; // root suite - this.suite = new exports.Suite('', new exports.Context()); + this.suite = new exports.Suite('', new exports.Context(), true); if ('useColors' in options) { utils.deprecate( diff --git a/lib/suite.js b/lib/suite.js index 0e3831fabb..2cb3a39de9 100644 --- a/lib/suite.js +++ b/lib/suite.js @@ -39,15 +39,18 @@ exports.create = function(parent, title) { }; /** - * Initialize a new `Suite` with the given `title` and `ctx`. Derived from [EventEmitter](https://nodejs.org/api/events.html#events_class_eventemitter) + * Constructs a new `Suite` instance with the given `title`, `ctx`, and `isRoot`. * - * @memberof Mocha * @public * @class - * @param {string} title - * @param {Context} parentContext + * @extends EventEmitter + * @memberof Mocha + * @see {@link https://nodejs.org/api/events.html#events_class_eventemitter|EventEmitter} + * @param {string} title - Suite title. + * @param {Context} parentContext - Parent context instance. + * @param {boolean} [isRoot=false] - Whether this is the root suite. */ -function Suite(title, parentContext) { +function Suite(title, parentContext, isRoot) { if (!utils.isString(title)) { throw createInvalidArgumentTypeError( 'Suite argument "title" must be a string. Received type "' + @@ -68,7 +71,7 @@ function Suite(title, parentContext) { this._beforeAll = []; this._afterEach = []; this._afterAll = []; - this.root = !title; + this.root = isRoot === true; this._timeout = 2000; this._enableTimeouts = true; this._slow = 75; @@ -324,6 +327,7 @@ Suite.prototype.afterEach = function(title, fn) { */ Suite.prototype.addSuite = function(suite) { suite.parent = this; + suite.root = false; suite.timeout(this.timeout()); suite.retries(this.retries()); suite.enableTimeouts(this.enableTimeouts()); diff --git a/test/unit/suite.spec.js b/test/unit/suite.spec.js index 50a0bda332..fa57898a51 100644 --- a/test/unit/suite.spec.js +++ b/test/unit/suite.spec.js @@ -302,6 +302,27 @@ describe('Suite', function() { }); }); + describe('.create()', function() { + before(function() { + this.first = new Suite('Root suite', {}, true); + this.second = new Suite('RottenRoot suite', {}, true); + this.first.addSuite(this.second); + }); + + it('does not create a second root suite', function() { + expect(this.second.parent, 'to be', this.first); + expect(this.first.root, 'to be', true); + expect(this.second.root, 'to be', false); + }); + + it('does not denote the root suite by being titleless', function() { + var emptyTitleSuite = Suite.create(this.second, ''); + expect(emptyTitleSuite.parent, 'to be', this.second); + expect(emptyTitleSuite.root, 'to be', false); + expect(this.second.root, 'to be', false); + }); + }); + describe('.addSuite()', function() { beforeEach(function() { this.first = new Suite('First suite'); @@ -390,8 +411,8 @@ describe('Suite', function() { describe('when there is a parent', function() { describe('the parent is the root suite', function() { it('returns the suite title', function() { - var parentSuite = new Suite(''); - parentSuite.addSuite(this.suite); + var rootSuite = new Suite('', {}, true); + rootSuite.addSuite(this.suite); expect(this.suite.titlePath(), 'to equal', ['A Suite']); }); });