• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2require('../common');
3
4// This test ensures that console methods cannot be invoked as constructors and
5// that their name is always correct.
6
7const assert = require('assert');
8
9const { Console } = console;
10const newInstance = new Console(process.stdout);
11const err = TypeError;
12
13const methods = [
14  'log',
15  'warn',
16  'dir',
17  'time',
18  'timeEnd',
19  'timeLog',
20  'trace',
21  'assert',
22  'clear',
23  'count',
24  'countReset',
25  'group',
26  'groupEnd',
27  'table',
28  'debug',
29  'info',
30  'dirxml',
31  'error',
32  'groupCollapsed',
33];
34
35const alternateNames = {
36  debug: 'log',
37  info: 'log',
38  dirxml: 'log',
39  error: 'warn',
40  groupCollapsed: 'group'
41};
42
43function assertEqualName(method) {
44  try {
45    assert.strictEqual(console[method].name, method);
46  } catch {
47    assert.strictEqual(console[method].name, alternateNames[method]);
48  }
49  try {
50    assert.strictEqual(newInstance[method].name, method);
51  } catch {
52    assert.strictEqual(newInstance[method].name, alternateNames[method]);
53  }
54}
55
56for (const method of methods) {
57  assertEqualName(method);
58
59  assert.throws(() => new console[method](), err);
60  assert.throws(() => new newInstance[method](), err);
61  assert.throws(() => Reflect.construct({}, [], console[method]), err);
62  assert.throws(() => Reflect.construct({}, [], newInstance[method]), err);
63}
64