From 442b6a0e3d7ce929f6990f0d3c3d48e936f664a2 Mon Sep 17 00:00:00 2001 From: Ariel Mashraki Date: Thu, 12 Nov 2015 11:38:35 +0200 Subject: [PATCH] fix(Suite/Test): untitled suite/test-case #1632 Throw a user-friendly error when the suite title or the test-case title isn't provided. --- lib/suite.js | 3 +++ lib/test.js | 6 +++++- test/acceptance/throw.js | 2 +- test/runner.js | 2 +- test/suite.js | 38 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 48 insertions(+), 3 deletions(-) diff --git a/lib/suite.js b/lib/suite.js index d43dd45604..74054dd0c6 100644 --- a/lib/suite.js +++ b/lib/suite.js @@ -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; diff --git a/lib/test.js b/lib/test.js index a95cd31a48..1e00c6f5cc 100644 --- a/lib/test.js +++ b/lib/test.js @@ -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`. @@ -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'; diff --git a/test/acceptance/throw.js b/test/acceptance/throw.js index 1016a19efe..46cfbad6ad 100644 --- a/test/acceptance/throw.js +++ b/test/acceptance/throw.js @@ -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); }) diff --git a/test/runner.js b/test/runner.js index 7673a8384e..37aeb22d6f 100644 --- a/test/runner.js +++ b/test/runner.js @@ -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); }) diff --git a/test/suite.js b/test/suite.js index 6588e24817..7fead0745a 100644 --- a/test/suite.js +++ b/test/suite.js @@ -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(); + }); + }); +}); \ No newline at end of file