最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

assert.deepStrictEqual(actual, expected[, message])

运维笔记admin23浏览0评论

assert.deepStrictEqual(actual, expected[, message])

assert.deepStrictEqual(actual, expected[, message])

History

actual <any>
expected <any>
message <string> | <Error>

Tests for deep equality between the actual and expected parameters. “Deep” equality means that the enumerable “own” properties of child objects are recursively evaluated also by the following rules.
Comparison details

Primitive values are compared using the SameValue Comparison, used by Object.is().
Type tags of objects should be the same.
[[Prototype]] of objects are compared using the Strict Equality Comparison.
Only enumerable "own" properties are considered.
Error names and messages are always compared, even if these are not enumerable properties.
Enumerable own Symbol properties are compared as well.
Object wrappers are compared both as objects and unwrapped values.
Object properties are compared unordered.
Map keys and Set items are compared unordered.
Recursion stops when both sides differ or both sides encounter a circular reference.
WeakMap and WeakSet comparison does not rely on their values. See below for further details.

const assert = require(‘assert’).strict;

// This fails because 1 !== ‘1’.
assert.deepStrictEqual({ a: 1 }, { a: ‘1’ });
// AssertionError: Expected inputs to be strictly deep-equal:
// + actual - expected
//
// {
// + a: 1
// - a: ‘1’
// }

// The following objects don’t have own properties
const date = new Date();
const object = {};
const fakeDate = {};
Object.setPrototypeOf(fakeDate, Date.prototype);

// Different [[Prototype]]:
assert.deepStrictEqual(object, fakeDate);
// AssertionError: Expected inputs to be strictly deep-equal:
// + actual - expected
//
// + {}
// - Date {}

// Different type tags:
assert.deepStrictEqual(date, fakeDate);
// AssertionError: Expected inputs to be strictly deep-equal:
// + actual - expected
//
// + 2018-04-26T00:49:08.604Z
// - Date {}

assert.deepStrictEqual(NaN, NaN);
// OK, because of the SameValue comparison

// Different unwrapped numbers:
assert.deepStrictEqual(new Number(1), new Number(2));
// AssertionError: Expected inputs to be strictly deep-equal:
// + actual - expected
//
// + [Number: 1]
// - [Number: 2]

assert.deepStrictEqual(new String(‘foo’), Object(‘foo’));
// OK because the object and the string are identical when unwrapped.

assert.deepStrictEqual(-0, -0);
// OK

// Different zeros using the SameValue Comparison:
assert.deepStrictEqual(0, -0);
// AssertionError: Expected inputs to be strictly deep-equal:
// + actual - expected
//
// + 0
// - -0

const symbol1 = Symbol();
const symbol2 = Symbol();
assert.deepStrictEqual({ [symbol1]: 1 }, { [symbol1]: 1 });
// OK, because it is the same symbol on both objects.

assert.deepStrictEqual({ [symbol1]: 1 }, { [symbol2]: 1 });
// AssertionError [ERR_ASSERTION]: Inputs identical but not reference equal:
//
// {
// [Symbol()]: 1
// }

const weakMap1 = new WeakMap();
const weakMap2 = new WeakMap([[{}, {}]]);
const weakMap3 = new WeakMap();
weakMap3.unequal = true;

assert.deepStrictEqual(weakMap1, weakMap2);
// OK, because it is impossible to compare the entries

// Fails because weakMap3 has a property that weakMap1 does not contain:
assert.deepStrictEqual(weakMap1, weakMap3);
// AssertionError: Expected inputs to be strictly deep-equal:
// + actual - expected
//
// WeakMap {
// + [items unknown]
// - [items unknown],
// - unequal: true
// }

If the values are not equal, an AssertionError is thrown with a message property set equal to the value of the message parameter. If the message parameter is undefined, a default error message is assigned. If the message parameter is an instance of an Error then it will be thrown instead of the AssertionError.

translate:
翻译:

测试实际参数和预期参数之间的深度相等。”深度“相等”意味着子对象的可枚举“own”属性也由以下规则递归求值。
比较详细信息

使用Object.is()使用的SameValue比较来比较原语值。
对象的类型标记应该相同。
使用严格的相等比较来比较对象的[Prototype]]。
只考虑可枚举的“own”属性。
即使错误名称和消息不是可枚举属性,也始终会进行比较。
还比较了可枚举的自身符号属性。
对象包装器作为对象和展开值进行比较。
对象属性是无序比较的。
映射键和集合项是无序比较的。
当两边不同或两边都遇到循环引用时,递归停止。
WeakMap和WeakSet的比较并不依赖于它们的价值观。详情见下文。

发布评论

评论列表(0)

  1. 暂无评论