Skip to content
This repository has been archived by the owner on Dec 4, 2023. It is now read-only.

Commit

Permalink
Set "root" property on Suite correctly (mochajs#3632)
Browse files Browse the repository at this point in the history
* suite constructor

* tests

* suite.js corrections and new tests
  • Loading branch information
juergba authored Jan 16, 2019
1 parent 04f0c6d commit 4518afa
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
16 changes: 10 additions & 6 deletions lib/suite.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 "' +
Expand All @@ -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;
Expand Down Expand Up @@ -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());
Expand Down
25 changes: 23 additions & 2 deletions test/unit/suite.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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']);
});
});
Expand Down

0 comments on commit 4518afa

Please sign in to comment.