var assert = require('assert');
assert( 3 === 4, '預期出錯');
另一種寫法
assert.equal(4, 3, '预期錯誤');
相反
assert.notEqual(4, 3, '预期錯誤');
**注意其使用!= 而非!==
如要使用=== 可用
assert.strictEqual()
assert.notStrictEqual()
比較物件是否相等
var assert = require('assert');
var person1 = { "name":"john", "age":"21" };
var person2 = { "name":"john", "age":"22" };
assert.notDeepEqual(person1, person2, '预期錯誤');
反之
assert.deepEqual()
throws() 和 doesNotThrow(),他們用来判斷一段代碼是否會抛出異常
var assert = require('assert');
assert.throws(function() {///預期會拋出
throw new Error("Seven Fingers. Ten is too mainstream.");
});
assert.doesNotThrow(function() {///預期不會拋出,但拋出了,所以產生異常訊息
throw new Error("I lived in the ocean way before Nemo");
});