大家好,我正在使用 jest 和酶为 React 应用编写单元测试,一切进展顺利,但由于 QueryClient、QueryClientProvider 突然出现错误
我正在尝试从我的应用程序中拍摄快照,但出现此错误
<块引用>TypeError: _reactQuery.default is not a constructor
它与反应查询有关 谁能帮忙!
这是我的代码
App.js
import { QueryClient, QueryClientProvider } from "react-query";
import { Dashboard } from "./components/Dashboard";
import "./App.css";
const queryClient = new QueryClient();
export function App() {
return (
<QueryClientProvider client={queryClient}>
<Dashboard />
</QueryClientProvider>
);
}
export default App;
App.test.js
import { render, screen } from '@testing-library/react';
import { QueryClient, QueryClientProvider } from "react-query";
import renderer from 'react-test-renderer';
import { App } from './App';
describe("<App /> component:", () => {
const { queryClient } = new QueryClient({
defaultOptions: {
queries: {
staleTime: Infinity,
},
},
});
it('renders correctly', () => {
const tree = renderer
.create(
<QueryClientProvider client={queryClient}>
<App />
</QueryClientProvider>
)
.toJSON();
expect(tree).toMatchSnapshot();
});
test('renders loading', () => {
render(<App />);
const linkElement = screen.getByText(/Loading.../i);
expect(linkElement).toBeInTheDocument();
});
});
setupTest.js
import '@testing-library/jest-dom';
import { configure } from "enzyme";
import Adapter from '@wojtekmaj/enzyme-adapter-react-17';
configure({adapter: new Adapter()});
jest.mock('react-chartjs-2', () => ({
Bar: () => null, // add any additional chart types here
Line: () => null
}));
jest.mock('react-query', () => ({
useQuery: () => ({ isLoading: false, isError: false, data: [], }),
useMutation: () => ({ mutate: {}, isError: false, isLoading: false }),
useQueryClient: () => null,
}));
答案 0 :(得分:0)
您在 App.test.js 中将 default 作为 QueryClient 导入
import { default as QueryClient, QueryClientProvider } from "react-query";
但是 react-query 没有默认导出。像在应用程序中一样将其导入为命名导入:
import { QueryClient, QueryClientProvider } from "react-query";