开玩笑没有覆盖useEffect内部的功能

时间:2020-10-30 07:11:48

标签: reactjs unit-testing jestjs react-hooks enzyme

我想测试一个从服务器获取内容的函数,它看起来像这样:

import React, { useEffect } from 'react';

function Component1() {

    useEffect(() => {
        fetchSomeData();
    }, [])

    const fetchSomeData = async () =>{
        console.log('fetchSomeData')
       

    }
    return <div>Component1</div>;
}

export default Component1;

和测试文件:

import React from 'react';
import { render, unmountComponentAtNode } from 'react-dom';
import { act } from 'react-dom/test-utils';
import Enzyme, { shallow } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import Component1 from './Component1.js';

Enzyme.configure({ adapter: new Adapter() });

describe('Name of the group', () => {
    it('should ', () => {
        const wrapper = shallow(<Component1 />);
        expect(wrapper.exists());
        
    });
});

[![picture1] [1]] [1]

但是测试未涵盖'fetchSomeData',有人知道为什么吗?

我提到了这个答案: test restapi inside useeffect in jest 尝试模拟一个fn并调用它,但是我得到了:

enter image description here

0 个答案:

没有答案