mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 07:29:16 +00:00
## 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
209 lines
7.5 KiB
TypeScript
209 lines
7.5 KiB
TypeScript
import { describe, expect, it, vi } from 'vitest'
|
|
|
|
import {
|
|
getItemsAffectingProjectCreation,
|
|
getRegionRestrictionCopy,
|
|
getRegionRestrictionMessage,
|
|
parseRestrictedRegions,
|
|
REGION_RESTRICTION_COPY,
|
|
regionMatches,
|
|
resolveRegionRestriction,
|
|
SELECT_DIFFERENT_REGION,
|
|
} from './RegionSelector.utils'
|
|
import type { StatusItem } from '@/lib/status-page/status-page.utils'
|
|
|
|
vi.mock('@/lib/error-reporting', () => ({ captureCriticalError: vi.fn() }))
|
|
|
|
describe('parseRestrictedRegions', () => {
|
|
it.each([
|
|
['the false that useFlag returns when unresolved or errored', false],
|
|
['undefined', undefined],
|
|
['an empty string', ''],
|
|
['an empty object', '{}'],
|
|
['invalid JSON', 'not json'],
|
|
['an unknown status value', '{"sa-east-1":"bogus"}'],
|
|
['a status value the flag no longer supports', '{"sa-east-1":"paid_only"}'],
|
|
['an array instead of a record', '["sa-east-1"]'],
|
|
['a non-string value', 42],
|
|
])('fails open to no restrictions for %s', (_label, flagValue) => {
|
|
expect(parseRestrictedRegions(flagValue)).toEqual({})
|
|
})
|
|
|
|
it('returns the restrictions keyed by region code for a valid payload', () => {
|
|
expect(
|
|
parseRestrictedRegions('{"sa-east-1":"unavailable","eu-central-1":"unavailable"}')
|
|
).toEqual({ 'sa-east-1': 'unavailable', 'eu-central-1': 'unavailable' })
|
|
})
|
|
|
|
it('drops every restriction when any entry is invalid', () => {
|
|
expect(parseRestrictedRegions('{"sa-east-1":"unavailable","eu-central-1":"bogus"}')).toEqual({})
|
|
})
|
|
|
|
it('reports a present-but-invalid payload without reporting the unresolved flag value', async () => {
|
|
const { captureCriticalError } = await import('@/lib/error-reporting')
|
|
vi.mocked(captureCriticalError).mockClear()
|
|
|
|
parseRestrictedRegions(false)
|
|
parseRestrictedRegions('')
|
|
expect(captureCriticalError).not.toHaveBeenCalled()
|
|
|
|
parseRestrictedRegions('not json')
|
|
parseRestrictedRegions('{"sa-east-1":"bogus"}')
|
|
expect(captureCriticalError).toHaveBeenCalledTimes(2)
|
|
})
|
|
})
|
|
|
|
describe('resolveRegionRestriction', () => {
|
|
it('lets the platform status win over the flag', () => {
|
|
expect(
|
|
resolveRegionRestriction({ platformStatus: 'capacity', flagRestriction: 'unavailable' })
|
|
).toBe('capacity')
|
|
expect(
|
|
resolveRegionRestriction({ platformStatus: 'other', flagRestriction: 'unavailable' })
|
|
).toBe('other')
|
|
})
|
|
|
|
it('applies the flag only where the platform reports no status', () => {
|
|
expect(
|
|
resolveRegionRestriction({ platformStatus: undefined, flagRestriction: 'unavailable' })
|
|
).toBe('unavailable')
|
|
})
|
|
|
|
it('passes a platform status it does not recognise through unchanged', () => {
|
|
expect(
|
|
resolveRegionRestriction({ platformStatus: 'brand_new', flagRestriction: undefined })
|
|
).toBe('brand_new')
|
|
})
|
|
|
|
it('returns no restriction when neither source restricts the region', () => {
|
|
expect(
|
|
resolveRegionRestriction({ platformStatus: undefined, flagRestriction: undefined })
|
|
).toBeUndefined()
|
|
})
|
|
})
|
|
|
|
describe('getRegionRestrictionCopy', () => {
|
|
it('uses the capacity copy for a capacity status', () => {
|
|
expect(getRegionRestrictionCopy('capacity')).toBe(REGION_RESTRICTION_COPY.capacity)
|
|
})
|
|
|
|
it.each(['other', 'unavailable', 'brand_new', 'toString'])(
|
|
'falls back to the generic copy for %s',
|
|
(restriction) => {
|
|
expect(getRegionRestrictionCopy(restriction)).toBe(REGION_RESTRICTION_COPY.other)
|
|
}
|
|
)
|
|
|
|
it('builds the submit message from the title and the shared call to action', () => {
|
|
expect(getRegionRestrictionMessage('capacity')).toBe(
|
|
`Selected region is at capacity. ${SELECT_DIFFERENT_REGION}`
|
|
)
|
|
expect(getRegionRestrictionMessage('brand_new')).toBe(
|
|
`Selected region is unavailable. ${SELECT_DIFFERENT_REGION}`
|
|
)
|
|
})
|
|
})
|
|
|
|
describe('regionMatches', () => {
|
|
it('matches an exact region code', () => {
|
|
expect(regionMatches('us-east-1', 'us-east-1')).toBe(true)
|
|
})
|
|
|
|
it('matches a smart group against a specific region it contains', () => {
|
|
expect(regionMatches('americas', 'us-east-1')).toBe(true)
|
|
})
|
|
|
|
it('does not match a smart group against a region from a different group', () => {
|
|
expect(regionMatches('apac', 'us-east-1')).toBe(false)
|
|
})
|
|
|
|
it.each([
|
|
['US-EAST-1', 'us-east-1'],
|
|
['us-east-1', 'US-EAST-1'],
|
|
['AMERICAS', 'us-east-1'],
|
|
])('matches case-insensitively (%s vs %s)', (selectedCode, affectedRegion) => {
|
|
expect(regionMatches(selectedCode, affectedRegion)).toBe(true)
|
|
})
|
|
|
|
it('does not match unrelated specific regions', () => {
|
|
expect(regionMatches('us-east-1', 'eu-central-1')).toBe(false)
|
|
})
|
|
})
|
|
|
|
describe('getItemsAffectingProjectCreation', () => {
|
|
type IncidentItem = Extract<StatusItem, { kind: 'incident' }>
|
|
type UpcomingMaintenanceItem = Extract<StatusItem, { kind: 'upcoming_maintenance' }>
|
|
|
|
function buildItem(overrides: Partial<IncidentItem> = {}): IncidentItem {
|
|
return {
|
|
id: 'incident-1',
|
|
name: 'Project creation degraded',
|
|
url: 'https://status.supabase.com/incidents/1',
|
|
lastUpdateAt: '2026-01-01T00:00:00Z',
|
|
lastUpdateMessage: null,
|
|
components: [],
|
|
scope: { type: 'global' },
|
|
projectCreationScope: null,
|
|
isProjectCreationOnly: true,
|
|
showBanner: true,
|
|
kind: 'incident',
|
|
status: 'investigating',
|
|
impact: 'partial_outage',
|
|
...overrides,
|
|
}
|
|
}
|
|
|
|
function buildUpcomingMaintenanceItem(
|
|
overrides: Partial<UpcomingMaintenanceItem> = {}
|
|
): UpcomingMaintenanceItem {
|
|
return {
|
|
id: 'maintenance-1',
|
|
name: 'Upcoming project creation maintenance',
|
|
url: 'https://status.supabase.com/incidents/2',
|
|
lastUpdateAt: '2026-01-01T00:00:00Z',
|
|
lastUpdateMessage: null,
|
|
components: [],
|
|
scope: { type: 'global' },
|
|
projectCreationScope: null,
|
|
isProjectCreationOnly: true,
|
|
showBanner: true,
|
|
kind: 'upcoming_maintenance',
|
|
startsAt: '2026-02-01T00:00:00Z',
|
|
endsAt: '2026-02-02T00:00:00Z',
|
|
leadDays: 7,
|
|
...overrides,
|
|
}
|
|
}
|
|
|
|
it('includes a global project creation item regardless of the selected region', () => {
|
|
const item = buildItem({ projectCreationScope: { type: 'global' } })
|
|
expect(getItemsAffectingProjectCreation([item], undefined)).toEqual([item])
|
|
expect(getItemsAffectingProjectCreation([item], 'us-east-1')).toEqual([item])
|
|
})
|
|
|
|
it('excludes a region-scoped item when no region is selected', () => {
|
|
const item = buildItem({ projectCreationScope: { type: 'regions', regions: ['us-east-1'] } })
|
|
expect(getItemsAffectingProjectCreation([item], undefined)).toEqual([])
|
|
})
|
|
|
|
it('excludes an item that does not affect project creation', () => {
|
|
const item = buildItem({ projectCreationScope: null })
|
|
expect(getItemsAffectingProjectCreation([item], 'us-east-1')).toEqual([])
|
|
})
|
|
|
|
it('excludes upcoming maintenance even with a matching project creation scope', () => {
|
|
const item = buildUpcomingMaintenanceItem({ projectCreationScope: { type: 'global' } })
|
|
expect(getItemsAffectingProjectCreation([item], undefined)).toEqual([])
|
|
})
|
|
|
|
it('still includes an item with showBanner false', () => {
|
|
const item = buildItem({ projectCreationScope: { type: 'global' }, showBanner: false })
|
|
expect(getItemsAffectingProjectCreation([item], undefined)).toEqual([item])
|
|
})
|
|
|
|
it('includes a region-scoped item matching the selected region via a smart group', () => {
|
|
const item = buildItem({ projectCreationScope: { type: 'regions', regions: ['us-east-1'] } })
|
|
expect(getItemsAffectingProjectCreation([item], 'americas')).toEqual([item])
|
|
})
|
|
})
|