From 30507951d91b4db33124bc83641af8ff4644af64 Mon Sep 17 00:00:00 2001 From: Eugene Obrezkov Date: Thu, 21 Apr 2016 19:02:27 +0300 Subject: [PATCH] console: timeEnd() with no label emits warning When timeEnd() provided with label that doesn't exists it emits warning in the console, so developer get know about it. PR-URL: https://github.com/nodejs/node/pull/5901 Reviewed-By: Colin Ihrig Reviewed-By: James M Snell Reviewed-By: Sakthipriyan Vairamani --- lib/console.js | 5 +++-- test/parallel/test-console.js | 10 +++++++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/console.js b/lib/console.js index 646e268624dd8f..2359cb36e011d0 100644 --- a/lib/console.js +++ b/lib/console.js @@ -67,9 +67,10 @@ Console.prototype.time = function(label) { Console.prototype.timeEnd = function(label) { - var time = this._times.get(label); + const time = this._times.get(label); if (!time) { - throw new Error(`No such label: ${label}`); + process.emitWarning(`No such label '${label}' for console.timeEnd()`); + return; } const duration = process.hrtime(time); const ms = duration[0] * 1000 + duration[1] / 1e6; diff --git a/test/parallel/test-console.js b/test/parallel/test-console.js index 8540db6169b09c..bacf0c1afbb0a1 100644 --- a/test/parallel/test-console.js +++ b/test/parallel/test-console.js @@ -1,6 +1,6 @@ 'use strict'; -require('../common'); -var assert = require('assert'); +const common = require('../common'); +const assert = require('assert'); assert.ok(process.stdout.writable); assert.ok(process.stderr.writable); @@ -8,7 +8,11 @@ assert.ok(process.stderr.writable); assert.equal('number', typeof process.stdout.fd); assert.equal('number', typeof process.stderr.fd); -assert.throws(function() { +assert.doesNotThrow(function() { + process.once('warning', common.mustCall((warning) => { + assert(/no such label/.test(warning.message)); + })); + console.timeEnd('no such label'); });