Skip to content
Source
Sign inSign up
TanStack/query

_reactQuery.QueryClient is not a constructor #7831

Send to my agent
ClosedJayYoungman(GitHub profile, opens in a new tab) opened this issue 2 comments

Describe the bug

Jest test suite is failing with TypeError: _reactQuery.QueryClient is not a constructor.

following this example of writing tests with react query.

https://github.com/TkDodo/testing-react-query/blob/main/src/tests/App.test.tsx

Your minimal, reproducible example

Snippet

Steps to reproduce

//testingLibraryHelpers.tsx

const createTestQueryClient = () =>
  new QueryClient({
    defaultOptions: {
      queries: {
        retry: false,
      },
    },
  })
export function renderWithClient(ui: React.ReactElement) {
  const testQueryClient = createTestQueryClient()
  const { rerender, ...result } = render(<QueryClientProvider client={testQueryClient}>{ui}</QueryClientProvider>)
  return {
    ...result,
    rerender: (rerenderUi: React.ReactElement) =>
      rerender(<QueryClientProvider client={testQueryClient}>{rerenderUi}</QueryClientProvider>),
  }
}
export function createWrapper() {
  const testQueryClient = createTestQueryClient()
  return ({ children }: { children: React.ReactNode }) => <QueryClientProvider client={testQueryClient}>{children}</QueryClientProvider>
}

//test.tsx

it.only('should not render the carousel for brands until there is data', () => {
      const { container } = renderWithClient(
        <MemoryRouter initialEntries={['/']}>
          <BrandsCarousel />
        </MemoryRouter>
      )

      const carousel = container.querySelector('.brand-carousel-wrapper')

      expect(carousel).not.toBeInTheDocument()
    })

//app.tsx

const queryClient = new QueryClient({
  defaultOptions: {
    queries: {
      staleTime: Infinity,
    },
  },
})
const AppWrapper: FC = (props) => {
  const { isRegionSelectorModalVisible } = useRegionSelector()
  const isScrollable = !isRegionSelectorModalVisible

  return (
      <QueryClientProvider client={queryClient}>
        <TamaguiProvider config={config} defaultTheme="light">
          ....
        </TamaguiProvider>
      </QueryClientProvider>
  )
}

Expected behavior

Test suite should not fail

How often does this bug happen?

Every time

Screenshots or Videos

Screenshot 2024-07-31 at 15 26 01

Platform

Jest/ React testing library

"@testing-library/react": "^16.0.0", "jest": "^27.4.7",

Tanstack Query adapter

react-query

TanStack Query version

5.51.11

TypeScript version

4.7.4

Additional context

Would appreciate any support, thank you :)

  1. Thank you for filing this issue, but we need more information before looking into this.

    Specifically, a minimal, standalone, runnable reproduction is needed - preferably with codesandbox or stackblitz. If your issue is TypeScript related, prefer a TypeScript playground.

    We'll close this issue for now, but we'll certainly re-open it if there is a proper reproduction.

  2. TkDodo closed this

  3. TkDodo added labelsneeds-info

  4. I am also facing the same issue. For some reason importing QueryClient works at runtime, but breaks during jest tests. The imports are undefined

Sign in to join this conversation.