[英]Mock entire es6 class except constructor in Jest?
如果我有A类,例如:
class A{
constructor(foo){
this.foo = foo;
}
doStuff(){
//Code protected by an NDA, they'll nuke my house if I tell you what it does.
}
nukeHouse(){
//The implementation of this is somewhat buggy...
}
}
我希望A类的用户可以访问this.foo
,所以我不想模拟构造函数。 所有其他方法都应被模拟。 我可能可以手动说出A.prototype.doStuff = jest.genMockFunction()ù, and do the same for
A.prototype.nukeHouse` A.prototype.doStuff = jest.genMockFunction()ù, and do the same for
事情,但是我希望有一种方法,而不必每次都更新模拟代码一次我向A添加方法
有没有办法做到这一点?
我猜通用的解决方案是简单地遍历prototype
并为每个属性创建一个模拟函数:
var A = require.requireActual('./A');
Object.getOwnPropertyNames(A.prototype).forEach(function(prop) {
A.prototype[prop] = jest.genMockFunction();
});
module.exports = A;
如果该类扩展了另一个类,则可能会更加困难。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.