Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/432.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 类中的Jest test Fat Arrow缺少此_Javascript_Reactjs_This_Jestjs_Ecmascript 2017 - Fatal编程技术网

Javascript 类中的Jest test Fat Arrow缺少此

Javascript 类中的Jest test Fat Arrow缺少此,javascript,reactjs,this,jestjs,ecmascript-2017,Javascript,Reactjs,This,Jestjs,Ecmascript 2017,我在开玩笑地为一个组件编写单元测试,目前我正在测试功能 该类函数如下所示: class Comp extends Component { fetch = null; update = async () => { try { if(this.fetch) this.fetch.cancel(); // Do stuff this.fetch = cre

我在开玩笑地为一个组件编写单元测试,目前我正在测试功能

该类函数如下所示:

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>
        )
    }
}
但是我从更新函数中得到了这个错误:

更新时出错!!!引用错误:\未定义此3


有人能帮我解决这个问题吗?

我想问题不是开玩笑的箭头函数,而是Comp类中的class属性。看看这个:

编辑:它在设置等级库模式后工作:

在此模式下传输的模块尽可能符合规范 可能不使用ES6代理


我假设问题不是开玩笑的arrow函数,而是Comp类中的class属性。看看这个:

编辑:它在设置等级库模式后工作:

在此模式下传输的模块尽可能符合规范 可能不使用ES6代理


您可以尝试不使用箭头功能吗?其余功能将崩溃。这也是我不必在组件的其他部分中使用fn.bind(this)的原因。假设问题不是jest中的arrow函数,而是Comp类中的class属性。看看这个:什么时候出现这个错误,什么时候运行测试,或者什么时候在浏览器上执行这个错误?thanx,添加了一些关于spec mode的信息,你可以尝试不使用箭头功能吗?其余的功能会崩溃。这也是我不必在组件的其他部分中使用fn.bind(this)的原因。假设问题不是jest中的arrow函数,而是Comp类中的class属性。看看这个:什么时候出现这个错误,什么时候运行测试,或者在浏览器上执行测试?thanx,添加了一些关于规范模式的信息
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);
});