Jest测试错误【错误:没有设置QueryClient,使用QueryClientProvider设置一个】

问题描述 投票:0回答:1

尽管我已将测试包装在 QueryClientProvider 中,但我不断收到错误 [错误:未设置 QueryClient,请使用 QueryClientProvider 设置一个]。我做错了什么。

describe("Selector", () => {
  beforeEach(() => {

    const queryClient = new QueryClient();

    render(
      <QueryClientProvider client={queryClient}>
        <ErrorBoundary FallbackComponent={({ error }) => <div>Something went wrong: {error.message}</div>}>
           <Selector content={content} setLanguageCallback={() => {}} />
        </ErrorBoundary>
      </QueryClientProvider>,
    );
    screen.debug();
  });

  it("Is there a hello test text", () => {
    expect(screen.getByText("Hello")).toBeInTheDocument();
  });
  
}); 
reactjs testing jestjs react-query tanstackreact-query
1个回答
0
投票

这对我有用

const queryClient = new QueryClient();

    describe('Home', () => {
      test('renders without crashing', () => {
        render(
          <QueryClientProvider client={queryClient}>
            <SnackbarProvider>
              <Provider store={store}>
                <Home />
              </Provider>
            </SnackbarProvider>
          </QueryClientProvider>
        );
        const myEleme = screen.getByText('Sign in');
        expect(myEleme).toBeInTheDocument();
      });
    });
© www.soinside.com 2019 - 2024. All rights reserved.