Should.js 使用问题记录
should.extend 方法
should 的基本原理就是在 Object.prototype 上定义一个 should 对象,拦截 get,进行特殊处理。
should$1.extend = function (propertyName, proto) {
propertyName = propertyName || "should";
proto = proto || Object.prototype;
var prevDescriptor = Object.getOwnPropertyDescriptor(proto, propertyName);
Object.defineProperty(proto, propertyName, {
set: function () {
},
get: function () {
return should$1(isWrapperType(this) ? this.valueOf() : this);
},
configurable: true
});
return {name: propertyName, descriptor: prevDescriptor, proto: proto};
};
should.noConflict 方法
删除掉对象中 proto 中的 should 对象,取消 should 对属性读取的拦截
should$1.noConflict = function (desc) {
desc = desc || should$1._prevShould;
if (desc) {
delete desc.proto[desc.name];
if (desc.descriptor) {
Object.defineProperty(desc.proto, desc.name, desc.descriptor);
}
}
return should$1;
};
导致的问题
如果代码中有一个对象中,想要额外增加一个 should 的属性,正好重名,那就永远访问也不能覆盖掉这个should。 这时就会导致代码中缺少了 should 中的对象,导致代码出错。
require('should')
const a = {
a: 1,
b: 2,
}
// 此时的 should 是 should.Assertion 对象,无法重新赋值
a.should = {
a: 1,
b: 2
}
console.log(a)
// 打印结果
// {a: 1,b: 2}
解决办法
const should = require('should')
// 关闭 should 的拦截,保证这次测试可以使用
should.noConflict()
const a = {
a: 1,
b: 2,
}
a.should = {
a: 1,
b: 2
}
console.log(a)
// { a: 1, b: 2, should: { a: 1, b: 2 } }
// 重新开启 should 的拦截,保证后续的测试可以继续使用 should
should.extend()