Jestjs 如何监视类属性中的方法? import React,{Component}来自'React'; 从“酶”导入{shall}; 类应用程序扩展组件{ a={ func:()=>1 }; 渲染(){ 返回null; } } 描述(“”,()=>{ 测试('func()',()=>{ 常量app=shallow(), func_spy=开玩笑的间谍(??); }); });

Jestjs 如何监视类属性中的方法? import React,{Component}来自'React'; 从“酶”导入{shall}; 类应用程序扩展组件{ a={ func:()=>1 }; 渲染(){ 返回null; } } 描述(“”,()=>{ 测试('func()',()=>{ 常量app=shallow(), func_spy=开玩笑的间谍(??); }); });,jestjs,enzyme,Jestjs,Enzyme,我想监视a类属性的func函数。我可以用spyOn方法或其他方法来实现它吗?提前感谢您的回复。您可以使用获取类实例并使用它创建spy: import React,{Component}来自'React'; 从“酶”导入{shall}; 类应用程序扩展组件{ a={ func:()=>1 }; render(){ 返回null; } } 描述(“”,()=>{ 测试('func()',()=>{ const-app=shallow(); const instance=app.instance()/

我想监视
a
类属性的
func
函数。我可以用
spyOn
方法或其他方法来实现它吗?提前感谢您的回复。

您可以使用获取类实例并使用它创建spy:

import React,{Component}来自'React';
从“酶”导入{shall};
类应用程序扩展组件{
a={
func:()=>1
};
render(){
返回null;
}
}
描述(“”,()=>{
测试('func()',()=>{
const-app=shallow();
const instance=app.instance()//
import React, { Component } from 'react';
import { shallow } from 'enzyme';

class App extends Component {
    a = {
        func: () => 1
    };

    render () {
        return null;
    }
}

describe('<App>', () => {
    test('func()', () => {
        const app = shallow(<App />),
              func_spy = jest.spyOn(???);
    });
});