From 710529eebd752d6207281cb1234db7b61be36109 Mon Sep 17 00:00:00 2001 From: Ariel Mashraki Date: Sat, 18 Jul 2015 16:18:53 +0300 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 | 4 ++++ test/acceptance/throw.js | 2 +- test/runner.js | 2 +- test/suite.js | 38 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 47 insertions(+), 2 deletions(-) diff --git a/lib/suite.js b/lib/suite.js index eb89fb346e..68c54e7fb4 100644 --- a/lib/suite.js +++ b/lib/suite.js @@ -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.title = title; function Context() {} Context.prototype = parentContext; diff --git a/lib/test.js b/lib/test.js index 057e772824..6b43da916f 100644 --- a/lib/test.js +++ b/lib/test.js @@ -4,6 +4,7 @@ var Runnable = require('./runnable'); 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('Expecting test title as the first argument'); + } Runnable.call(this, title, fn); this.pending = !fn; this.type = 'test'; diff --git a/test/acceptance/throw.js b/test/acceptance/throw.js index ac74f22c4a..53e5a6b7bb 100644 --- a/test/acceptance/throw.js +++ b/test/acceptance/throw.js @@ -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); }) diff --git a/test/runner.js b/test/runner.js index 08a5ce3e78..1cd20f0ce9 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 011b3fb2b8..a26b9541fc 100644 --- a/test/suite.js +++ b/test/suite.js @@ -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(); + }); + }); +}); \ No newline at end of file