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

Commit 293ce95

Browse files
charlierudolphScottFreeCode
authored andcommitted
indent test contexts (mochajs#2814)
Add an optional extended description…
1 parent 8fd92d4 commit 293ce95

File tree

7 files changed

+97
-14
lines changed

7 files changed

+97
-14
lines changed

lib/reporters/base.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,19 @@ exports.list = function (failures) {
222222
// indent stack trace
223223
stack = stack.replace(/^/gm, ' ');
224224

225-
console.log(fmt, (i + 1), test.fullTitle(), msg, stack);
225+
// indented test title
226+
var testTitle = '';
227+
test.titlePath().forEach(function (str, index) {
228+
if (index !== 0) {
229+
testTitle += '\n ';
230+
}
231+
for (var i = 0; i < index; i++) {
232+
testTitle += ' ';
233+
}
234+
testTitle += str;
235+
});
236+
237+
console.log(fmt, (i + 1), testTitle, msg, stack);
226238
});
227239
};
228240

lib/runnable.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,17 @@ Runnable.prototype.currentRetry = function (n) {
179179
* @return {string}
180180
*/
181181
Runnable.prototype.fullTitle = function () {
182-
return this.parent.fullTitle() + ' ' + this.title;
182+
return this.titlePath().join(' ');
183+
};
184+
185+
/**
186+
* Return the title path generated by concatenating the parent's title path with the title.
187+
*
188+
* @api public
189+
* @return {string}
190+
*/
191+
Runnable.prototype.titlePath = function () {
192+
return this.parent.titlePath().concat([this.title]);
183193
};
184194

185195
/**

lib/suite.js

+17-5
Original file line numberDiff line numberDiff line change
@@ -355,13 +355,25 @@ Suite.prototype.addTest = function (test) {
355355
* @return {string}
356356
*/
357357
Suite.prototype.fullTitle = function () {
358+
return this.titlePath().join(' ');
359+
};
360+
361+
/**
362+
* Return the title path generated by recursively concatenating the parent's
363+
* title path.
364+
*
365+
* @api public
366+
* @return {string}
367+
*/
368+
Suite.prototype.titlePath = function () {
369+
var result = [];
358370
if (this.parent) {
359-
var full = this.parent.fullTitle();
360-
if (full) {
361-
return full + ' ' + this.title;
362-
}
371+
result = result.concat(this.parent.titlePath());
372+
}
373+
if (!this.root) {
374+
result.push(this.title);
363375
}
364-
return this.title;
376+
return result;
365377
};
366378

367379
/**

test/integration/hook-err.spec.js

+15-5
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ describe('hook error handling', function () {
1919
});
2020

2121
describe('before hook error tip', function () {
22-
before(run('hooks/before-hook-error-tip.fixture.js', onlyErrorTitle));
22+
before(run('hooks/before-hook-error-tip.fixture.js', onlyErrorTitle()));
2323
it('should verify results', function () {
2424
assert.deepEqual(
2525
lines,
26-
['1) spec 2 "before all" hook:']
26+
['1) spec 2', '"before all" hook:']
2727
);
2828
});
2929
});
@@ -107,11 +107,11 @@ describe('hook error handling', function () {
107107
});
108108

109109
describe('async - before hook error tip', function () {
110-
before(run('hooks/before-hook-async-error-tip.fixture.js', onlyErrorTitle));
110+
before(run('hooks/before-hook-async-error-tip.fixture.js', onlyErrorTitle()));
111111
it('should verify results', function () {
112112
assert.deepEqual(
113113
lines,
114-
['1) spec 2 "before all" hook:']
114+
['1) spec 2', '"before all" hook:']
115115
);
116116
});
117117
});
@@ -213,5 +213,15 @@ function onlyConsoleOutput () {
213213
}
214214

215215
function onlyErrorTitle (line) {
216-
return !!(/^1\)/).exec(line);
216+
var foundErrorTitle = false;
217+
var foundError = false;
218+
return function (line) {
219+
if (!foundErrorTitle) {
220+
foundErrorTitle = !!(/^1\)/).exec(line);
221+
}
222+
if (!foundError) {
223+
foundError = (/Error:/).exec(line);
224+
}
225+
return foundErrorTitle && !foundError;
226+
};
217227
}

test/reporters/base.spec.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ var Assert = require('assert').AssertionError;
88
function makeTest (err) {
99
return {
1010
err: err,
11-
fullTitle: function () {
12-
return 'test title';
11+
titlePath: function () {
12+
return ['test title'];
1313
}
1414
};
1515
}

test/unit/runnable.spec.js

+9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
var mocha = require('../../lib/mocha');
44
var utils = mocha.utils;
55
var Runnable = mocha.Runnable;
6+
var Suite = mocha.Suite;
67

78
describe('Runnable(title, fn)', function () {
89
// For every test we poison the global time-related methods.
@@ -87,6 +88,14 @@ describe('Runnable(title, fn)', function () {
8788
});
8889
});
8990

91+
describe('.titlePath()', function () {
92+
it('returns the concatenation of the parent\'s title path and runnable\'s title', function () {
93+
var runnable = new Runnable('bar');
94+
runnable.parent = new Suite('foo');
95+
runnable.titlePath().should.deepEqual(['foo', 'bar']);
96+
});
97+
});
98+
9099
describe('when arity >= 1', function () {
91100
it('should be .async', function () {
92101
var run = new Runnable('foo', function (done) {});

test/unit/suite.spec.js

+30
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,36 @@ describe('Suite', function () {
395395
});
396396
});
397397

398+
describe('.titlePath()', function () {
399+
beforeEach(function () {
400+
this.suite = new Suite('A Suite');
401+
});
402+
403+
describe('when there is no parent', function () {
404+
it('returns the suite title', function () {
405+
this.suite.titlePath().should.deepEqual(['A Suite']);
406+
});
407+
});
408+
409+
describe('when there is a parent', function () {
410+
describe('the parent is the root suite', function () {
411+
it('returns the suite title', function () {
412+
var parentSuite = new Suite('');
413+
parentSuite.addSuite(this.suite);
414+
this.suite.titlePath().should.deepEqual(['A Suite']);
415+
});
416+
});
417+
418+
describe('the parent is not the root suite', function () {
419+
it('returns the concatenation of parent\'s and suite\'s title', function () {
420+
var parentSuite = new Suite('I am a parent');
421+
parentSuite.addSuite(this.suite);
422+
this.suite.titlePath().should.deepEqual(['I am a parent', 'A Suite']);
423+
});
424+
});
425+
});
426+
});
427+
398428
describe('.total()', function () {
399429
beforeEach(function () {
400430
this.suite = new Suite('A Suite');

0 commit comments

Comments
 (0)