mirror of
https://github.com/supabase/supabase.git
synced 2026-12-03 23:18:57 +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
109 lines
3.7 KiB
TypeScript
109 lines
3.7 KiB
TypeScript
import { screen } from '@testing-library/react'
|
|
import { HttpResponse } from 'msw'
|
|
import { describe, expect, test, vi } from 'vitest'
|
|
|
|
import { DatabaseFormSchema, PerformanceSettingsForm } from './PerformanceSettingsForm'
|
|
import { customRender } from '@/tests/lib/custom-render'
|
|
import { addAPIMock } from '@/tests/lib/msw'
|
|
|
|
vi.mock('@/lib/constants', async (importOriginal) => {
|
|
const actual = await importOriginal<typeof import('@/lib/constants')>()
|
|
return { ...actual, IS_PLATFORM: true }
|
|
})
|
|
|
|
vi.mock('@/hooks/misc/useSelectedProject', () => ({
|
|
useSelectedProjectQuery: () => ({ data: { ref: 'default', connectionString: null } }),
|
|
}))
|
|
|
|
vi.mock('@/hooks/misc/useCheckEntitlements', () => ({
|
|
useCheckEntitlements: () => ({ hasAccess: true, isLoading: false }),
|
|
}))
|
|
|
|
vi.mock('@/hooks/misc/useCheckPermissionsV2', () => ({
|
|
useAsyncCheckPermissionsV2: () => ({ can: true, isLoading: false, isSuccess: true }),
|
|
FGA_PERMISSIONS: {
|
|
PROJECT: {
|
|
AUTH_CONFIG_READ: 'auth_config_read',
|
|
AUTH_CONFIG_WRITE: 'auth_config_write',
|
|
},
|
|
},
|
|
}))
|
|
|
|
function mockAuthConfig(overrides: Record<string, unknown>) {
|
|
addAPIMock({
|
|
method: 'get',
|
|
path: '/platform/auth/:ref/config',
|
|
response: () =>
|
|
HttpResponse.json<any>({
|
|
API_MAX_REQUEST_DURATION: 10,
|
|
DB_MAX_POOL_SIZE: 10,
|
|
DB_MAX_POOL_SIZE_UNIT: 'connections',
|
|
...overrides,
|
|
}),
|
|
})
|
|
}
|
|
|
|
function mockMaxConnections(maxConnections: number) {
|
|
addAPIMock({
|
|
method: 'post',
|
|
path: '/platform/pg-meta/:ref/query',
|
|
response: () => HttpResponse.json<any>([{ max_connections: maxConnections }]),
|
|
})
|
|
}
|
|
|
|
describe('DatabaseFormSchema', () => {
|
|
test.each([
|
|
{ size: 1, unit: 'percent', valid: true },
|
|
{ size: 100, unit: 'percent', valid: true },
|
|
{ size: 0, unit: 'percent', valid: false },
|
|
{ size: 101, unit: 'percent', valid: false },
|
|
{ size: -5, unit: 'percent', valid: false },
|
|
{ size: '75', unit: 'percent', valid: true },
|
|
{ size: '150', unit: 'percent', valid: false },
|
|
{ size: 1, unit: 'connections', valid: true },
|
|
// No upper schema bound for an absolute connection count
|
|
{ size: 500, unit: 'connections', valid: true },
|
|
{ size: 0, unit: 'connections', valid: false },
|
|
{ size: 10, unit: 'bytes', valid: false },
|
|
])('pool size $size with unit $unit -> valid: $valid', ({ size, unit, valid }) => {
|
|
const result = DatabaseFormSchema.safeParse({
|
|
DB_MAX_POOL_SIZE: size,
|
|
DB_MAX_POOL_SIZE_UNIT: unit,
|
|
})
|
|
expect(result.success).toBe(valid)
|
|
})
|
|
|
|
test('explains the percentage bounds when out of range', () => {
|
|
const result = DatabaseFormSchema.safeParse({
|
|
DB_MAX_POOL_SIZE: 101,
|
|
DB_MAX_POOL_SIZE_UNIT: 'percent',
|
|
})
|
|
const issue = result.error?.issues.find((i) => i.path[0] === 'DB_MAX_POOL_SIZE')
|
|
expect(issue?.message).toBe('Percentage must be between 1 and 100')
|
|
})
|
|
})
|
|
|
|
describe('PerformanceSettingsForm', () => {
|
|
test('reflects a persisted percentage allocation strategy after loading', async () => {
|
|
mockAuthConfig({ DB_MAX_POOL_SIZE: 15, DB_MAX_POOL_SIZE_UNIT: 'percent' })
|
|
mockMaxConnections(60)
|
|
|
|
customRender(<PerformanceSettingsForm />)
|
|
|
|
expect(await screen.findByText('Percentage')).toBeInTheDocument()
|
|
expect(screen.getByDisplayValue('15')).toBeInTheDocument()
|
|
expect(screen.getByText('%')).toBeInTheDocument()
|
|
})
|
|
|
|
test('reflects a persisted absolute allocation strategy after loading', async () => {
|
|
mockAuthConfig({ DB_MAX_POOL_SIZE: 12, DB_MAX_POOL_SIZE_UNIT: 'connections' })
|
|
mockMaxConnections(60)
|
|
|
|
customRender(<PerformanceSettingsForm />)
|
|
|
|
expect(await screen.findByText('Absolute')).toBeInTheDocument()
|
|
expect(screen.getByDisplayValue('12')).toBeInTheDocument()
|
|
expect(screen.getByText('connections')).toBeInTheDocument()
|
|
})
|
|
})
|