Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set "root" property on Suite correctly #3632

Merged
merged 3 commits into from
Jan 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -41,15 +41,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.
*/
plroebuck marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -70,7 +73,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 @@ -326,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());
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