Skip to content

Commit

Permalink
fix(Suite/Test): untitled suite/test-case #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 authored and boneskull committed Aug 1, 2016
1 parent 9efb860 commit 7910a92
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 3 deletions.
3 changes: 3 additions & 0 deletions lib/suite.js
Expand Up @@ -41,6 +41,9 @@ exports.create = function(parent, title) {
* @param {Context} parentContext
*/
function Suite(title, parentContext) {
if (!utils.isString(title)) {
throw new Error('Suite `title` should be a "string" but "' + typeof title + '" was given instead.');
}
this.title = title;
function Context() {}
Context.prototype = parentContext;
Expand Down
6 changes: 5 additions & 1 deletion lib/test.js
Expand Up @@ -3,7 +3,8 @@
*/

var Runnable = require('./runnable');
var inherits = require('./utils').inherits;
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('Test `title` should be a "string" but "' + typeof title + '" was given instead.');
}
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 @@ -6,7 +6,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 @@ -398,4 +398,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 7910a92

Please sign in to comment.