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

Commit

Permalink
indent test contexts (mochajs#2814)
Browse files Browse the repository at this point in the history
Add an optional extended description…
  • Loading branch information
charlierudolph authored and ScottFreeCode committed Sep 29, 2017
1 parent 8fd92d4 commit 293ce95
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 14 deletions.
14 changes: 13 additions & 1 deletion lib/reporters/base.js
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
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
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
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
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
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
Expand Up @@ -395,6 +395,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

0 comments on commit 293ce95

Please sign in to comment.