Skip to content

Latest commit

 

History

History
210 lines (121 loc) · 6.14 KB

NtarhInitAppRouter.md

File metadata and controls

210 lines (121 loc) · 6.14 KB

next-test-api-route-handlerDocs


next-test-api-route-handler / NtarhInitAppRouter

Interface: NtarhInitAppRouter<NextResponseJsonType>

The parameters expected by testApiHandler when using appHandler.

Extends

Type Parameters

NextResponseJsonType = unknown

Properties

appHandler

appHandler: Partial<Omit<AppRouteUserlandModule, "GET" | "HEAD" | "OPTIONS" | "POST" | "PUT" | "DELETE" | "PATCH"> & object>

The actual App Router route handler under test. It should be an object containing one or more async functions named for valid HTTP methods and/or a valid configuration option. See the Next.js documentation for details.

Defined in

index.ts:163


pagesHandler?

optional pagesHandler: undefined

Defined in

index.ts:174


params?

optional params: Record<string, string | string[]>

params is passed directly to the handler and represents processed dynamic routes. This should not be confused with query string parsing, which is handled by Request automatically.

params: { id: 'some-id' } is shorthand for paramsPatcher: (params) => { params.id = 'some-id' }. This is useful for quickly setting many params at once.

Defined in

index.ts:184


paramsPatcher()?

optional paramsPatcher: (params) => Promisable<void | Record<string, string | string[]>>

A function that receives params, an object representing "processed" dynamic route parameters. Modifications to params are passed directly to the handler. You can also return a custom object from this function which will replace params entirely.

Parameter patching should not be confused with query string parsing, which is handled by Request automatically.

Parameters

params: Record<string, string | string[]>

Returns

Promisable<void | Record<string, string | string[]>>

Defined in

index.ts:194


rejectOnHandlerError?

optional rejectOnHandlerError: boolean

If false, errors thrown from within a handler are kicked up to Next.js's resolver to deal with, which is what would happen in production. If true, the testApiHandler function will reject immediately instead.

You should use rejectOnHandlerError whenever you want to manually handle an error that bubbles up from your handler (which is especially true if you're using expect within your handler) or when you notice a false negative despite exceptions being thrown.

Default

false

Inherited from

NtarhInit.rejectOnHandlerError

Defined in

index.ts:137


requestPatcher()?

optional requestPatcher: (request) => Promisable<void | Request>

A function that receives a NextRequest object and returns a Request instance. Use this function to edit the request before it's injected into the handler.

If the returned Request instance is not also an instance of NextRequest, it will be wrapped with NextRequest, e.g. new NextRequest(returnedRequest, { ... }).

Parameters

request: NextRequest

Returns

Promisable<void | Request>

Defined in

index.ts:207


responsePatcher()?

optional responsePatcher: (res) => Promisable<void | Response>

A function that receives the Response object returned from appHandler and returns a Response instance. Use this function to edit the response after your handler runs but before it's processed by the server.

Note that responsePatcher is called even in the case of exceptions, including unhandled exceptions (for which Next.js returns an HTTP 500 response). The only time responsePatcher is not called is when an unhandled exception occurs and rejectOnHandlerError is true.

Parameters

res: Response

Returns

Promisable<void | Response>

Defined in

index.ts:220


test()

test: (parameters) => Promisable<void>

test is a function that runs your test assertions. This function receives one destructured parameter: fetch, which is equivalent to globalThis.fetch but with the first parameter omitted.

Parameters

parameters

parameters.fetch

Returns

Promisable<void>

Inherited from

NtarhInit.test

Defined in

index.ts:143


url?

optional url: string

url: 'your-url' is shorthand for requestPatcher: (request) => new NextRequest('your-url', request)

Defined in

index.ts:225