Programmatically set test case title with mocha.
While testing my nodejs (or browser) apps with mocha I sometimes feel the need to display input values as title of the test case to explain the test case better. Obviously I can't just console.log
it since it will break the formatting of the reporter. While hacking with it I came across this trick with which we can add to the title programmatically or even change the title entirely, the latter however is not very useful.
So the magic property is this._runnable.title
. Attach any string to it and it will show as title in test report. Append to it to preserve the current title and add some more text
Imagine a simple test case written with mocha:
var assert = require('assert');
describe("My Test", function(){
it("should pass with input ", function(){
var input = "This will be appended at the end of the title";
this._runnable.title = this._runnable.title + input;
assert(typeof input === 'string');
});
});
And running mocha /path/to/test/file/ --reporter spec
gives following report.
My Test
✓ should pass with input This will be appended at the end of the title
✔ 1 test complete (5ms)
I usually use this to attach inputs that I am testing against at the end of the title. It would be neat if mocha supported such feature directly without actually touching the _runnable
property since its a private one and end user isn't supposed to hack with it. Anyhow quite useful for such use case. If you too support the idea +1
this PR https://github.com/visionmedia/mocha/pull/918
Written by Juzer Ali
Related protips
1 Response
Thank you for your article. It was incredibly interesting for me to read. Got all the important information written down. Can you tell me more about the test case? This is the topic of my test.