console: throw when no such label exists in `console.timeEnd`
authorMaciej Małecki <maciej.malecki@notimplemented.org>
Sun, 29 Apr 2012 13:17:16 +0000 (15:17 +0200)
committerBen Noordhuis <info@bnoordhuis.nl>
Sun, 29 Apr 2012 20:27:45 +0000 (22:27 +0200)
Test included.

lib/console.js
test/simple/test-console.js

index ac8912f..64d21a4 100644 (file)
@@ -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);
 };
 
index 3a73286..d07ca50 100644 (file)
@@ -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');
+});