summaryrefslogtreecommitdiff
path: root/test/parallel/test-console-methods.js
blob: d338cc1f807f2c850423c4fa9e14c8a9e7e37a2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
'use strict';
require('../common');

// This test ensures that console methods cannot be invoked as constructors and
// that their name is always correct.

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',
];

const alternateNames = {
  debug: 'log',
  info: 'log',
  dirxml: 'log',
  error: 'warn',
  groupCollapsed: 'group'
};

function assertEqualName(method) {
  try {
    assert.strictEqual(console[method].name, method);
  } catch {
    assert.strictEqual(console[method].name, alternateNames[method]);
  }
  try {
    assert.strictEqual(newInstance[method].name, method);
  } catch {
    assert.strictEqual(newInstance[method].name, alternateNames[method]);
  }
}

for (const method of methods) {
  assertEqualName(method);

  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);
}