Skip to content

armgit / mocha

Public Imported from GitHub
0

☕️ Classic, reliable, trusted test framework for Node.js and the browser

indent test contexts (#2814)

Charlie Rudolph8b79c5d20924
diff --git a/lib/reporters/base.js b/lib/reporters/base.js
index 2ccfd1f3..9243ec42 100644
--- a/lib/reporters/base.js
+++ b/lib/reporters/base.js
@@ -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);
});
};
diff --git a/lib/runnable.js b/lib/runnable.js
index ee8c4a34..8194d322 100644
--- a/lib/runnable.js
+++ b/lib/runnable.js
@@ -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]);
};
/**
diff --git a/lib/suite.js b/lib/suite.js
index 13617ab9..c592e46b 100644
--- a/lib/suite.js
+++ b/lib/suite.js
@@ -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;
};
/**
diff --git a/test/integration/hook-err.spec.js b/test/integration/hook-err.spec.js
index 8e0896f0..10d0e97c 100644
--- a/test/integration/hook-err.spec.js
+++ b/test/integration/hook-err.spec.js
@@ -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:']
);
});
});
@@ -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:']
);
});
});
@@ -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;
+ };
}
diff --git a/test/reporters/base.spec.js b/test/reporters/base.spec.js
index 6c0cc587..69c25835 100644
--- a/test/reporters/base.spec.js
+++ b/test/reporters/base.spec.js
@@ -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'];
}
};
}
diff --git a/test/unit/runnable.spec.js b/test/unit/runnable.spec.js
index 8c084281..5ee36297 100644
--- a/test/unit/runnable.spec.js
+++ b/test/unit/runnable.spec.js
@@ -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.
@@ -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) {});
diff --git a/test/unit/suite.spec.js b/test/unit/suite.spec.js
index a4530e3c..0e944229 100644
--- a/test/unit/suite.spec.js
+++ b/test/unit/suite.spec.js
@@ -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');