summaryrefslogtreecommitdiff
path: root/test/parallel/test-assert-checktag.js
blob: c0b80adffcaa1a4ac5bd766a4ab4e5a327fa637b (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
64
65
'use strict';
require('../common');
const assert = require('assert');

// Disable colored output to prevent color codes from breaking assertion
// message comparisons. This should only be an issue when process.stdout
// is a TTY.
if (process.stdout.isTTY)
  process.env.NODE_DISABLE_COLORS = '1';

// Turn off no-restricted-properties because we are testing deepEqual!
/* eslint-disable no-restricted-properties */

// See https://github.com/nodejs/node/issues/10258
{
  const date = new Date('2016');
  function FakeDate() {}
  FakeDate.prototype = Date.prototype;
  const fake = new FakeDate();

  assert.notDeepEqual(date, fake);
  assert.notDeepEqual(fake, date);

  // For deepStrictEqual we check the runtime type,
  // then reveal the fakeness of the fake date
  assert.throws(
    () => assert.deepStrictEqual(date, fake),
    {
      message: 'Expected values to be strictly deep-equal:\n' +
               '+ actual - expected\n\n+ 2016-01-01T00:00:00.000Z\n- Date {}'
    }
  );
  assert.throws(
    () => assert.deepStrictEqual(fake, date),
    {
      message: 'Expected values to be strictly deep-equal:\n' +
               '+ actual - expected\n\n+ Date {}\n- 2016-01-01T00:00:00.000Z'
    }
  );
}

{  // At the moment global has its own type tag
  const fakeGlobal = {};
  Object.setPrototypeOf(fakeGlobal, Object.getPrototypeOf(global));
  for (const prop of Object.keys(global)) {
    fakeGlobal[prop] = global[prop];
  }
  assert.notDeepEqual(fakeGlobal, global);
  // Message will be truncated anyway, don't validate
  assert.throws(() => assert.deepStrictEqual(fakeGlobal, global),
                assert.AssertionError);
}

{ // At the moment process has its own type tag
  const fakeProcess = {};
  Object.setPrototypeOf(fakeProcess, Object.getPrototypeOf(process));
  for (const prop of Object.keys(process)) {
    fakeProcess[prop] = process[prop];
  }
  assert.notDeepEqual(fakeProcess, process);
  // Message will be truncated anyway, don't validate
  assert.throws(() => assert.deepStrictEqual(fakeProcess, process),
                assert.AssertionError);
}
/* eslint-enable */