Skip to content

Commit

Permalink
fix(Suite/Test): untitled suite/test-case mochajs#1632
Browse files Browse the repository at this point in the history
Throw a user-friendly error when the suite title or the test-case title
isn't provided.
  • Loading branch information
a8m committed Jul 18, 2015
1 parent 0b9876b commit 710529e
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 2 deletions.
3 changes: 3 additions & 0 deletions lib/suite.js
Expand Up @@ -44,6 +44,9 @@ exports.create = function(parent, title) {
* @param {Context} parentContext
*/
function Suite(title, parentContext) {
if (!utils.isString(title)) {
throw new Error('Expecting suite title as the first argument');

This comment has been minimized.

Copy link
@dasilvacontin

dasilvacontin Jul 27, 2015

What about (or something similar):

'Suite `title` should be a "string" but "' + typeof title + '" was given instead.'

Imo it's more user friendly, makes it easier to understand/find the problem.

}
this.title = title;
function Context() {}
Context.prototype = parentContext;
Expand Down
4 changes: 4 additions & 0 deletions lib/test.js
Expand Up @@ -4,6 +4,7 @@

var Runnable = require('./runnable');
var create = require('lodash.create');
var isString = require('./utils').isString;

/**
* Expose `Test`.
Expand All @@ -19,6 +20,9 @@ module.exports = Test;
* @param {Function} fn
*/
function Test(title, fn) {
if (!isString(title)) {
throw new Error('Expecting test title as the first argument');
}
Runnable.call(this, title, fn);
this.pending = !fn;
this.type = 'test';
Expand Down
2 changes: 1 addition & 1 deletion test/acceptance/throw.js
Expand Up @@ -7,7 +7,7 @@ describe('a test that throws', function () {
var suite, runner;

beforeEach(function(){
suite = new Suite(null, 'root');
suite = new Suite('Suite', 'root');
runner = new Runner(suite);
})

Expand Down
2 changes: 1 addition & 1 deletion test/runner.js
Expand Up @@ -7,7 +7,7 @@ describe('Runner', function(){
var suite, runner;

beforeEach(function(){
suite = new Suite(null, 'root');
suite = new Suite('Suite', 'root');
runner = new Runner(suite);
})

Expand Down
38 changes: 38 additions & 0 deletions test/suite.js
Expand Up @@ -393,4 +393,42 @@ describe('Suite', function(){
});

});

describe('initialization', function() {
it('should throw an error if the title isn\'t a string', function() {
(function() {
new Suite(undefined, 'root');
}).should.throw();

(function() {
new Suite(function(){}, 'root');
}).should.throw();
});

it('should not throw if the title is a string', function() {
(function() {
new Suite('Bdd suite', 'root');
}).should.not.throw();
});
});
});

describe('Test', function() {
describe('initialization', function() {
it('should throw an error if the title isn\'t a string', function() {
(function() {
new Test(function(){});
}).should.throw();

(function() {
new Test(undefined, function(){});
}).should.throw();
});

it('should not throw if the title is a string', function() {
(function() {
new Test('test-case', function(){});
}).should.not.throw();
});
});
});

0 comments on commit 710529e

Please sign in to comment.