[英]Jest test Fat Arrow in class missing this
我正在Jest中为组件编写单元测试,而我目前仅在测试功能。
类函数如下:
class Comp extends Component {
fetch = null;
update = async () => {
try {
if(this.fetch)
this.fetch.cancel();
// Do stuff
this.fetch = createFetch();
await this.fetch();
} catch (e) {
console.log('Error in update!!!', e);
}
}
render() {
return(
<div></div>
)
}
}
开玩笑的测试看起来像这样:
test('Should call fetch.cancel if fetch exists', async () => {
const spy = jest.fn();
const comp = new Comp();
comp.fetch = {cancel: spy};
await comp.update();
expect(spy).toHaveBeenCalledTimes(1);
});
但是我从更新功能得到这个错误:
更新错误!!! ReferenceError:_this3未定义
谁能帮我这个?
我认为问题不是Jest中的arrow功能,而是您的Comp类中的class属性。 看一下这个: http : //babeljs.io/docs/plugins/transform-class-properties
编辑:在设置规范模式后可以使用: http : //2ality.com/2017/01/babel-esm-spec-mode.html
在不使用ES6代理的情况下,以这种模式转译的模块尽可能符合规格要求
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.