|
| 1 | +'use strict'; |
| 2 | +require('../common'); |
| 3 | +const assert = require('assert'); |
| 4 | +const util = require('util'); |
| 5 | + |
| 6 | +// Template tag function turning an error message into a RegExp |
| 7 | +// for assert.throws() |
| 8 | +function re(literals, ...values) { |
| 9 | + let result = literals[0]; |
| 10 | + for (const [i, value] of values.entries()) { |
| 11 | + const str = util.inspect(value); |
| 12 | + // Need to escape special characters. |
| 13 | + result += str.replace(/[\\^$.*+?()[\]{}|=!<>:-]/g, '\\$&'); |
| 14 | + result += literals[i + 1]; |
| 15 | + } |
| 16 | + return new RegExp('^AssertionError: ' + result + '$'); |
| 17 | +} |
| 18 | + |
| 19 | +// Turn off no-restricted-properties because we are testing deepEqual! |
| 20 | +/* eslint-disable no-restricted-properties */ |
| 21 | + |
| 22 | +// See https://github.com/nodejs/node/issues/10258 |
| 23 | +{ |
| 24 | + const date = new Date('2016'); |
| 25 | + function FakeDate() {} |
| 26 | + FakeDate.prototype = Date.prototype; |
| 27 | + const fake = new FakeDate(); |
| 28 | + |
| 29 | + assert.doesNotThrow(() => assert.deepEqual(date, fake)); |
| 30 | + assert.doesNotThrow(() => assert.deepEqual(fake, date)); |
| 31 | + |
| 32 | + // For deepStrictEqual we check the runtime type, |
| 33 | + // then reveal the fakeness of the fake date |
| 34 | + assert.throws(() => assert.deepStrictEqual(date, fake), |
| 35 | + re`${date} deepStrictEqual Date {}`); |
| 36 | + assert.throws(() => assert.deepStrictEqual(fake, date), |
| 37 | + re`Date {} deepStrictEqual ${date}`); |
| 38 | +} |
| 39 | + |
| 40 | +{ // At the moment global has its own type tag |
| 41 | + const fakeGlobal = {}; |
| 42 | + Object.setPrototypeOf(fakeGlobal, Object.getPrototypeOf(global)); |
| 43 | + for (const prop of Object.keys(global)) { |
| 44 | + fakeGlobal[prop] = global[prop]; |
| 45 | + } |
| 46 | + assert.doesNotThrow(() => assert.deepEqual(fakeGlobal, global)); |
| 47 | + // Message will be truncated anyway, don't validate |
| 48 | + assert.throws(() => assert.deepStrictEqual(fakeGlobal, global)); |
| 49 | +} |
| 50 | + |
| 51 | +{ // At the moment process has its own type tag |
| 52 | + const fakeProcess = {}; |
| 53 | + Object.setPrototypeOf(fakeProcess, Object.getPrototypeOf(process)); |
| 54 | + for (const prop of Object.keys(process)) { |
| 55 | + fakeProcess[prop] = process[prop]; |
| 56 | + } |
| 57 | + assert.doesNotThrow(() => assert.deepEqual(fakeProcess, process)); |
| 58 | + // Message will be truncated anyway, don't validate |
| 59 | + assert.throws(() => assert.deepStrictEqual(fakeProcess, process)); |
| 60 | +} |
| 61 | +/* eslint-enable */ |
0 commit comments