Skip to content

Commit 1c84579

Browse files
ghaiklorjasnell
authored andcommitted
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: #5901 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
1 parent 6c1e5ad commit 1c84579

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

lib/console.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,10 @@ Console.prototype.time = function(label) {
6767

6868

6969
Console.prototype.timeEnd = function(label) {
70-
var time = this._times.get(label);
70+
const time = this._times.get(label);
7171
if (!time) {
72-
throw new Error(`No such label: ${label}`);
72+
process.emitWarning(`No such label '${label}' for console.timeEnd()`);
73+
return;
7374
}
7475
const duration = process.hrtime(time);
7576
const ms = duration[0] * 1000 + duration[1] / 1e6;

test/parallel/test-console.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
'use strict';
2-
require('../common');
3-
var assert = require('assert');
2+
const common = require('../common');
3+
const assert = require('assert');
44

55
assert.ok(process.stdout.writable);
66
assert.ok(process.stderr.writable);
77
// Support legacy API
88
assert.equal('number', typeof process.stdout.fd);
99
assert.equal('number', typeof process.stderr.fd);
1010

11-
assert.throws(function() {
11+
assert.doesNotThrow(function() {
12+
process.once('warning', common.mustCall((warning) => {
13+
assert(/no such label/.test(warning.message));
14+
}));
15+
1216
console.timeEnd('no such label');
1317
});
1418

0 commit comments

Comments
 (0)