Files
supabase/apps/studio/data/handleError.test.ts
T
Joshen Lim 60b51d74d5 Audit existing unit tests + Remove redundant ones (#52215)
## Context

_TLDR: Changes here speeds up unit tests on CI from ~14 minutes to ~3
minutes_ 😂
_TLDR 2: The file changes are solely only test files +
`studio-unit-tests.yml`_

We've currently got a lot of unit tests in the repository which while
great for coverage, hasn't been ideally written. There's a lot of
redundancy + overly specific tests that break easily, and our current
unit test takes about 15 minutes to run in the GHA per PR. Hence am
doing a audit clean up of all existing unit tests in the studio repo.

## Changes involved
- `.test.ts` files now run in `node`, `.test.tsx` in `jsdom`
- jsdom costs about 1s of setup per file. A `.ts` test that needs the
DOM opts in with `// @vitest-environment jsdom` (46 files do).
- Removed redundant and over-specific tests
- Duplicates are merged into `test.each`. Tests that only covered
libraries, existed for coverage, or asserted class names and snapshots
are deleted. 19 files are merged into a sibling or deleted.
- Fixed about 15 tests that couldn't fail 
- Examples: unawaited assertions, fixtures equal to the default, and a
`semverGte` test calling `semverLte`.
- Sped up the slowest files 
- Real sleeps are replaced with fake timers, `user.type` with paste, and
Radix option queries are scoped to the open listbox. For example,
`RestoringState` went from 18s to 0.3s and
`useTableEditorFiltersSort.navigation` from 20s to 3s.
- These ^ already shave down the unit tests time from 14 mins to 9 mins,
but am also sharding unit tests across 4 jobs to increase speed up
further which then shaves down time to 3 mins now
2026-10-23 18:51:27 +08:00

108 lines
4.2 KiB
TypeScript

import { describe, expect, it, vi } from 'vitest'
import { ConnectionTimeoutError, UnknownAPIResponseError } from '@/types/api-errors'
import { ResponseError } from '@/types/base'
vi.mock('@sentry/nextjs', () => ({ captureException: vi.fn() }))
vi.mock('common', () => ({ IS_PLATFORM: false, getAccessToken: vi.fn() }))
vi.mock('@/lib/constants', () => ({ API_URL: 'http://localhost' }))
vi.mock('@/lib/helpers', () => ({ uuidv4: () => 'test-uuid' }))
// Import after mocks are set up
const { handleError } = await import('./fetchers')
function throwAndCatch(error: unknown): ResponseError {
try {
handleError(error)
} catch (e) {
return e as ResponseError
}
throw new Error('handleError did not throw')
}
describe('handleError — error classification', () => {
// Pattern matching itself (case, whitespace, near-misses) is covered in error-patterns.test.ts.
it.each([
['message', { message: 'CONNECTION TERMINATED DUE TO CONNECTION TIMEOUT' }],
['msg', { msg: 'ERROR: CONNECTION TERMINATED DUE TO CONNECTION TIMEOUT' }],
])('classifies a known pattern from the %s field', (_, raw) => {
const err = throwAndCatch(raw)
expect(err).toBeInstanceOf(ConnectionTimeoutError)
expect(err).toBeInstanceOf(ResponseError)
expect((err as ConnectionTimeoutError).errorType).toBe('connection-timeout')
})
it.each([
['an unmatched message', { message: 'something went wrong' }],
['an empty message', { message: '' }],
['null', null],
['a non-object', 'raw string error'],
])('throws UnknownAPIResponseError for %s', (_, raw) => {
const err = throwAndCatch(raw)
expect(err).toBeInstanceOf(UnknownAPIResponseError)
expect(err).toBeInstanceOf(ResponseError)
})
describe('field preservation', () => {
it('preserves all ResponseError fields on classified errors', () => {
const err = throwAndCatch({
message: 'CONNECTION TERMINATED DUE TO CONNECTION TIMEOUT',
code: 503,
requestId: 'req-abc',
retryAfter: 30,
requestPathname: '/rest/v1/table',
})
expect(err.message).toBe('CONNECTION TERMINATED DUE TO CONNECTION TIMEOUT')
expect(err.code).toBe(503)
expect(err.requestId).toBe('req-abc')
expect(err.retryAfter).toBe(30)
expect(err.requestPathname).toBe('/rest/v1/table')
})
it('preserves all ResponseError fields on unclassified errors', () => {
const err = throwAndCatch({ message: 'some error', code: 500, requestId: 'req-xyz' })
expect(err.code).toBe(500)
expect(err.requestId).toBe('req-xyz')
})
it('msg field takes priority over message field for error text', () => {
const err = throwAndCatch({ msg: 'from msg field', message: 'from message field' })
expect(err.message).toBe('from msg field')
})
it('preserves formattedError on classified errors', () => {
const err = throwAndCatch({
message: 'CONNECTION TERMINATED DUE TO CONNECTION TIMEOUT',
formattedError: 'ERROR: 08000: CONNECTION TERMINATED\nHINT: retry later',
})
expect(err).toBeInstanceOf(ConnectionTimeoutError)
expect(err.formattedError).toBe('ERROR: 08000: CONNECTION TERMINATED\nHINT: retry later')
})
it('preserves formattedError on unclassified errors (permission denied with HINT)', () => {
const err = throwAndCatch({
message: 'permission denied for table users',
code: 400,
formattedError:
'ERROR: 42501: permission denied for table users\n' +
'HINT: To grant access to anon on a specific table:\n' +
' GRANT SELECT ON TABLE public.users TO anon;',
})
expect(err).toBeInstanceOf(UnknownAPIResponseError)
expect(err.formattedError).toContain('ERROR:')
expect(err.formattedError).toContain('HINT:')
expect(err.formattedError?.split('\n').length).toBeGreaterThan(1)
})
it('leaves formattedError undefined when raw error omits it', () => {
const err = throwAndCatch({ message: 'some error' })
expect(err.formattedError).toBeUndefined()
})
it('ignores non-string formattedError values', () => {
const err = throwAndCatch({ message: 'some error', formattedError: 42 })
expect(err.formattedError).toBeUndefined()
})
})
})