斷言模塊提供了一組用於驗證不變量的斷言函數。 assert.deepEqual()函數測試實際參數和預期參數之間的深度相等性。如果條件為真,則不會產生輸出,否則會引發斷言錯誤。
用法:
assert.deepEqual(actual, expected[, message])
參數:該函數接受上述和以下描述的以下參數:
- actual:此參數保存需要評估的實際值。它是任何類型。
 - expected:此參數保存與實際值匹配的期望值。它是任何類型。
 - message:此參數保存字符串或錯誤類型的錯誤消息。它是一個可選參數。
 
返回值:此函數返回對象類型的斷言錯誤。
斷言模塊的安裝:
- 您可以訪問指向安裝斷言模塊的鏈接。您可以使用此命令安裝此軟件包。
npm install assert
注意:安裝是可選步驟,因為它是內置的Node.js模塊。
 - 安裝斷言模塊後,可以使用命令在命令提示符下檢查斷言版本。
npm version assert
 - 之後,您可以創建一個文件夾並添加一個文件,例如index.js,如下所示。
 
範例1: 文件名:index.js
// Requiring the module 
const assert = require('assert').strict; 
    
// Function call 
try { 
    assert.deepEqual({ a:'5' }, { a:'11' }); 
} catch(error) { 
    console.log("Error:", error) 
}運行程序的步驟:
- 項目結構將如下所示:

 - 使用以下命令運行index.js文件:
node index.js
輸出:
Error:AssertionError [ERR_ASSERTION]:Expected values to be strictly deep-equal:
+ actual - expected{
+ a:‘5’
- a:’11’
}
at Object.(C:\Users\Lenovo\Downloads\index.js:14:12) 
at Module._compile (internal/modules/cjs/loader.js:1138:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
at Module.load (internal/modules/cjs/loader.js:986:32)
at Function.Module._load (internal/modules/cjs/loader.js:879:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
at internal/main/run_main_module.js:17:47 {
generatedMessage:true,
code:‘ERR_ASSERTION’,
actual:[Object],
expected:[Object],
operator:‘deepStrictEqual’
} 
範例2: 文件名:index.js
// Requiring the module 
const assert = require('assert').strict; 
     
// Function call 
try { 
    assert.deepEqual({ a:'5' }, { a:'5' }); 
    console.log("No Error Occured") 
} catch(error) { 
    console.log("Error:", error) 
}運行程序的步驟:
- 項目結構將如下所示:

 - 使用以下命令運行index.js文件:
node index.js
輸出:
No Error Occured
 
相關用法
- Node.js GM monochrome()用法及代碼示例
 - Node.js GM whiteThreshold()用法及代碼示例
 - Node.js GM whitePoint()用法及代碼示例
 - Node.js GM threshold()用法及代碼示例
 - Node.js GM thumbnail()用法及代碼示例
 - Node.js GM write()用法及代碼示例
 - Node.js GM median()用法及代碼示例
 - Node.js GM modulate()用法及代碼示例
 - Node.js GM magnify()用法及代碼示例
 - Node.js GM minify()用法及代碼示例
 - Node.js GM paint()用法及代碼示例
 - Node.js GM drawRectangle()用法及代碼示例
 - Node.js GM roll()用法及代碼示例
 - Node.js GM transparent()用法及代碼示例
 - Node.js GM resize()用法及代碼示例
 - Node.js GM raise()用法及代碼示例
 - Node.js GM quality()用法及代碼示例
 - Node.js GM segment()用法及代碼示例
 - Node.js GM recolor()用法及代碼示例
 
注:本文由純淨天空篩選整理自gouravhammad大神的英文原創作品 Node.js assert.deepEqual() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
