Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rejecting promise suspends indefinitely with colocated suspense and promise #8219

Closed
ali-idrizi opened this issue Oct 25, 2024 · 0 comments · Fixed by #8247
Closed

Rejecting promise suspends indefinitely with colocated suspense and promise #8219

ali-idrizi opened this issue Oct 25, 2024 · 0 comments · Fixed by #8247

Comments

@ali-idrizi
Copy link

Describe the bug

As a follow-up to #8120, there's another case where the component suspends indefinitely. This occurs consistently when the promise and suspense are colocated. Here's a failing test:

it.only('should throw error if the promise fails (colocate suspense and promise)', async () => {
  const consoleMock = vi
    .spyOn(console, 'error')
    .mockImplementation(() => undefined)

  const key = queryKey()
  let queryCount = 0

  function MyComponent() {
    const query = useQuery({
      queryKey: key,
      queryFn: async () => {
        await sleep(1)
        if (++queryCount > 1) {
          // second time this query mounts, it should not throw
          return 'data'
        }
        throw new Error('Error test')
      },
      retry: false,
    })
    const data = React.use(query.promise)

    return <>{data}</>
  }

  function Page() {
    return (
      <React.Suspense fallback="loading..">
        <MyComponent />
      </React.Suspense>
    )
  }

  const rendered = renderWithClient(
    queryClient,
    <ErrorBoundary
      fallbackRender={(props) => (
        <>
          error boundary{' '}
          <button
            onClick={() => {
              props.resetErrorBoundary()
            }}
          >
            resetErrorBoundary
          </button>
        </>
      )}
    >
      <Page />
    </ErrorBoundary>,
  )

  await waitFor(() => rendered.getByText('loading..'))
  await waitFor(() => rendered.getByText('error boundary'))

  consoleMock.mockRestore()

  fireEvent.click(rendered.getByText('resetErrorBoundary'))

  await waitFor(() => rendered.getByText('loading..'))
  await waitFor(() => rendered.getByText('data'))

  expect(queryCount).toBe(2)
})

Your minimal, reproducible example

Failing test above

Steps to reproduce

Run the test above

Expected behavior

The error boundary should trigger, but the component suspends indefinitely

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

OS: Windows 11
Browser: Chrome
Version: 129.0

Tanstack Query adapter

react-query

TanStack Query version

5.59.16

TypeScript version

No response

Additional context

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant