assert.deepStrictEqual(actual, expected[, message])
历史
版本 | 变化 |
---|---|
v18.0.0 | 现在也比较正则表达式 lastIndex 属性。 |
v9.0.0 | 现在比较可枚举的符号属性。 |
v9.0.0 |
|
v8.5.0 |
|
v8.0.0 | 还比较了 |
v6.1.0 | 具有循环引用的对象现在可以用作输入。 |
v6.4.0、v4.7.1 | 现在可以正确处理类型化的数组切片。 |
v5.10.1、v4.4.3 | 正确处理非 |
v1.2.0 | 添加于:v1.2.0 |
参数
测试 actual
和 expected
参数之间的深度相等性。 "Deep" 相等意味着子对象的可枚举"own" 属性也通过以下规则递归评估。
比较细节#
- 使用
Object.is()
- Type tags 个对象应该相同。
[[Prototype]]
对象的比较使用===
运算符.- 仅考虑 enumerable "own" properties。
Error
- 还比较了可枚举的自己的
Symbol
- Object wrappers 作为对象和展开的值进行比较。
Object
属性比较无序。Map
Set
- 当双方不同或双方遇到循环引用时,递归停止。
WeakMap
WeakSet
RegExp
import assert from 'node: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 Object.is(NaN, NaN) is true. // 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: 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 // }
const assert = require('node: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 Object.is(NaN, NaN) is true. // 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: 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 // }
如果这些值不相等,则会抛出一个
,并将 AssertionError
message
属性设置为等于 message
参数的值。如果 message
参数未定义,则分配默认错误消息。如果 message
参数是
的实例,那么它将被抛出而不是 Error
AssertionError
。
相关用法
- Node.js assert.deepStrictEqual()用法及代码示例
- Node.js assert.deepEqual(actual, expected[, message])用法及代码示例
- Node.js assert.deepEqual()用法及代码示例
- Node.js assert.doesNotThrow(fn[, error][, message])用法及代码示例
- Node.js assert.doesNotThrow()用法及代码示例
- Node.js assert.doesNotReject(asyncFn[, error][, message])用法及代码示例
- Node.js assert.doesNotMatch()用法及代码示例
- Node.js assert.doesNotMatch(string, regexp[, message])用法及代码示例
- Node.js assert.notEqual(actual, expected[, message])用法及代码示例
- Node.js assert.notDeepStrictEqual(actual, expected[, message])用法及代码示例
- Node.js assert.fail(actual, expected[, message[, operator[, stackStartFn]]])用法及代码示例
- Node.js assert.throws(fn[, error][, message])用法及代码示例
- Node.js assert.equal()用法及代码示例
- Node.js assert.ifError()用法及代码示例
- Node.js assert.throws()用法及代码示例
- Node.js assert.ok()用法及代码示例
- Node.js assert.strictEqual()用法及代码示例
- Node.js assert.fail()用法及代码示例
- Node.js assert.strictEqual(actual, expected[, message])用法及代码示例
- Node.js assert.notStrictEqual(actual, expected[, message])用法及代码示例
- Node.js assert.match()用法及代码示例
- Node.js assert.notDeepStrictEqual()用法及代码示例
- Node.js assert.rejects()用法及代码示例
- Node.js assert.rejects(asyncFn[, error][, message])用法及代码示例
- Node.js assert.match(string, regexp[, message])用法及代码示例
注:本文由纯净天空筛选整理自nodejs.org大神的英文原创作品 assert.deepStrictEqual(actual, expected[, message])。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。