mirror of
https://github.com/supabase/supabase.git
synced 2026-12-06 00:19:08 +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
244 lines
6.4 KiB
TypeScript
244 lines
6.4 KiB
TypeScript
import { fireEvent, screen } from '@testing-library/react'
|
|
import { mockAnimationsApi } from 'jsdom-testing-mocks'
|
|
import type { MouseEventHandler, ReactNode } from 'react'
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
|
|
import { ProjectNeedsSecuring } from './ProjectNeedsSecuring'
|
|
import { render } from '@/tests/helpers'
|
|
|
|
const {
|
|
mockUseFlag,
|
|
mockUseProjectLintsQuery,
|
|
mockUseSelectedProjectQuery,
|
|
mockUseTablesQuery,
|
|
mockUseProjectPostgrestConfigQuery,
|
|
mockUseTablePrivilegesQuery,
|
|
mockUseLocalStorageQuery,
|
|
mockUseRouter,
|
|
mockRouterPush,
|
|
} = vi.hoisted(() => ({
|
|
mockUseFlag: vi.fn(),
|
|
mockUseProjectLintsQuery: vi.fn(),
|
|
mockUseSelectedProjectQuery: vi.fn(),
|
|
mockUseTablesQuery: vi.fn(),
|
|
mockUseProjectPostgrestConfigQuery: vi.fn(),
|
|
mockUseTablePrivilegesQuery: vi.fn(),
|
|
mockUseLocalStorageQuery: vi.fn(),
|
|
mockUseRouter: vi.fn(),
|
|
mockRouterPush: vi.fn(),
|
|
}))
|
|
|
|
vi.mock('common', async () => {
|
|
const actual = await vi.importActual<typeof import('common')>('common')
|
|
|
|
return {
|
|
...actual,
|
|
useFlag: mockUseFlag,
|
|
useParams: () => ({ ref: 'project-ref' }),
|
|
}
|
|
})
|
|
|
|
vi.mock('next/router', () => ({
|
|
useRouter: () => mockUseRouter(),
|
|
}))
|
|
|
|
vi.mock('next/link', () => ({
|
|
default: ({
|
|
href,
|
|
children,
|
|
onClick,
|
|
...props
|
|
}: {
|
|
href: string
|
|
children: ReactNode
|
|
onClick?: MouseEventHandler<HTMLAnchorElement>
|
|
[key: string]: unknown
|
|
}) => (
|
|
<a href={href} onClick={onClick} {...props}>
|
|
{children}
|
|
</a>
|
|
),
|
|
}))
|
|
|
|
vi.mock('@/data/lint/lint-query', () => ({
|
|
useProjectLintsQuery: mockUseProjectLintsQuery,
|
|
}))
|
|
|
|
vi.mock('@/hooks/misc/useSelectedProject', () => ({
|
|
useSelectedProjectQuery: mockUseSelectedProjectQuery,
|
|
}))
|
|
|
|
vi.mock('@/data/tables/tables-query', () => ({
|
|
useTablesQuery: mockUseTablesQuery,
|
|
}))
|
|
|
|
vi.mock('@/data/config/project-postgrest-config-query', () => ({
|
|
parseDbSchemaString: vi.fn((value: string) => value.split(',').map((schema) => schema.trim())),
|
|
useProjectPostgrestConfigQuery: mockUseProjectPostgrestConfigQuery,
|
|
}))
|
|
|
|
vi.mock('@/data/privileges/table-privileges-query', () => ({
|
|
useTablePrivilegesQuery: mockUseTablePrivilegesQuery,
|
|
}))
|
|
|
|
vi.mock('@/hooks/misc/useLocalStorage', () => ({
|
|
useLocalStorageQuery: mockUseLocalStorageQuery,
|
|
}))
|
|
|
|
vi.mock('sonner', () => ({
|
|
toast: {
|
|
error: vi.fn(),
|
|
},
|
|
}))
|
|
|
|
const issueLint = {
|
|
cache_key: 'lint-1',
|
|
name: 'rls_disabled_in_public',
|
|
detail: 'RLS is disabled on public.invoices',
|
|
description: 'RLS disabled',
|
|
level: 'ERROR',
|
|
categories: ['SECURITY'],
|
|
metadata: {
|
|
schema: 'public',
|
|
name: 'invoices',
|
|
},
|
|
}
|
|
|
|
const tables = [
|
|
{
|
|
id: 1,
|
|
name: 'invoices',
|
|
schema: 'public',
|
|
rls_enabled: false,
|
|
},
|
|
{
|
|
id: 2,
|
|
name: 'profiles',
|
|
schema: 'public',
|
|
rls_enabled: false,
|
|
},
|
|
{
|
|
id: 3,
|
|
name: 'customers',
|
|
schema: 'public',
|
|
rls_enabled: true,
|
|
},
|
|
]
|
|
|
|
const tablePrivileges = [
|
|
{
|
|
schema: 'public',
|
|
name: 'invoices',
|
|
privileges: [
|
|
{
|
|
grantee: 'anon',
|
|
privilege_type: 'SELECT',
|
|
},
|
|
],
|
|
},
|
|
]
|
|
|
|
describe('ProjectNeedsSecuring', () => {
|
|
beforeEach(() => {
|
|
mockAnimationsApi()
|
|
mockUseFlag.mockReturnValue(true)
|
|
mockUseRouter.mockReturnValue({ pathname: '/project/[ref]', push: mockRouterPush })
|
|
mockUseSelectedProjectQuery.mockReturnValue({
|
|
data: { connectionString: 'postgresql://example' },
|
|
})
|
|
mockUseProjectLintsQuery.mockReturnValue({
|
|
data: [issueLint],
|
|
isPending: false,
|
|
isError: false,
|
|
})
|
|
mockUseTablesQuery.mockReturnValue({
|
|
data: tables,
|
|
isPending: false,
|
|
isError: false,
|
|
})
|
|
mockUseProjectPostgrestConfigQuery.mockReturnValue({
|
|
data: 'public',
|
|
isPending: false,
|
|
isError: false,
|
|
})
|
|
mockUseTablePrivilegesQuery.mockReturnValue({
|
|
data: tablePrivileges,
|
|
isPending: false,
|
|
isError: false,
|
|
})
|
|
mockUseLocalStorageQuery.mockReturnValue([null, vi.fn(), { isLoading: false }])
|
|
})
|
|
|
|
afterEach(() => {
|
|
window.localStorage.clear()
|
|
})
|
|
|
|
it('renders the security gate when an exposed table has RLS disabled and the project has not been dismissed', () => {
|
|
render(
|
|
<ProjectNeedsSecuring>
|
|
<div data-testid="project-children">Project content</div>
|
|
</ProjectNeedsSecuring>
|
|
)
|
|
|
|
expect(screen.getByText('Your project needs securing')).toBeInTheDocument()
|
|
expect(screen.getByText('Review and fix')).toBeInTheDocument()
|
|
expect(screen.getByRole('link', { name: 'Open Data API settings' })).toHaveAttribute(
|
|
'href',
|
|
'/project/project-ref/integrations/data_api/settings'
|
|
)
|
|
expect(screen.queryByRole('columnheader', { name: 'Action' })).not.toBeInTheDocument()
|
|
expect(screen.queryByRole('link', { name: 'View policies' })).not.toBeInTheDocument()
|
|
expect(screen.queryByText('profiles')).not.toBeInTheDocument()
|
|
expect(screen.queryByText('customers')).not.toBeInTheDocument()
|
|
expect(screen.getByText('Skip to home')).toBeInTheDocument()
|
|
expect(screen.queryByTestId('project-children')).not.toBeInTheDocument()
|
|
})
|
|
|
|
it('navigates to the table policies page when a table row is clicked', () => {
|
|
render(
|
|
<ProjectNeedsSecuring>
|
|
<div data-testid="project-children">Project content</div>
|
|
</ProjectNeedsSecuring>
|
|
)
|
|
|
|
fireEvent.click(screen.getByText('invoices'))
|
|
|
|
expect(mockRouterPush).toHaveBeenCalledWith(
|
|
'/project/project-ref/database/policies?schema=public&search=invoices'
|
|
)
|
|
})
|
|
|
|
it.each([
|
|
[
|
|
'the security gate has been dismissed',
|
|
() =>
|
|
mockUseLocalStorageQuery.mockReturnValue([
|
|
'2026-04-21T00:00:00.000Z',
|
|
vi.fn(),
|
|
{ isLoading: false },
|
|
]),
|
|
],
|
|
[
|
|
'there are no RLS issues',
|
|
() =>
|
|
mockUseProjectLintsQuery.mockReturnValue({ data: [], isPending: false, isError: false }),
|
|
],
|
|
[
|
|
'on non-home project routes',
|
|
() => mockUseRouter.mockReturnValue({ pathname: '/project/[ref]/database/tables' }),
|
|
],
|
|
['the feature flag is disabled', () => mockUseFlag.mockReturnValue(false)],
|
|
])('renders the project content when %s', (_, arrange) => {
|
|
arrange()
|
|
|
|
render(
|
|
<ProjectNeedsSecuring>
|
|
<div data-testid="project-children">Project content</div>
|
|
</ProjectNeedsSecuring>
|
|
)
|
|
|
|
expect(screen.queryByText('Your project needs securing')).not.toBeInTheDocument()
|
|
expect(screen.getByTestId('project-children')).toBeInTheDocument()
|
|
})
|
|
})
|