From 77c18d1e1b470d99af317a2b1ed4b7e9bb81d5b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Ma=C5=82ecki?= Date: Sun, 29 Apr 2012 15:17:16 +0200 Subject: [PATCH] console: throw when no such label exists in `console.timeEnd` Test included. --- lib/console.js | 6 +++++- test/simple/test-console.js | 9 +++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/console.js b/lib/console.js index ac8912f828e..64d21a4266c 100644 --- a/lib/console.js +++ b/lib/console.js @@ -49,7 +49,11 @@ exports.time = function(label) { exports.timeEnd = function(label) { - var duration = Date.now() - times[label]; + var time = times[label]; + if (!time) { + throw new Error('No such label: ' + label); + } + var duration = Date.now() - time; exports.log('%s: %dms', label, duration); }; diff --git a/test/simple/test-console.js b/test/simple/test-console.js index 3a73286def8..d07ca50ae4a 100644 --- a/test/simple/test-console.js +++ b/test/simple/test-console.js @@ -50,3 +50,12 @@ assert.equal('foo bar hop\n', strings.shift()); assert.equal("{ slashes: '\\\\\\\\' }\n", strings.shift()); process.stderr.write('hello world'); + +assert.throws(function () { + console.timeEnd('no such label'); +}); + +assert.doesNotThrow(function () { + console.time('label'); + console.timeEnd('label'); +});