mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 07:29:16 +00:00
80 lines
2.9 KiB
Markdown
80 lines
2.9 KiB
Markdown
# Studio Unit & Component Testing
|
|
|
|
See `.agents/skills/studio-testing/SKILL.md` (and its
|
|
`references/msw-component-tests.md` guide) for the full decision gate, quality
|
|
criteria, and MSW gotchas.
|
|
|
|
## Conventions
|
|
|
|
- **Co-locate new tests** beside the source file (`MyComponent.test.tsx`,
|
|
`MyComponent.utils.test.ts`). Legacy tests still live under `apps/studio/tests/`.
|
|
- **Choose the environment by extension** (`vitest.config.ts`):
|
|
- `*.test.ts` runs in `node` (fast — use for pure logic).
|
|
- `*.test.tsx` runs in `jsdom` (use for components, `renderHook`, or DOM APIs).
|
|
- `*.platform.test.tsx` runs with `NEXT_PUBLIC_IS_PLATFORM=true`.
|
|
- **Skip low-value tests:** Do not add tests for TypeScript/ESLint/Knip
|
|
guarantees, static copy, constants, thin wrappers, or standard `data/**`
|
|
React Query hooks and `keys.ts` factories.
|
|
|
|
## Rendering Components, Hooks, and Routes
|
|
|
|
Use `customRender` and `customRenderHook` from `@/tests/lib/custom-render`
|
|
(wraps `QueryClientProvider`, `TooltipProvider`, `NuqsTestingAdapter`, and
|
|
TanStack Router memory history):
|
|
|
|
```tsx
|
|
import { customRender, customRenderHook } from '@/tests/lib/custom-render'
|
|
import { createMockProfileContext } from '@/tests/lib/profile-helpers'
|
|
|
|
customRender(<MyComponent />, {
|
|
initialUrl: '/project/default/issues',
|
|
nuqs: { searchParams: { search: 'hello' } },
|
|
profileContext: createMockProfileContext(),
|
|
})
|
|
|
|
customRenderHook(() => useMyHook())
|
|
```
|
|
|
|
To mount a TanStack Start file route that reads `Route.useParams()`, use
|
|
`renderRoute` from `@/tests/lib/route-render`.
|
|
|
|
## Mocking API Requests with MSW
|
|
|
|
Mock API calls at the network boundary with `addAPIMock` (`@/tests/lib/msw`),
|
|
not `vi.mock('@/data/...')`. Register `addAPIMock` per test or in `beforeEach`
|
|
(never `beforeAll` — handlers reset after every test in
|
|
`apps/studio/tests/vitestSetup.ts`), and unhandled requests fail tests loudly.
|
|
|
|
```tsx
|
|
import { platformComponents as components } from 'api-types'
|
|
import { HttpResponse } from 'msw'
|
|
|
|
import { addAPIMock, type APIErrorBody } from '@/tests/lib/msw'
|
|
|
|
type OrganizationResponse = components['schemas']['OrganizationResponse_Output']
|
|
|
|
addAPIMock({
|
|
method: 'get',
|
|
path: '/platform/organizations',
|
|
response: () => HttpResponse.json<OrganizationResponse[]>([]),
|
|
})
|
|
|
|
// Error response
|
|
addAPIMock({
|
|
method: 'get',
|
|
path: '/platform/organizations',
|
|
response: () => HttpResponse.json<APIErrorBody>({ message: 'Failed' }, { status: 500 }),
|
|
})
|
|
```
|
|
|
|
- Global defaults live in `apps/studio/tests/lib/msw-global-api-mocks.ts`.
|
|
- Mock FGA permissions with `mockPermissions` / `mockPermissionsError`
|
|
(`@/tests/lib/msw-permissions`), `allPermissionsFor` / `NO_PERMISSIONS`
|
|
(`@/tests/lib/permissions-fixtures`), or `mockProjectGrantContext`
|
|
(`@/tests/lib/project-grant-mocks`).
|
|
|
|
## Popovers & Dropdowns
|
|
|
|
Use `userEvent.click` for Popovers, and `clickDropdown(element)` from
|
|
`@/tests/helpers` when opening a Radix Dropdown trigger in jsdom.
|