我使用axios并获取一些数据:
export const getSomething = () => {
return axios
.get(`/somehting`, proxyConfig)
.then(response => {
return response.data;
})
.catch(error => {
console.log(error.message);
httpErrors(error);
});
};
开玩笑:
import mockAxios from "jest-mock-axios";
import { getSomething } from "./Api";
afterEach(() => {
// cleaning up the mess left behind the previous test
mockAxios.reset();
});
it("should get some data..", () => {
const dummyF = jest.fn();
const dummyCatch = jest.fn();
getSomething()
.then(dummyF)
.catch(dummyCatch);
mockAxios.mockResponse({ data: "" });
expect(mockAxios.get).toHaveBeenCalledWith(
"/somehting"
);
expect(dummyF).toHaveBeenCalledTimes(1);
expect(dummyCatch).not.toHaveBeenCalled();
});
但是我遇到以下错误:
TypeError: (0 , _Api.getSomething) is not a function
我可以从其他文件调用该方法,是因为我在getSomething方法上使用ES6语法吗?