LogoRobo.js
Packages@robojs/server

testRoute()

Function: testRoute()

function testRoute(module, options): Promise<TestRouteResult>

Tests an API route module by dispatching to the appropriate handler. Handles method routing, OPTIONS auto-handling, HEAD fallback, and response conversion.

Parameters

ParameterType
moduleApiHandlerModule
optionsTestRouteOptions

Returns

Promise<TestRouteResult>

Example

import { testRoute } from '@robojs/server/testing'
import * as usersRoute from '../src/api/users/[id]'

// Test GET request
const result = await testRoute(usersRoute, {
  method: 'GET',
  params: { id: '123' }
})
expect(result.status).toBe(200)
expect(await result.json()).toEqual({ id: '123' })

// Test POST request with body
const postResult = await testRoute(usersRoute, {
  method: 'POST',
  body: { name: 'John' }
})
expect(postResult.status).toBe(201)

On this page