From c458e210dbd78ea93fa19d6ac7bf163eb0495580 Mon Sep 17 00:00:00 2001 From: juergba Date: Fri, 21 Dec 2018 23:12:50 +0100 Subject: [PATCH 1/3] suite constructor --- lib/mocha.js | 2 +- lib/suite.js | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) 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 64e7db4b24..4909c7cca4 100644 --- a/lib/suite.js +++ b/lib/suite.js @@ -41,15 +41,16 @@ 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) + * Initialize a new `Suite` with the given `title`, `ctx` and `root`. Derived from [EventEmitter](https://nodejs.org/api/events.html#events_class_eventemitter) * * @memberof Mocha * @public * @class * @param {string} title * @param {Context} parentContext + * @param {boolean} root */ -function Suite(title, parentContext) { +function Suite(title, parentContext, root) { if (!utils.isString(title)) { throw createInvalidArgumentTypeError( 'Suite argument "title" must be a string. Received type "' + @@ -70,7 +71,7 @@ function Suite(title, parentContext) { this._beforeAll = []; this._afterEach = []; this._afterAll = []; - this.root = !title; + this.root = root || false; this._timeout = 2000; this._enableTimeouts = true; this._slow = 75; From aa4fd162f6f19f333446ac6d2d6dc0bb29e8aaae Mon Sep 17 00:00:00 2001 From: juergba Date: Fri, 28 Dec 2018 14:44:33 +0100 Subject: [PATCH 2/3] tests --- test/unit/suite.spec.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/test/unit/suite.spec.js b/test/unit/suite.spec.js index 50a0bda332..7992aefae5 100644 --- a/test/unit/suite.spec.js +++ b/test/unit/suite.spec.js @@ -390,7 +390,7 @@ 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(''); + var parentSuite = new Suite('', {}, true); parentSuite.addSuite(this.suite); expect(this.suite.titlePath(), 'to equal', ['A Suite']); }); @@ -493,6 +493,16 @@ describe('Suite', function() { new Suite('Bdd suite', 'root'); }, 'not to throw'); }); + + it('should be root suite', function() { + var rootSuite = new Suite('', {}, true); + expect(rootSuite.root, 'to be', true); + }); + + it('should not be root suite', function() { + var suite = new Suite(''); + expect(suite.root, 'to be', false); + }); }); describe('timeout()', function() { From ac1a0278028796fb0c6dcf20a20b8c3081d4d8bd Mon Sep 17 00:00:00 2001 From: juergba Date: Thu, 3 Jan 2019 11:36:48 +0100 Subject: [PATCH 3/3] suite.js corrections and new tests --- lib/suite.js | 17 ++++++++++------- test/unit/suite.spec.js | 35 +++++++++++++++++++++++------------ 2 files changed, 33 insertions(+), 19 deletions(-) diff --git a/lib/suite.js b/lib/suite.js index 4909c7cca4..a916563a82 100644 --- a/lib/suite.js +++ b/lib/suite.js @@ -41,16 +41,18 @@ exports.create = function(parent, title) { }; /** - * Initialize a new `Suite` with the given `title`, `ctx` and `root`. 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 - * @param {boolean} root + * @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, root) { +function Suite(title, parentContext, isRoot) { if (!utils.isString(title)) { throw createInvalidArgumentTypeError( 'Suite argument "title" must be a string. Received type "' + @@ -71,7 +73,7 @@ function Suite(title, parentContext, root) { this._beforeAll = []; this._afterEach = []; this._afterAll = []; - this.root = root || false; + this.root = isRoot === true; this._timeout = 2000; this._enableTimeouts = true; this._slow = 75; @@ -327,6 +329,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 7992aefae5..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('', {}, true); - parentSuite.addSuite(this.suite); + var rootSuite = new Suite('', {}, true); + rootSuite.addSuite(this.suite); expect(this.suite.titlePath(), 'to equal', ['A Suite']); }); }); @@ -493,16 +514,6 @@ describe('Suite', function() { new Suite('Bdd suite', 'root'); }, 'not to throw'); }); - - it('should be root suite', function() { - var rootSuite = new Suite('', {}, true); - expect(rootSuite.root, 'to be', true); - }); - - it('should not be root suite', function() { - var suite = new Suite(''); - expect(suite.root, 'to be', false); - }); }); describe('timeout()', function() {