Jestjs Promise.resolve返回未定义的jest模拟函数
我有一个函数,它进行API调用,并根据第一个API调用返回的内容进行第二个API调用。但是第一个API总是返回未定义的Jestjs Promise.resolve返回未定义的jest模拟函数,jestjs,enzyme,Jestjs,Enzyme,我有一个函数,它进行API调用,并根据第一个API调用返回的内容进行第二个API调用。但是第一个API总是返回未定义的 getTotalCount = async () => { const { showCountCallBack, showCount } = this.props; try { const response = await showCount(); const count = isEmpty(response.result);
getTotalCount = async () => {
const { showCountCallBack, showCount } = this.props;
try {
const response = await showCount();
const count = isEmpty(response.result);
if (count) {
console.log(" success");
} else {
showCountCallBack({ ...this.state });
}
} catch (e) {
console.log("error");
}
};
description(“组件”),()=>{
让浅显的成分;
让浅薄的成分站起来;
const showCountMock=jest.fn(()=>Promise.resolve({result:[]}));
const showCountCallBackMock=jest.fn(()=>Promise.resolve({result:[]}));
在每个之前(()=>{
showCountMock.mockReset();
shallowComponent=shallowWithTheme(
);
shallowComponentInstance=shallowComponent.instance();
});
它(“视图映射”,()=>{
shallowComponentInstance.getTotalCount();
期望(showCountMock)。已被催收时间(1);
预期(showCountCallBackMock)。已被催收时间(1);
});
});
挣扎了几个小时之后。我已经找到了原因。导致此问题的原因是mockReset
它还会重置返回值。所以我刚刚从代码中删除了mockReset
。最好在此处使用mockClear
describe("component", () => {
let shallowComponent;
let shallowComponentInstance;
const showCountMock = jest.fn(() => Promise.resolve({ result: [] }));
const showCountCallBackMock = jest.fn(() => Promise.resolve({ result: [] }));
beforeEach(() => {
showCountMock.mockReset();
shallowComponent = shallowWithTheme(
<Component
showCount={showCountMock}
showCountCallBack={showCountCallBackMock}
/>
);
shallowComponentInstance = shallowComponent.instance();
});
it("viewMapping", () => {
shallowComponentInstance.getTotalCount();
expect(showCountMock).toHaveBeenCalledTimes(1);
expect(showCountCallBackMock).toHaveBeenCalledTimes(1);
});
});