【发布时间】:2016-12-24 05:50:35
【问题描述】:
class Name extends String {
constructor(){
super(...arguments);
}
valid(){
return /[a-zA-Z]/.test(this);
}
}
使用 expect of jest ,我运行我的测试
const name=new Name('Ahmed'); // instruction does not throw exception
it('something',()=>{
expect(name.valid()).toEqual(true); //---calling valid make the ERROR
})
我收到以下错误:
String.prototype.toString is not generic
所以我做什么,我手动实现toString 和valueOf 方法。
class Name extends String {
valid(){
return /[a-zA-Z]/.test(this);
}
toString(){
return this;
}
valueOf(){
return this;
}
}
我又跑了,我得到了:
: Cannot convert object to primitive value
【问题讨论】:
-
我先运行了sn-p,它工作正常,没有错误。
-
它可能是一个 pb 的单元测试框架 +jsx
-
你的问题有babel标签,表示生成的代码是ES5。字符串在 ES5 中是不可子类化的。您的代码可以正常工作,即作为 ES6 代码。
标签: javascript unit-testing ecmascript-6 babeljs jestjs