createTestContext

Create a test context for testing middleware and server functions. Builds a complete context object with a Request, cookies, headers, and route parameters.

Import

import { createTestContext } from 'catmint/testing'

Signature

function createTestContext(options?: TestContextOptions): TestContext

Parameters

ParameterTypeRequiredDescription
optionsTestContextOptionsNoConfiguration for the test context.

TestContextOptions

PropertyTypeRequiredDescription
cookiesRecord<string, string>NoCookie key-value pairs. Serialized into the Cookie header automatically.
headersRecord<string, string>NoRequest headers to include.
paramsRecord<string, string>NoRoute parameters (e.g. { id: '42' }).

Return Value

Returns a TestContext object:

interface TestContext {
  /** The underlying Request object */
  request: Request
  /** Cookie key-value pairs */
  cookies: Record<string, string>
  /** Request headers */
  headers: Headers
  /** Route parameters */
  params: Record<string, string>
}

The request is a standard Request object pointing at http://localhost/ with the configured headers and cookies applied.

Examples

import { createTestContext } from 'catmint/testing'

// Basic context with no options
const ctx = createTestContext()
console.log(ctx.request.url) // "http://localhost/"
// Context with cookies and route params
const ctx = createTestContext({
  cookies: { session: 'abc123' },
  params: { id: '42' },
  headers: { 'X-Custom': 'value' },
})

const result = await myMiddleware(ctx.request, async () => {
  return new Response('OK')
})
// Testing a server function
const ctx = createTestContext({
  cookies: { auth: 'token-xyz' },
})

const data = await myServerFunction(ctx.request)
expect(data.authenticated).toBe(true)

See Also