const assert = require("assert");
assert("blue","第一个值为false时以我为错误信息抛出");
assert(true,"第一个值为false时以我为错误信息抛出");
assert(false,"第一个值为false时以我为错误信息抛出"); // AssertionError [ERR_ASSERTION]: 第一个值为false时以我为错误信息抛出
assert(false); // AssertionError [ERR_ASSERTION]: false == true
const assert = require("assert");
assert.ok(true);
assert.ok(1);
assert.ok(0); //AssertionError [ERR_ASSERTION]: 0 == true assert.ok(false); //AssertionError [ERR_ASSERTION]: false == true assert.ok(false,"自定义错误信息"); //AssertionError [ERR_ASSERTION]: 自定义错误信息
assert.equal(1, 1);
assert.equal("1", 1);
assert.equal({},{},"AssertionError");
assert.equal(() => { }, () => { }, "AssertionError");
assert.equal([],[],'AssertionError');
const obj={};
assert.equal(obj,obj);
// 断言通过
const a = 'Blue', b = 'Pink'; assert.deepEqual(a,a,'actual unequal to expected'); // 断言通过 assert.deepEqual(a,b,'actual unequal to expected'); // AssertionError [ERR_ASSERTION]: actual unequal to expected
const obj1 = { name: "foo", gender: "men" },
obj2 = { name: "foo", gender: "men" },
obj3 = { name: "bar", gender: "men" }
assert.deepEqual(obj1, obj2, 'actual unequal to expected');
// 断言通过
assert.deepEqual(obj1, obj3, 'actual unequal to expected');
// AssertionError [ERR_ASSERTION]: actual unequal to expected
const son1 = Object.create(obj1), son2 = Object.create(obj2); son1.name="Summer"; son2.name="Summer"; assert.deepEqual(son1,son2,"actual unequal to expected"); // 断言通过
const ena = {}, enb = {};
Object.defineProperties(ena,{
name:{
value:"Blue"
},
hobby:{
value:"foo",
enumerable:false //可枚举性设置为false
}
});
Object.defineProperties(enb,{
name:{
value:"Blue"
},
hobby:{
value:"bar",
enumerable:false //可枚举性设置为false
}
})
assert.deepEqual(ena,enb,"actual unequal to expected")
//ok,actual equal to expected
const obj1 = { name: "foo", gender: "men" },
obj2 = { name: "bar", gender: "men" }
const son1 = Object.create(obj1),
son2 = Object.create(obj2);
son1.name = "Summer";
son2.name = "Summer";
assert.deepEqual(son1, son2, "actual unequal to expected");
//断言通过
assert.deepStrictEqual(son1, son2, "actual unequal to expected")
//AssertionError [ERR_ASSERTION]: actual unequal to expected
assert.strictEqual(1, 2); // 抛出 AssertionError: 1 === 2 assert.strictEqual(1, 1); // 测试通过。 assert.strictEqual(1, '1'); // 抛出 AssertionError: 1 === '1' assert.equal(1, '1'); // 测试通过。
assert.notEqual("1", "2");
// 断言通过
assert.notEqual("1", 2);
// 断言通过
assert.notEqual("1", 1);
// AssertionError [ERR_ASSERTION]: '1' != 1
assert.notEqual({ a: "foo" }, { a: "foo" });
assert.notEqual(() => { }, () => { });
assert.notEqual([], []);
const obj1 = { a: "foo" },
obj2 = { b: "bar" },
obj3 = Object.create(obj1);
assert.notDeepEqual(obj1,obj1,'actual equal to expected');
// AssertionError [ERR_ASSERTION]: actual equal to expected
assert.notDeepEqual(obj1,obj2,'actual equal to expected');
// 断言通过
assert.notDeepEqual(obj1,obj3,'actual equal to expected');
// 断言通过
assert.notStrictEqual("1", 1);
// 断言通过
assert.notStrictEqual("1", "1");
// AssertionError [ERR_ASSERTION]: '1' !== '1'
assert.notStrictEqual({ a: "foo" }, { a: "foo" });
assert.notStrictEqual(() => { }, () => { });
assert.notStrictEqual([], []);
assert.notDeepStrictEqual({ a: '1' }, { a: 1 });
//断言通过
assert.notDeepStrictEqual({ a: '1' }, { a: "1" });
//AssertionError [ERR_ASSERTION]: { a: '1' } notDeepStrictEqual { a: '1' }
assert.fail("自定义错误信息");
// AssertionError [ERR_ASSERTION]: 自定义错误信息
assert.fail("BLUE","PINK");
// AssertionError [ERR_ASSERTION]: 'BLUE' != 'PINK'
assert.fail("BLUE","PINK","自定义的错误信息");
// AssertionError [ERR_ASSERTION]: 自定义的错误信息
assert.fail("BLUE","PINK","自定义的错误信息","?",()=>{
console.log("hello");
});
// AssertionError [ERR_ASSERTION]: 自定义的错误信息
assert.fail("BLUE","PINK",undefined);
// AssertionError [ERR_ASSERTION]: 'BLUE' undefined 'PINK'
assert.fail("BLUE","PINK",undefined,"?");
// AssertionError [ERR_ASSERTION]: 'BLUE' ? 'PINK'
assert.throws(
() => {
throw new Error('错误信息');
},
Error
);
assert.throws(
() => {
throw new Error('错误信息');
},
/错误/
);
// 这是错误的!不要这么做! assert.throws(myFunction, '错误信息', '没有抛出期望的信息'); // 应该这么做。 assert.throws(myFunction, /错误信息/, '没有抛出期望的信息');
assert.throws(
() => {
throw new Error('错误信息');
},
function (err) {
if ((err instanceof Error) && /错误/.test(err)) {
return true;
}
},
'不是期望的错误'
);
assert.doesNotThrow(
() => {
throw new TypeError('错误信息');
},
SyntaxError
);
assert.doesNotThrow(
() => {
throw new TypeError('错误信息');
},
TypeError
);
assert.doesNotThrow(
() => {
throw new TypeError('错误信息');
},
TypeError,
'抛出错误'
);
// 抛出 AssertionError: Got unwanted exception (TypeError). 抛出错误
assert.ifError(true); //抛出true assert.ifError(false); //断言通过
assert.ifError(0);
//断言通过
assert.ifError("0");
//抛出 "0"
assert.ifError(1);
//抛出 1
assert.ifError(new Error());
//抛出 Error,对象名称
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有