Files
supabase/apps/studio/data/query-client-reporting.test.ts
T
Alaister YoungandAlaister Young bbb0af65cb [FE-3106] (part 14) – refactor(studio): drop the dual-runtime shims and aliases (#53223)
Drops the remaining Next-era shims and aliases that only existed for the
dual runtime (FE-3106). Part 14 of the stack.

**Depends on:** #53222

**Changed:**
- ~45 files – import Sentry from `@sentry/react` directly instead of the
aliased `@sentry/nextjs`
- `hooks/ui/usePreventNavigationOnUnsavedChanges.ts` – is now the
TanStack `useBlocker` implementation (previously swapped in by a Vite
alias)
- `components/interfaces/App/RouteValidationWrapper.tsx` – `next/router`
→ `useNavigate` + the leaf route's `fullPath` (`/project/$ref/sql/$id`)
for the exempt-route and last-visited checks
- `hooks/misc/withAuth.tsx` – no `getLayout` forwarding
- `TemplateEditor.test.tsx` – renders inside a router for the
unsaved-changes blocker

**Removed:**
- `compat/sentry-nextjs.ts` and its Vite alias, the
`usePreventNavigationOnUnsavedChanges` alias and Next hook,
`types/next.ts` (`NextPageWithLayout`), the `@sentry/nextjs` dependency

The `compat/next/*` shims for `link`, `router`, `head`, `image`,
`navigation`, `dynamic` and `script` stay: about 300 components outside
`routes/**` still import them.

Addresses
[FE-3106](https://linear.app/supabase/issue/FE-3106/move-page-bodies-into-route-files-and-delete-pages).

## To test

- Trigger an error (e.g. a failing query) – it reaches Sentry as before
- Edit an email template or edge function and navigate away – the
unsaved-changes prompt appears
- Open an org you don't belong to – toast and redirect home; open a SQL
snippet, leave and come back – last snippet is restored

---------

Co-authored-by: Alaister Young <10985857+alaister@users.noreply.github.com>
2026-11-17 17:49:20 +09:00

112 lines
3.7 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from 'vitest'
import { PlatformError, UnclassifiedApiError } from '@/lib/errors/studio-errors'
const captureException = vi.hoisted(() => vi.fn(() => 'event-123'))
vi.mock('@sentry/react', () => ({ captureException }))
vi.mock('common', () => ({ IS_PLATFORM: false, getAccessToken: vi.fn() }))
vi.mock('@/lib/constants', () => ({ API_URL: 'http://localhost', IS_PLATFORM: false }))
vi.mock('@/lib/helpers', () => ({ uuidv4: () => 'test-uuid' }))
// Import after mocks are set up
const { handleError } = await import('./fetchers')
const { getQueryClient } = await import('./query-client')
// The production client, so its cache-level onError is what's under test. Retry settings are
// overridden per query to keep the tests fast.
const client = getQueryClient()
beforeEach(() => {
captureException.mockClear()
client.clear()
})
describe('reporting errors from React Query', () => {
it('reports a query that fails through handleError once, after the last retry', async () => {
const queryFn = vi.fn(async () => {
handleError(
{ message: 'boom', code: 500 },
{ alwaysCapture: true, sentryContext: { tags: { feature: 'x' } } }
)
})
await expect(
client.fetchQuery({ queryKey: ['a'], queryFn, retry: 2, retryDelay: 0 })
).rejects.toMatchObject({ _tag: 'PlatformError' })
expect(queryFn).toHaveBeenCalledTimes(3)
expect(captureException).toHaveBeenCalledTimes(1)
expect(captureException).toHaveBeenCalledWith(
expect.objectContaining({ _tag: 'PlatformError' }),
expect.objectContaining({
tags: expect.objectContaining({ feature: 'x', error_tag: 'PlatformError', sample_rate: 1 }),
})
)
})
it('reports a failed mutation once', async () => {
const mutation = client.getMutationCache().build(client, {
mutationFn: async () => {
handleError({ message: 'boom', code: 500 }, { alwaysCapture: true })
},
})
await expect(mutation.execute(undefined)).rejects.toMatchObject({ _tag: 'PlatformError' })
expect(captureException).toHaveBeenCalledTimes(1)
})
it('classifies and reports an error a query function throws itself', async () => {
await expect(
client.fetchQuery({
queryKey: ['b'],
retry: false,
queryFn: async () => {
throw new TypeError("Cannot read properties of undefined (reading 'x')")
},
})
).rejects.toBeDefined()
expect(captureException).toHaveBeenCalledTimes(1)
expect(captureException).toHaveBeenCalledWith(
expect.objectContaining({ _tag: 'UnexpectedError' }),
expect.anything()
)
})
})
describe('retry policy', () => {
const retry = client.getDefaultOptions().queries!.retry as (
failureCount: number,
error: unknown
) => boolean
it.each([
'/platform/projects/{ref}/run-lints',
'/platform/organizations/{slug}/usage',
'/platform/pg-meta/{ref}/query',
'/v1/projects/{ref}/analytics/endpoints/logs.all',
])('does not retry a server error from %s', (endpoint) => {
expect(retry(0, new PlatformError({ code: 500, endpoint }))).toBe(false)
})
it('still retries a rate limit from a skip-listed endpoint', () => {
const error = new UnclassifiedApiError({
code: 429,
endpoint: '/platform/pg-meta/{ref}/query',
})
expect(retry(0, error)).toBe(true)
})
it('retries a server error from another endpoint', () => {
expect(retry(0, new PlatformError({ code: 500, endpoint: '/platform/projects/{ref}' }))).toBe(
true
)
})
it('does not match concrete paths', () => {
expect(
retry(0, new PlatformError({ code: 500, endpoint: '/platform/pg-meta/abc/query' }))
).toBe(true)
})
})