2.9 KiB
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 underapps/studio/tests/. - Choose the environment by extension (
vitest.config.ts):*.test.tsruns innode(fast — use for pure logic).*.test.tsxruns injsdom(use for components,renderHook, or DOM APIs).*.platform.test.tsxruns withNEXT_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 andkeys.tsfactories.
Rendering Components, Hooks, and Routes
Use customRender and customRenderHook from @/tests/lib/custom-render
(wraps QueryClientProvider, TooltipProvider, NuqsTestingAdapter, and
TanStack Router memory history):
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.
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), ormockProjectGrantContext(@/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.