Skip to content

Commit addb6a0

Browse files
committed
fix(Suite/Test): untitled suite/test-case mochajs#1632
Throw a user-friendly error when the suite title or the test-case title isn't provided.
1 parent 0b9876b commit addb6a0

File tree

5 files changed

+47
-2
lines changed

5 files changed

+47
-2
lines changed

lib/suite.js

+3
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ exports.create = function(parent, title) {
4444
* @param {Context} parentContext
4545
*/
4646
function Suite(title, parentContext) {
47+
if (!utils.isString(title)) {
48+
throw new Error('Suite `title` should be a "string" but "' + typeof title + '" was given instead.');
49+
}
4750
this.title = title;
4851
function Context() {}
4952
Context.prototype = parentContext;

lib/test.js

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
var Runnable = require('./runnable');
66
var create = require('lodash.create');
7+
var isString = require('./utils').isString;
78

89
/**
910
* Expose `Test`.
@@ -19,6 +20,9 @@ module.exports = Test;
1920
* @param {Function} fn
2021
*/
2122
function Test(title, fn) {
23+
if (!isString(title)) {
24+
throw new Error('Test `title` should be a "string" but "' + typeof title + '" was given instead.');
25+
}
2226
Runnable.call(this, title, fn);
2327
this.pending = !fn;
2428
this.type = 'test';

test/acceptance/throw.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ describe('a test that throws', function () {
77
var suite, runner;
88

99
beforeEach(function(){
10-
suite = new Suite(null, 'root');
10+
suite = new Suite('Suite', 'root');
1111
runner = new Runner(suite);
1212
})
1313

test/runner.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ describe('Runner', function(){
77
var suite, runner;
88

99
beforeEach(function(){
10-
suite = new Suite(null, 'root');
10+
suite = new Suite('Suite', 'root');
1111
runner = new Runner(suite);
1212
})
1313

test/suite.js

+38
Original file line numberDiff line numberDiff line change
@@ -393,4 +393,42 @@ describe('Suite', function(){
393393
});
394394

395395
});
396+
397+
describe('initialization', function() {
398+
it('should throw an error if the title isn\'t a string', function() {
399+
(function() {
400+
new Suite(undefined, 'root');
401+
}).should.throw();
402+
403+
(function() {
404+
new Suite(function(){}, 'root');
405+
}).should.throw();
406+
});
407+
408+
it('should not throw if the title is a string', function() {
409+
(function() {
410+
new Suite('Bdd suite', 'root');
411+
}).should.not.throw();
412+
});
413+
});
396414
});
415+
416+
describe('Test', function() {
417+
describe('initialization', function() {
418+
it('should throw an error if the title isn\'t a string', function() {
419+
(function() {
420+
new Test(function(){});
421+
}).should.throw();
422+
423+
(function() {
424+
new Test(undefined, function(){});
425+
}).should.throw();
426+
});
427+
428+
it('should not throw if the title is a string', function() {
429+
(function() {
430+
new Test('test-case', function(){});
431+
}).should.not.throw();
432+
});
433+
});
434+
});

0 commit comments

Comments
 (0)