我正在尝试测试一个简单的自定义钩子:
export const getSearchResults = async (searchText: string) => {
const { data } = await axios.get(`${BASE_URL}/search`, {
params: {
searchText,
apiToken: API_KEY
},
});
return data as SearchResponse;
};
export default function useFetchSearch(searchTerm: string) {
return useQuery<SearchResponse>([ 'search', searchTerm ], () => getSearchResults(searchTerm));
}
首先,我尝试用 jest 来模拟 API 调用,然后我用 nock 进行了尝试,正如它在 react-query 文档 ( https://react-query.tanstack.com/guides/testing ) 中所说的那样。
我的测试如下所示:
it('Displays card', async () => {
const queryClient = new QueryClient({
defaultOptions: {
queries: {
retry: false,
},
},
});
const wrapper = ({ children }: any) => (
<QueryClientProvider client={queryClient}>
{children}
</QueryClientProvider>
);
const BASE_URL = process.env.REACT_APP_API_URL;
const expectation = nock(`${BASE_URL}`)
.get('/search')
.reply(200, {
answer: 42
});
const { result, waitFor } = renderHook(() => useFetchSearch('test'), { wrapper });
await waitFor(() => result.current.isSuccess, { interval: 100 });
expect(result.current.data.answer).toEqual(42);
});
我得到的错误是:错误:1000 毫秒后在 waitFor 中超时。
我的 package.json 如下:
"dependencies": {
"@testing-library/dom": "7.21.4",
"@testing-library/jest-dom": "^5.11.9",
"@testing-library/react": "^11.2.5",
"@testing-library/react-hooks": "^7.0.1",
"@testing-library/user-event": "^12.1.10",
"@types/jest": "^26.0.15",
"@types/node": "^12.0.0",
"@types/react": "16.9.0",
"@types/react-dom": "16.9.0",
"@types/react-router": "^5.1.12",
"@types/react-router-dom": "^5.1.7",
"axios": "^0.21.1",
"nock": "^13.1.1",
"react": "16.9.0",
"react-dom": "16.9.0",
"react-query": "^3.5.0",
"react-router": "^5.2.0",
"react-router-dom": "^5.2.0",
"react-scripts": "4.0.1",
"react-test-renderer": "16.9.0",
"typescript": "^3.8.3",
},
react-test-renderer、react 和 react-dom 的版本是匹配的。
任何输入都非常受欢迎!