From 9099f90693af19754158a64039db4475c560e317 Mon Sep 17 00:00:00 2001 From: Michael Barrett Date: Fri, 18 Jul 2014 09:25:12 +0100 Subject: [PATCH] add test for error when invalid unit used --- test/timestring.js | 10 +++++++++- timestring.js | 7 +++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/test/timestring.js b/test/timestring.js index 8a54209..39301ee 100644 --- a/test/timestring.js +++ b/test/timestring.js @@ -3,7 +3,15 @@ var expect = chai.expect; var timestring = require('../timestring'); describe('timestring', function() { - it('should expose a method on String.prototype that will attempt to parse the string as a timestring', function(done){ + it('throws an error when an invalid unit is used in the timestring', function(done) { + var ts = new timestring(); + + expect(ts.parse.bind(ts, '1g')).to.throw(Error); + + done(); + }); + + it('should expose a method on String.prototype that will parse the string as a timestring', function(done){ var str = '1min'; // no arguments passed diff --git a/timestring.js b/timestring.js index 281809f..bab6110 100644 --- a/timestring.js +++ b/timestring.js @@ -12,8 +12,7 @@ // merge default settings with user settings settings = settings || {}; - this.settings = {}; - for (var d in defaults) { this.settings[d] = defaults[d]; } + this.settings = defaults; for (var s in settings) { this.settings[s] = settings[s]; } // time units @@ -56,8 +55,8 @@ } } - // throw exception if invalid unit is passed - throw 'The unit [' + unit + '] is not supported by timestring'; + // throw error if invalid unit was passed + throw new Error('The unit [' + unit + '] is not supported by timestring'); } // convert a value to a specific unit