renderPage

Renders a page component in a test environment with full route context. Creates a simulated request and runs it through the framework's routing, middleware, and rendering pipeline. Designed for integration tests.

Import

import { renderPage } from 'catmint/testing'

Signature

function renderPage(
  path: string,
  opts?: TestRenderOptions,
): Promise<TestRenderResult>
interface TestRenderOptions {
  params?: Record<string, string>
  cookies?: Record<string, string>
  headers?: Record<string, string>
  search?: Record<string, string>
  method?: string
  body?: BodyInit
}

interface TestRenderResult {
  statusCode: number
  headers: Headers
  html: string
  data?: unknown
}

Parameters

ParameterTypeRequiredDescription
pathstringYesThe URL path to render (e.g. '/dashboard').
optsTestRenderOptionsNoOptions to configure the simulated request.

TestRenderOptions

PropertyTypeRequiredDescription
paramsRecord<string, string>NoRoute parameters to inject (e.g. { id: '42' }).
cookiesRecord<string, string>NoCookies to include in the request.
headersRecord<string, string>NoHTTP headers to include in the request.
searchRecord<string, string>NoURL search/query parameters.
methodstringNoHTTP method. Defaults to 'GET'.
bodyBodyInitNoRequest body (only sent for non-GET/HEAD methods).

Return Value

Returns a Promise<TestRenderResult>:

PropertyTypeDescription
statusCodenumberThe HTTP status code of the response.
headersHeadersThe response headers.
htmlstringThe rendered HTML string.
dataunknownOptional data returned by status responses.

Examples

Basic page render test

import { renderPage } from 'catmint/testing'

test('renders the dashboard', async () => {
  const result = await renderPage('/dashboard', {
    cookies: { session: 'valid-token' },
  })

  expect(result.statusCode).toBe(200)
  expect(result.html).toContain('Dashboard')
})

Testing with search params

const result = await renderPage('/products', {
  search: { category: 'shoes', sort: 'price' },
})

expect(result.statusCode).toBe(200)

Testing redirects

const result = await renderPage('/admin', {
  // No session cookie — should redirect
})

expect(result.statusCode).toBe(302)
expect(result.headers.get('Location')).toBe('/login')

Testing with a POST body

const result = await renderPage('/api/submit', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({ name: 'Alice' }),
})

expect(result.statusCode).toBe(200)

See Also