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
130 lines
4.5 KiB
TypeScript
130 lines
4.5 KiB
TypeScript
import { describe, expect, it } from 'vitest'
|
|
|
|
import { buildEntityMaps, getApiEndpoint } from './DataApi.utils'
|
|
import type { ProjectJsonSchemaPaths } from '@/data/docs/project-json-schema-query'
|
|
import type { LoadBalancer } from '@/data/read-replicas/load-balancers-query'
|
|
import type { Database } from '@/data/read-replicas/replicas-query'
|
|
|
|
const makeDatabase = (
|
|
identifier: string,
|
|
restUrl: string
|
|
): Pick<Database, 'identifier' | 'restUrl'> => ({ identifier, restUrl })
|
|
|
|
const makeLoadBalancer = (endpoint: string): Pick<LoadBalancer, 'endpoint'> => ({ endpoint })
|
|
|
|
describe('getApiEndpoint', () => {
|
|
it('returns custom domain URL with /rest/v1/ when custom domain is active and primary database is selected', () => {
|
|
expect(
|
|
getApiEndpoint({
|
|
selectedDatabaseId: 'project-ref',
|
|
projectRef: 'project-ref',
|
|
resolvedEndpoint: 'https://api.example.com',
|
|
loadBalancers: undefined,
|
|
selectedDatabase: makeDatabase(
|
|
'project-ref',
|
|
'https://project-ref.supabase.co/rest/v1/'
|
|
) as Database,
|
|
})
|
|
).toBe('https://api.example.com/rest/v1/')
|
|
})
|
|
|
|
it('returns database restUrl when custom domain is active but a replica is selected', () => {
|
|
expect(
|
|
getApiEndpoint({
|
|
selectedDatabaseId: 'replica-1',
|
|
projectRef: 'project-ref',
|
|
resolvedEndpoint: 'https://api.example.com',
|
|
loadBalancers: undefined,
|
|
selectedDatabase: makeDatabase(
|
|
'replica-1',
|
|
'https://replica-1.supabase.co/rest/v1/'
|
|
) as Database,
|
|
})
|
|
).toBe('https://replica-1.supabase.co/rest/v1/')
|
|
})
|
|
|
|
it('normalizes a replica restUrl without a trailing slash', () => {
|
|
expect(
|
|
getApiEndpoint({
|
|
selectedDatabaseId: 'replica-1',
|
|
projectRef: 'project-ref',
|
|
resolvedEndpoint: undefined,
|
|
loadBalancers: undefined,
|
|
selectedDatabase: makeDatabase(
|
|
'replica-1',
|
|
'https://replica-1.supabase.co/rest/v1'
|
|
) as Database,
|
|
})
|
|
).toBe('https://replica-1.supabase.co/rest/v1/')
|
|
})
|
|
|
|
it('returns load balancer endpoint with /rest/v1/ when load balancer is selected', () => {
|
|
expect(
|
|
getApiEndpoint({
|
|
selectedDatabaseId: 'load-balancer',
|
|
projectRef: 'project-ref',
|
|
resolvedEndpoint: 'https://project-ref.supabase.co',
|
|
loadBalancers: [makeLoadBalancer('https://lb.supabase.co') as LoadBalancer],
|
|
selectedDatabase: undefined,
|
|
})
|
|
).toBe('https://lb.supabase.co/rest/v1/')
|
|
})
|
|
|
|
it('returns empty string when load balancer is selected but none exist', () => {
|
|
expect(
|
|
getApiEndpoint({
|
|
selectedDatabaseId: 'load-balancer',
|
|
projectRef: 'project-ref',
|
|
resolvedEndpoint: 'https://project-ref.supabase.co',
|
|
loadBalancers: undefined,
|
|
selectedDatabase: undefined,
|
|
})
|
|
).toBe('')
|
|
})
|
|
})
|
|
|
|
describe('buildEntityMaps', () => {
|
|
it.each([undefined, {}])('returns empty maps for %j paths', (paths) => {
|
|
expect(buildEntityMaps(paths)).toEqual({ resources: {}, rpcs: {} })
|
|
})
|
|
|
|
it('skips the root path', () => {
|
|
const paths: ProjectJsonSchemaPaths = {
|
|
'/': { get: {} as ProjectJsonSchemaPaths[string]['get'] },
|
|
}
|
|
expect(buildEntityMaps(paths)).toEqual({ resources: {}, rpcs: {} })
|
|
})
|
|
|
|
it('enriches entities with displayName and camelCase', () => {
|
|
const paths: ProjectJsonSchemaPaths = {
|
|
'/user_profiles': { get: {} as ProjectJsonSchemaPaths[string]['get'] },
|
|
'/rpc/get_user_count': { post: {} as ProjectJsonSchemaPaths[string]['post'] },
|
|
}
|
|
const result = buildEntityMaps(paths)
|
|
|
|
expect(result.resources['user_profiles']).toEqual({
|
|
id: 'user_profiles',
|
|
displayName: 'user profiles',
|
|
camelCase: 'userProfiles',
|
|
})
|
|
expect(result.rpcs['get_user_count']).toEqual({
|
|
id: 'get_user_count',
|
|
displayName: 'get user count',
|
|
camelCase: 'getUserCount',
|
|
})
|
|
})
|
|
|
|
it('classifies rpc/ paths as rpcs and other paths as resources', () => {
|
|
const paths: ProjectJsonSchemaPaths = {
|
|
'/': { get: {} as ProjectJsonSchemaPaths[string]['get'] },
|
|
'/users': { get: {} as ProjectJsonSchemaPaths[string]['get'] },
|
|
'/rpc/hello': { post: {} as ProjectJsonSchemaPaths[string]['post'] },
|
|
'/posts': { get: {} as ProjectJsonSchemaPaths[string]['get'] },
|
|
'/rpc/goodbye': { post: {} as ProjectJsonSchemaPaths[string]['post'] },
|
|
}
|
|
const result = buildEntityMaps(paths)
|
|
expect(Object.keys(result.resources)).toEqual(['users', 'posts'])
|
|
expect(Object.keys(result.rpcs)).toEqual(['hello', 'goodbye'])
|
|
})
|
|
})
|