Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/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
Jestjs 如何监视组件外部的函数? /./index.js 从“react”导入{Component}; 导出默认类测试扩展组件{ 方法(){ log('method()'); } do(){ 这个方法(); func(); } 渲染(){ 返回null; } } 导出函数func(){ log('func()'); } //./index.test.js 从“酶”导入{shall}; 从“React”导入React; 从“./index”导入*作为测试; 描述(“”,()=>{ 常量组件=浅(), method_spy=jest.spyOn(component.instance(),'method'), func_spy=jest.spyOn(测试“func”); 测试('func()',()=>{ component.instance().do(); expect(method_spy).tohavencalledtimes(1);//已通过 expect(func_spy).toHaveBeenCalledTimes(1);//失败 }); });_Jestjs_Enzyme - Fatal编程技术网

Jestjs 如何监视组件外部的函数? /./index.js 从“react”导入{Component}; 导出默认类测试扩展组件{ 方法(){ log('method()'); } do(){ 这个方法(); func(); } 渲染(){ 返回null; } } 导出函数func(){ log('func()'); } //./index.test.js 从“酶”导入{shall}; 从“React”导入React; 从“./index”导入*作为测试; 描述(“”,()=>{ 常量组件=浅(), method_spy=jest.spyOn(component.instance(),'method'), func_spy=jest.spyOn(测试“func”); 测试('func()',()=>{ component.instance().do(); expect(method_spy).tohavencalledtimes(1);//已通过 expect(func_spy).toHaveBeenCalledTimes(1);//失败 }); });

Jestjs 如何监视组件外部的函数? /./index.js 从“react”导入{Component}; 导出默认类测试扩展组件{ 方法(){ log('method()'); } do(){ 这个方法(); func(); } 渲染(){ 返回null; } } 导出函数func(){ log('func()'); } //./index.test.js 从“酶”导入{shall}; 从“React”导入React; 从“./index”导入*作为测试; 描述(“”,()=>{ 常量组件=浅(), method_spy=jest.spyOn(component.instance(),'method'), func_spy=jest.spyOn(测试“func”); 测试('func()',()=>{ component.instance().do(); expect(method_spy).tohavencalledtimes(1);//已通过 expect(func_spy).toHaveBeenCalledTimes(1);//失败 }); });,jestjs,enzyme,Jestjs,Enzyme,我想监视组件外部的函数,但它不能很好地工作 我收到一条类似于的消息,预期mock函数被调用了一次,但它被调用了零次。 在这种情况下,我不想使用mock()方法而不是spyOn() 有办法解决吗?谢谢你的阅读D它不起作用,因为这行: const func_spy=jest.spyOn(测试'func'); …正在为func的模块导出创建间谍 …但是Test.do不调用func的模块导出,它直接调用func 有两个选项可以修复它 一种是将func移动到它自己的模块中 然后,它的模块导出将被导入到

我想监视组件外部的函数,但它不能很好地工作

我收到一条类似于
的消息,预期mock函数被调用了一次,但它被调用了零次。

在这种情况下,我不想使用mock()方法而不是spyOn()


有办法解决吗?谢谢你的阅读D

它不起作用,因为这行:

const func_spy=jest.spyOn(测试'func'); …正在为
func
的模块导出创建间谍

…但是
Test.do
不调用
func
的模块导出,它直接调用
func


有两个选项可以修复它

一种是将
func
移动到它自己的模块中

然后,它的模块导出将被导入到
index.js
中,并在
Test.do
中调用

…当
func
的模块导出被封装在spy中时,spy将被
Test.do
调用


另一个选项是注意,这样模块就可以导入到自身中

如果模块导入到自身中,则
Test.do
可以调用
func
的模块导出:

从'react'导入{Component};
从“./index”导入*作为索引;//{
常量组件=浅(),
method_spy=jest.spyOn(component.instance(),'method'),
func_spy=jest.spyOn(测试“func”);
测试('func()',()=>{
component.instance().do();
期待(方法_spy).tohavencalledtimes(1);//成功!
期待(func_spy)。获得成功(1);//成功!
});
});

你说得对!我浏览了
笑话
的源代码部分。我意识到这是因为我不知道
spyOn()
是如何工作的。谢谢
// ./index.js
import { Component } from 'react';

export default class Test extends Component {
    method () {
        console.log('method()');
    }

    do () {
        this.method();
        func();
    }

    render () {
        return null;
    }
}

export function func () {
    console.log('func()');
}

// ./index.test.js
import { shallow } from 'enzyme';
import React from 'react';
import * as Test from './index';

describe('<Test>', () => {
    const component = shallow(<Test.default/>),
          method_spy = jest.spyOn(component.instance(), 'method'),
          func_spy = jest.spyOn(Test, 'func');

    test('func()', () => {
        component.instance().do();
        expect(method_spy).toHaveBeenCalledTimes(1); // passed
        expect(func_spy).toHaveBeenCalledTimes(1); // failed
    });
});