node/test/parallel/test-console-methods.js
Thomas e9ed6b988f
console: prevent constructing console methods
Ref: https://github.com/nodejs/node/issues/25987

PR-URL: https://github.com/nodejs/node/pull/26096
Refs: https://github.com/nodejs/node/issues/25987
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Gabriel Schulhof <gabriel.schulhof@intel.com>
2019-03-02 00:07:53 +01:00

41 lines
789 B
JavaScript

'use strict';
require('../common');
// This test ensures that console methods
// cannot be invoked as constructors
const assert = require('assert');
const { Console } = console;
const newInstance = new Console(process.stdout);
const err = TypeError;
const methods = [
'log',
'warn',
'dir',
'time',
'timeEnd',
'timeLog',
'trace',
'assert',
'clear',
'count',
'countReset',
'group',
'groupEnd',
'table',
'debug',
'info',
'dirxml',
'error',
'groupCollapsed',
];
for (const method of methods) {
assert.throws(() => new console[method](), err);
assert.throws(() => new newInstance[method](), err);
assert.throws(() => Reflect.construct({}, [], console[method]), err);
assert.throws(() => Reflect.construct({}, [], newInstance[method]), err);
}