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

indent test contexts #2814

Merged
merged 2 commits into from
Sep 29, 2017
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
14 changes: 13 additions & 1 deletion lib/reporters/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,19 @@ exports.list = function (failures) {
// indent stack trace
stack = stack.replace(/^/gm, ' ');

console.log(fmt, (i + 1), test.fullTitle(), msg, stack);
// indented test title
var testTitle = '';
test.titlePath().forEach(function (str, index) {
if (index !== 0) {
testTitle += '\n ';
}
for (var i = 0; i < index; i++) {
testTitle += ' ';
}
testTitle += str;
});

console.log(fmt, (i + 1), testTitle, msg, stack);
});
};

Expand Down
12 changes: 11 additions & 1 deletion lib/runnable.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,17 @@ Runnable.prototype.currentRetry = function (n) {
* @return {string}
*/
Runnable.prototype.fullTitle = function () {
return this.parent.fullTitle() + ' ' + this.title;
return this.titlePath().join(' ');
};

/**
* Return the title path generated by concatenating the parent's title path with the title.
*
* @api public
* @return {string}
*/
Runnable.prototype.titlePath = function () {
return this.parent.titlePath().concat([this.title]);
};

/**
Expand Down
22 changes: 17 additions & 5 deletions lib/suite.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,13 +355,25 @@ Suite.prototype.addTest = function (test) {
* @return {string}
*/
Suite.prototype.fullTitle = function () {
return this.titlePath().join(' ');
};

/**
* Return the title path generated by recursively concatenating the parent's
* title path.
*
* @api public
* @return {string}
*/
Suite.prototype.titlePath = function () {
var result = [];
if (this.parent) {
var full = this.parent.fullTitle();
if (full) {
return full + ' ' + this.title;
}
result = result.concat(this.parent.titlePath());
}
if (!this.root) {
result.push(this.title);
}
return this.title;
return result;
};

/**
Expand Down
20 changes: 15 additions & 5 deletions test/integration/hook-err.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ describe('hook error handling', function () {
});

describe('before hook error tip', function () {
before(run('hooks/before-hook-error-tip.fixture.js', onlyErrorTitle));
before(run('hooks/before-hook-error-tip.fixture.js', onlyErrorTitle()));
it('should verify results', function () {
assert.deepEqual(
lines,
['1) spec 2 "before all" hook:']
['1) spec 2', '"before all" hook:']
);
});
});
Expand Down Expand Up @@ -107,11 +107,11 @@ describe('hook error handling', function () {
});

describe('async - before hook error tip', function () {
before(run('hooks/before-hook-async-error-tip.fixture.js', onlyErrorTitle));
before(run('hooks/before-hook-async-error-tip.fixture.js', onlyErrorTitle()));
it('should verify results', function () {
assert.deepEqual(
lines,
['1) spec 2 "before all" hook:']
['1) spec 2', '"before all" hook:']
);
});
});
Expand Down Expand Up @@ -213,5 +213,15 @@ function onlyConsoleOutput () {
}

function onlyErrorTitle (line) {
return !!(/^1\)/).exec(line);
var foundErrorTitle = false;
var foundError = false;
return function (line) {
if (!foundErrorTitle) {
foundErrorTitle = !!(/^1\)/).exec(line);
}
if (!foundError) {
foundError = (/Error:/).exec(line);
}
return foundErrorTitle && !foundError;
};
}
4 changes: 2 additions & 2 deletions test/reporters/base.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ var Assert = require('assert').AssertionError;
function makeTest (err) {
return {
err: err,
fullTitle: function () {
return 'test title';
titlePath: function () {
return ['test title'];
}
};
}
Expand Down
9 changes: 9 additions & 0 deletions test/unit/runnable.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
var mocha = require('../../lib/mocha');
var utils = mocha.utils;
var Runnable = mocha.Runnable;
var Suite = mocha.Suite;

describe('Runnable(title, fn)', function () {
// For every test we poison the global time-related methods.
Expand Down Expand Up @@ -87,6 +88,14 @@ describe('Runnable(title, fn)', function () {
});
});

describe('.titlePath()', function () {
it('returns the concatenation of the parent\'s title path and runnable\'s title', function () {
var runnable = new Runnable('bar');
runnable.parent = new Suite('foo');
runnable.titlePath().should.deepEqual(['foo', 'bar']);
});
});

describe('when arity >= 1', function () {
it('should be .async', function () {
var run = new Runnable('foo', function (done) {});
Expand Down
30 changes: 30 additions & 0 deletions test/unit/suite.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,36 @@ describe('Suite', function () {
});
});

describe('.titlePath()', function () {
beforeEach(function () {
this.suite = new Suite('A Suite');
});

describe('when there is no parent', function () {
it('returns the suite title', function () {
this.suite.titlePath().should.deepEqual(['A Suite']);
});
});

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);
this.suite.titlePath().should.deepEqual(['A Suite']);
});
});

describe('the parent is not the root suite', function () {
it('returns the concatenation of parent\'s and suite\'s title', function () {
var parentSuite = new Suite('I am a parent');
parentSuite.addSuite(this.suite);
this.suite.titlePath().should.deepEqual(['I am a parent', 'A Suite']);
});
});
});
});

describe('.total()', function () {
beforeEach(function () {
this.suite = new Suite('A Suite');
Expand Down