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])。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。