如何窥探类构造函数的玩笑? [英] How to spy on a class constructor jest?
本文介绍了如何窥探类构造函数的玩笑?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
export class Foo {
public static bar() {
doSomething();
}
constructor(paramA, paramB) {
}
}
对于类中的方法,我们可以使用jest.spyOn(Foo, 'bar')
监视方法.构造函数呢?我们如何监视对象是如何实例化的?
For a method in a class, we can use
jest.spyOn(Foo, 'bar')
to spy on the method. How about constructor? How do we spy how the object is instantiated?
推荐答案
@gillyb 是对的,只是忘了mock" Foo 模块
@gillyb is right just forgot to "mock" the Foo module
// Foo.js
export class Foo {
public static bar() {
doSomething();
}
constructor(paramA, paramB) {
}
}
// Foo.spec.js
import Foo from './Foo.js';
jest.mock('./Foo.js');
it('test something...', () => {
// Assuming we did something that called our constructor
expect(Foo).toHaveBeenCalledTimes(1);
});
这篇关于如何窥探类构造函数的玩笑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文