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
194 lines
5.0 KiB
TypeScript
194 lines
5.0 KiB
TypeScript
import { describe, expect, test } from 'vitest'
|
|
|
|
import { getEnumsAsMarkdown, getPoliciesAsMarkdown, getSchemaAsMarkdown } from './Schemas.utils'
|
|
|
|
describe('Schemas.utils', () => {
|
|
// getSchemaAsMarkdown composes getTableDefinitionAsMarkdown per table, so this covers both
|
|
test('getSchemaAsMarkdown returns properly formatted markdown', () => {
|
|
const schema = 'public'
|
|
const tables = [
|
|
{
|
|
ref: 'default',
|
|
id: 20999,
|
|
name: 'test',
|
|
description: 'An excellent description',
|
|
schema: 'public',
|
|
isForeign: false,
|
|
columns: [
|
|
{
|
|
id: '20999.1',
|
|
isPrimary: true,
|
|
name: 'id',
|
|
format: 'int8',
|
|
isNullable: false,
|
|
isUnique: false,
|
|
isUpdateable: true,
|
|
isIdentity: true,
|
|
description: '',
|
|
},
|
|
{
|
|
id: '20999.2',
|
|
isPrimary: false,
|
|
name: 'created_at',
|
|
format: 'timestamptz',
|
|
isNullable: false,
|
|
isUnique: false,
|
|
isUpdateable: true,
|
|
isIdentity: false,
|
|
description: '',
|
|
},
|
|
{
|
|
id: '20999.3',
|
|
isPrimary: false,
|
|
name: 'user_id',
|
|
format: 'uuid',
|
|
isNullable: true,
|
|
isUnique: false,
|
|
isUpdateable: true,
|
|
isIdentity: false,
|
|
description: '',
|
|
},
|
|
],
|
|
},
|
|
{
|
|
ref: 'default',
|
|
id: 21049,
|
|
name: 'test2',
|
|
description: '',
|
|
schema: 'public',
|
|
isForeign: false,
|
|
columns: [
|
|
{
|
|
id: '21049.1',
|
|
isPrimary: true,
|
|
name: 'id',
|
|
format: 'int8',
|
|
isNullable: false,
|
|
isUnique: false,
|
|
isUpdateable: true,
|
|
isIdentity: true,
|
|
description: '',
|
|
},
|
|
{
|
|
id: '21049.2',
|
|
isPrimary: false,
|
|
name: 'created_at',
|
|
format: 'timestamptz',
|
|
isNullable: false,
|
|
isUnique: false,
|
|
isUpdateable: true,
|
|
isIdentity: false,
|
|
description: '',
|
|
},
|
|
],
|
|
},
|
|
{
|
|
id: 21009,
|
|
ref: 'default',
|
|
schema: 'auth',
|
|
name: 'auth.users.id',
|
|
description: '',
|
|
isForeign: true,
|
|
columns: [],
|
|
},
|
|
]
|
|
const result = getSchemaAsMarkdown(schema, tables)
|
|
expect(result).toBe(`## Table \`test\`
|
|
|
|
An excellent description
|
|
|
|
### Columns
|
|
|
|
| Name | Type | Constraints |
|
|
|------|------|-------------|
|
|
| \`id\` | \`int8\` | Primary Identity |
|
|
| \`created_at\` | \`timestamptz\` | |
|
|
| \`user_id\` | \`uuid\` | Nullable |
|
|
|
|
## Table \`test2\`
|
|
|
|
### Columns
|
|
|
|
| Name | Type | Constraints |
|
|
|------|------|-------------|
|
|
| \`id\` | \`int8\` | Primary Identity |
|
|
| \`created_at\` | \`timestamptz\` | |
|
|
|
|
`)
|
|
})
|
|
test('getEnumsAsMarkdown returns formatted enum section', () => {
|
|
const enums = [
|
|
{ name: 'order_status', schema: 'public', enums: ['pending', 'processing', 'shipped'] },
|
|
{ name: 'role_type', schema: 'public', enums: ['admin', 'user'] },
|
|
{ name: 'other_enum', schema: 'auth', enums: ['a', 'b'] },
|
|
]
|
|
const result = getEnumsAsMarkdown('public', enums)
|
|
expect(result).toBe(`## Custom Types / Enums
|
|
|
|
### \`order_status\`
|
|
|
|
\`pending\` | \`processing\` | \`shipped\`
|
|
|
|
### \`role_type\`
|
|
|
|
\`admin\` | \`user\`
|
|
|
|
`)
|
|
})
|
|
|
|
test('getEnumsAsMarkdown returns empty string when no enums match schema', () => {
|
|
const enums = [{ name: 'other_enum', schema: 'auth', enums: ['a', 'b'] }]
|
|
const result = getEnumsAsMarkdown('public', enums)
|
|
expect(result).toBe('')
|
|
})
|
|
|
|
test('getPoliciesAsMarkdown returns formatted policy table grouped by table', () => {
|
|
const policies = [
|
|
{
|
|
name: 'users_select',
|
|
schema: 'public',
|
|
table: 'users',
|
|
command: 'SELECT',
|
|
roles: ['authenticated'],
|
|
action: 'PERMISSIVE',
|
|
definition: 'auth.uid() = id',
|
|
check: null,
|
|
},
|
|
{
|
|
name: 'users_insert',
|
|
schema: 'public',
|
|
table: 'users',
|
|
command: 'INSERT',
|
|
roles: ['authenticated'],
|
|
action: 'PERMISSIVE',
|
|
definition: null,
|
|
check: 'auth.uid() = id',
|
|
},
|
|
]
|
|
const result = getPoliciesAsMarkdown('public', policies)
|
|
expect(result).toContain('## RLS Policies')
|
|
expect(result).toContain('### `users`')
|
|
expect(result).toContain('`users_select`')
|
|
expect(result).toContain('`users_insert`')
|
|
expect(result).toContain('`auth.uid() = id`')
|
|
expect(result).toContain('—')
|
|
})
|
|
|
|
test('getPoliciesAsMarkdown returns empty string when no policies match schema', () => {
|
|
const policies = [
|
|
{
|
|
name: 'test',
|
|
schema: 'auth',
|
|
table: 'users',
|
|
command: 'SELECT',
|
|
roles: ['anon'],
|
|
action: 'PERMISSIVE',
|
|
definition: 'true',
|
|
check: null,
|
|
},
|
|
]
|
|
const result = getPoliciesAsMarkdown('public', policies)
|
|
expect(result).toBe('')
|
|
})
|
|
})
|