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
272 lines
8.6 KiB
TypeScript
272 lines
8.6 KiB
TypeScript
import { describe, expect, it } from 'vitest'
|
|
|
|
import { SUPABASE_TARGET_SCHEMA_OPTION, WRAPPER_HANDLERS } from './Wrappers.constants'
|
|
import { Table } from './Wrappers.types'
|
|
import {
|
|
getEditionFormSchema,
|
|
getRequiredExtensionsToInstall,
|
|
getTableFormSchema,
|
|
getWrapperCreationFormSchema,
|
|
hasForeignSchemaSupport,
|
|
} from './Wrappers.utils'
|
|
|
|
const stripe_wrapper = {
|
|
name: 'stripe_wrapper',
|
|
handlerName: WRAPPER_HANDLERS.STRIPE,
|
|
validatorName: 'stripe_fdw_validator',
|
|
icon: '',
|
|
description: 'Payment processing and subscription management',
|
|
extensionName: 'StripeFdw',
|
|
label: 'Stripe',
|
|
docsUrl: '',
|
|
server: {
|
|
options: [
|
|
{
|
|
name: 'api_key_id',
|
|
label: 'Stripe Secret Key',
|
|
required: true,
|
|
encrypted: true,
|
|
secureEntry: true,
|
|
urlHelper: 'https://stripe.com/docs/keys',
|
|
},
|
|
{
|
|
name: 'api_url',
|
|
label: 'Stripe API URL',
|
|
defaultValue: 'https://api.stripe.com/v1',
|
|
required: false,
|
|
encrypted: false,
|
|
secureEntry: false,
|
|
},
|
|
SUPABASE_TARGET_SCHEMA_OPTION,
|
|
],
|
|
},
|
|
tables: [],
|
|
}
|
|
|
|
describe('getWrapperCreationFormSchema', () => {
|
|
it('should build a schema that validates tables mode and wrapper specific options', () => {
|
|
const schema = getWrapperCreationFormSchema(stripe_wrapper)
|
|
const result = schema.safeParse({ mode: 'tables' })
|
|
|
|
expect(result.success).toEqual(false)
|
|
// Common required field
|
|
expect(
|
|
result.error?.issues.find((issue) => issue.path.some((p) => p === 'server_name'))
|
|
).toBeDefined()
|
|
// Table mode specific field
|
|
expect(
|
|
result.error?.issues.find((issue) => issue.path.some((p) => p === 'tables'))
|
|
).toBeDefined()
|
|
// Required wrapper option
|
|
expect(
|
|
result.error?.issues.find((issue) => issue.path.some((p) => p === 'api_key_id'))
|
|
).toBeDefined()
|
|
// Optional wrapper option
|
|
expect(
|
|
result.error?.issues.find((issue) => issue.path.some((p) => p === 'api_url'))
|
|
).toBeUndefined()
|
|
})
|
|
|
|
it('should build a schema that validates schema mode', () => {
|
|
const schema = getWrapperCreationFormSchema(stripe_wrapper)
|
|
const result = schema.safeParse({ mode: 'schema' })
|
|
|
|
expect(result.success).toEqual(false)
|
|
// Common required field
|
|
expect(
|
|
result.error?.issues.find((issue) => issue.path.some((p) => p === 'server_name'))
|
|
).toBeDefined()
|
|
// Schema mode specific field
|
|
expect(
|
|
result.error?.issues.find((issue) => issue.path.some((p) => p === 'source_schema'))
|
|
).toBeDefined()
|
|
expect(
|
|
result.error?.issues.find((issue) => issue.path.some((p) => p === 'target_schema'))
|
|
).toBeDefined()
|
|
})
|
|
})
|
|
|
|
describe('getEditionFormSchema', () => {
|
|
it('should build a schema that validates wrapper specific options', () => {
|
|
const schema = getEditionFormSchema(stripe_wrapper)
|
|
const result = schema.safeParse({ mode: 'tables' })
|
|
expect(result.success).toEqual(false)
|
|
// Common required field
|
|
expect(
|
|
result.error?.issues.find((issue) => issue.path.some((p) => p === 'server_name'))
|
|
).toBeDefined()
|
|
expect(
|
|
result.error?.issues.find((issue) => issue.path.some((p) => p === 'tables'))
|
|
).toBeDefined()
|
|
// Required option
|
|
expect(
|
|
result.error?.issues.find((issue) => issue.path.some((p) => p === 'api_key_id'))
|
|
).toBeDefined()
|
|
// Optional option
|
|
expect(
|
|
result.error?.issues.find((issue) => issue.path.some((p) => p === 'api_url'))
|
|
).toBeUndefined()
|
|
})
|
|
})
|
|
|
|
describe('getTableFormSchema', () => {
|
|
const table_schema = {
|
|
label: 'Accounts',
|
|
description: 'List of accounts on your Stripe account',
|
|
availableColumns: [
|
|
{
|
|
name: 'id',
|
|
type: 'text',
|
|
},
|
|
{
|
|
name: 'business_type',
|
|
type: 'text',
|
|
},
|
|
{
|
|
name: 'country',
|
|
type: 'text',
|
|
},
|
|
{
|
|
name: 'email',
|
|
type: 'text',
|
|
},
|
|
{
|
|
name: 'type',
|
|
type: 'text',
|
|
},
|
|
{
|
|
name: 'created',
|
|
type: 'timestamp',
|
|
},
|
|
{
|
|
name: 'attrs',
|
|
type: 'jsonb',
|
|
},
|
|
],
|
|
options: [
|
|
{
|
|
name: 'object',
|
|
defaultValue: 'accounts',
|
|
editable: false,
|
|
required: true,
|
|
type: 'text',
|
|
},
|
|
{
|
|
name: 'rowid_column',
|
|
label: 'Row ID Column',
|
|
defaultValue: 'id',
|
|
editable: true,
|
|
required: false,
|
|
type: 'text',
|
|
},
|
|
],
|
|
} satisfies Table
|
|
|
|
it('should build a schema that validates tables with existing schema', () => {
|
|
const schema = getTableFormSchema(table_schema)
|
|
const result = schema.safeParse({})
|
|
expect(result.success).toEqual(false)
|
|
// Common required field
|
|
expect(
|
|
result.error?.issues.find((issue) => issue.path.some((p) => p === 'table_name'))
|
|
).toBeDefined()
|
|
expect(
|
|
result.error?.issues.find((issue) => issue.path.some((p) => p === 'schema'))
|
|
).toBeDefined()
|
|
expect(
|
|
result.error?.issues.find((issue) => issue.path.some((p) => p === 'columns'))
|
|
).toBeDefined()
|
|
// Required options
|
|
expect(
|
|
result.error?.issues.find((issue) => issue.path.some((p) => p === 'object'))
|
|
).toBeDefined()
|
|
// Optional options
|
|
expect(
|
|
result.error?.issues.find((issue) => issue.path.some((p) => p === 'rowid_column'))
|
|
).toBeUndefined()
|
|
})
|
|
|
|
it('should build a schema that validates tables with custom schema', () => {
|
|
const schema = getTableFormSchema(table_schema)
|
|
// Because the schema_name is validated in zod superRefine, the normal validation
|
|
// must have succeeded first
|
|
const result = schema.safeParse({
|
|
table_name: 'test',
|
|
schema: 'custom',
|
|
columns: [],
|
|
object: 'acounts',
|
|
})
|
|
|
|
expect(result.success).toEqual(false)
|
|
expect(
|
|
result.error?.issues.find((issue) => issue.path.some((p) => p === 'schema_name'))
|
|
).toBeDefined()
|
|
})
|
|
})
|
|
|
|
// ─── getRequiredExtensionsToInstall ──────────────────────────────────────────
|
|
|
|
describe('getRequiredExtensionsToInstall', () => {
|
|
type Extension = { name: string; installed_version: string | null }
|
|
const wrappers: Extension = { name: 'wrappers', installed_version: null }
|
|
const vault = { name: 'supabase_vault', installed_version: null }
|
|
const wrappersInstalled = { name: 'wrappers', installed_version: '0.4.1' }
|
|
const vaultInstalled = { name: 'supabase_vault', installed_version: '0.2.8' }
|
|
const unrelated = { name: 'pgmq', installed_version: null }
|
|
|
|
it('returns null when extensions are undefined (still loading)', () => {
|
|
expect(getRequiredExtensionsToInstall(undefined, ['wrappers'])).toBeNull()
|
|
})
|
|
|
|
it.each<{ case: string; extensions: Extension[]; required: string[] }>([
|
|
{ case: 'the required list is empty', extensions: [wrappers], required: [] },
|
|
{
|
|
case: 'all required extensions are already installed',
|
|
extensions: [wrappersInstalled, vaultInstalled],
|
|
required: ['wrappers', 'supabase_vault'],
|
|
},
|
|
{ case: 'the extensions list is empty', extensions: [], required: ['wrappers'] },
|
|
])('returns an empty array when $case', ({ extensions, required }) => {
|
|
expect(getRequiredExtensionsToInstall(extensions, required)).toEqual([])
|
|
})
|
|
|
|
it('returns extensions that are required but not installed', () => {
|
|
const result = getRequiredExtensionsToInstall(
|
|
[wrappers, vaultInstalled],
|
|
['wrappers', 'supabase_vault']
|
|
)
|
|
expect(result).toHaveLength(1)
|
|
expect(result![0].name).toBe('wrappers')
|
|
})
|
|
|
|
it('returns all required extensions when none are installed', () => {
|
|
const result = getRequiredExtensionsToInstall([wrappers, vault], ['wrappers', 'supabase_vault'])
|
|
expect(result).toHaveLength(2)
|
|
})
|
|
|
|
it('excludes extensions that are not in the required list', () => {
|
|
const result = getRequiredExtensionsToInstall([wrappers, unrelated], ['wrappers'])
|
|
expect(result).toHaveLength(1)
|
|
expect(result![0].name).toBe('wrappers')
|
|
})
|
|
})
|
|
|
|
// ─── hasForeignSchemaSupport ──────────────────────────────────────────────────
|
|
|
|
describe('hasForeignSchemaSupport', () => {
|
|
it('returns false when extension is undefined', () => {
|
|
expect(hasForeignSchemaSupport(undefined)).toBe(false)
|
|
})
|
|
|
|
it.each([
|
|
[null, false],
|
|
['0.1.0', false],
|
|
['0.4.9', false],
|
|
['0.5.0', true],
|
|
['0.5.1', true],
|
|
['1.0.0', true],
|
|
])('installed_version %s returns %s', (installed_version, expected) => {
|
|
expect(hasForeignSchemaSupport({ installed_version })).toBe(expected)
|
|
})
|
|
})
|