我想测试一个从服务器获取内容的函数,它看起来像这样:
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]
我提到了这个答案: test restapi inside useeffect in jest 尝试模拟一个fn并调用它,但是我得到了: