Jest not covered the function inside useEffect

瘦欲@ 提交于 2020-12-15 01:55:23

问题


I want to test a function that fetch something from server, it looks like this :

import React, { useEffect } from 'react';

function Component1() {

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

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

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

export default Component1;

and test file:

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]

but the test is not covered 'fetchSomeData', any one knows why?

I referred this answer : test restapi inside useeffect in jest try to mock a fn and call it , but I got this:

来源:https://stackoverflow.com/questions/64604039/jest-not-covered-the-function-inside-useeffect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!