summaryrefslogtreecommitdiff
path: root/test/parallel/test-util-deprecate.js
blob: 1b4a5e76623743deab6e99cb34070a47ca678446 (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
'use strict';

require('../common');

// Tests basic functionality of util.deprecate().

const assert = require('assert');
const util = require('util');

const expectedWarnings = new Map();

// Emits deprecation only once if same function is called.
{
  const msg = 'fhqwhgads';
  const fn = util.deprecate(() => {}, msg);
  expectedWarnings.set(msg, { code: undefined, count: 1 });
  fn();
  fn();
}

// Emits deprecation twice for different functions.
{
  const msg = 'sterrance';
  const fn1 = util.deprecate(() => {}, msg);
  const fn2 = util.deprecate(() => {}, msg);
  expectedWarnings.set(msg, { code: undefined, count: 2 });
  fn1();
  fn2();
}

// Emits deprecation only once if optional code is the same, even for different
// functions.
{
  const msg = 'cannonmouth';
  const code = 'deprecatesque';
  const fn1 = util.deprecate(() => {}, msg, code);
  const fn2 = util.deprecate(() => {}, msg, code);
  expectedWarnings.set(msg, { code, count: 1 });
  fn1();
  fn2();
  fn1();
  fn2();
}

process.on('warning', (warning) => {
  assert.strictEqual(warning.name, 'DeprecationWarning');
  assert.ok(expectedWarnings.has(warning.message));
  const expected = expectedWarnings.get(warning.message);
  assert.strictEqual(warning.code, expected.code);
  expected.count = expected.count - 1;
  if (expected.count === 0)
    expectedWarnings.delete(warning.message);
});

process.on('exit', () => {
  assert.deepStrictEqual(expectedWarnings, new Map());
});