From b9f02728de6600a7a00b08c0ae5e5338ca57b953 Mon Sep 17 00:00:00 2001 From: Gus Caplan Date: Mon, 30 Apr 2018 19:44:41 -0500 Subject: [PATCH] console: console.time() should not reset a timer when it exists --- lib/console.js | 4 ++++ test/parallel/test-console.js | 14 ++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/lib/console.js b/lib/console.js index a0158ec6643782..07f177d595deb2 100644 --- a/lib/console.js +++ b/lib/console.js @@ -225,6 +225,10 @@ Console.prototype.dir = function dir(object, options) { Console.prototype.time = function time(label = 'default') { // Coerces everything other than Symbol to a string label = `${label}`; + if (this._times.has(label)) { + process.emitWarning(`Label '${label}' already exists for console.time()`); + return; + } this._times.set(label, process.hrtime()); }; diff --git a/test/parallel/test-console.js b/test/parallel/test-console.js index 4bac35c65dfc70..64df389be691c6 100644 --- a/test/parallel/test-console.js +++ b/test/parallel/test-console.js @@ -138,6 +138,20 @@ console.timeEnd(); console.time(NaN); console.timeEnd(NaN); +// make sure calling time twice without timeEnd doesn't reset the timer. +console.time('test'); +const time = console._times.get('test'); +setTimeout(() => { + assert.deepStrictEqual(console._times.get('test'), time); + common.expectWarning( + 'Warning', + 'Label \'test\' already exists for console.time()', + common.noWarnCode); + console.time('test'); + console.timeEnd('test'); +}, 1); + + console.assert(false, '%s should', 'console.assert', 'not throw'); assert.strictEqual(errStrings[errStrings.length - 1], 'Assertion failed: console.assert should not throw\n');