简体   繁体   中英

TypeError: _reactQuery.default is not a constructor

Hi guys I'm using jest and enzyme to write a unit test for react app everything was going well but suddenly I got an error because of QueryClient, QueryClientProvider

I'm trying to take a snapshot from my App but I got this error

TypeError: _reactQuery.default is not a constructor

It's related to react query can anyone help!

Here's my code

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,
}));

You import default as QueryClient here in App.test.js

import { default as QueryClient, QueryClientProvider } from "react-query";

but react-query has no default export. Import it like in the App as a named import:

import { QueryClient, QueryClientProvider } from "react-query";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM