Skip to content

Commit d4164ca

Browse files
domfarolinojasnell
authored andcommitted
console: console.countReset() should emit warning
The Console Standard specifies that console.countReset() should emit some type of a warning when given a label that has no previous account associated with it. This PR brings node's implementation of console.countReset() up-to-spec and adds a test asserting that a warning is emitted. Fixes: #20524 PR-URL: #21649 Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com> Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Minwoo Jung <minwoo@nodesource.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 85b0f16 commit d4164ca

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

lib/console.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ function Console(options /* or: stdout, stderr, ignoreErrors = true */) {
107107
if (typeof colorMode !== 'boolean' && colorMode !== 'auto')
108108
throw new ERR_INVALID_ARG_VALUE('colorMode', colorMode);
109109

110+
// Corresponds to https://console.spec.whatwg.org/#count-map
110111
this[kCounts] = new Map();
111112
this[kColorMode] = colorMode;
112113

@@ -308,12 +309,14 @@ Console.prototype.count = function count(label = 'default') {
308309
this.log(`${label}: ${count}`);
309310
};
310311

311-
// Not yet defined by the https://console.spec.whatwg.org, but
312-
// proposed to be added and currently implemented by Edge. Having
313-
// the ability to reset counters is important to help prevent
314-
// the counter from being a memory leak.
312+
// Defined by: https://console.spec.whatwg.org/#countreset
315313
Console.prototype.countReset = function countReset(label = 'default') {
316314
const counts = this[kCounts];
315+
if (!counts.has(label)) {
316+
process.emitWarning(`Count for '${label}' does not exist`);
317+
return;
318+
}
319+
317320
counts.delete(`${label}`);
318321
};
319322

test/parallel/test-console.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,16 @@ if (common.isMainThread) {
3535
common.expectWarning(
3636
'Warning',
3737
[
38-
['No such label \'nolabel\' for console.timeEnd()', common.noWarnCode],
39-
['No such label \'nolabel\' for console.timeLog()', common.noWarnCode],
38+
['Count for \'noLabel\' does not exist', common.noWarnCode],
39+
['No such label \'noLabel\' for console.timeLog()', common.noWarnCode],
40+
['No such label \'noLabel\' for console.timeEnd()', common.noWarnCode],
4041
['Label \'test\' already exists for console.time()', common.noWarnCode]
4142
]
4243
);
4344

44-
console.timeEnd('nolabel');
45-
console.timeLog('nolabel');
45+
console.countReset('noLabel');
46+
console.timeLog('noLabel');
47+
console.timeEnd('noLabel');
4648

4749
console.time('label');
4850
console.timeEnd('label');
@@ -245,6 +247,6 @@ common.hijackStderr(common.mustCall(function(data) {
245247

246248
// stderr.write will catch sync error, so use `process.nextTick` here
247249
process.nextTick(function() {
248-
assert.strictEqual(data.includes('nolabel'), true);
250+
assert.strictEqual(data.includes('noLabel'), true);
249251
});
250252
}));

0 commit comments

Comments
 (0)