next-test-api-route-handler / src / NtarhInitPagesRouter
Defined in: src/index.ts:238
The parameters expected by testApiHandler
when using pagesHandler
.
NtarhInit
<NextResponseJsonType
>
NextResponseJsonType
= unknown
optional
appHandler:undefined
Defined in: src/index.ts:250
pagesHandler:
NextApiHandler
| {default
:NextApiHandler
; }
Defined in: src/index.ts:249
The actual Pages Router route handler under test. It should be an async
function that accepts NextApiRequest
and NextApiResult
objects (in
that order) as its two parameters.
Note that type checking for res.send
and similar methods was retired in
NTARH@4. Only the response.json
method returned by NTARH's fetch wrapper
will have a typed result.
optional
params:Record
<string
,unknown
>
Defined in: src/index.ts:260
params
is passed directly to the handler and represents processed dynamic
routes. This should not be confused with query string parsing, which is
handled automatically.
params: { id: 'some-id' }
is shorthand for paramsPatcher: (params) => { params.id = 'some-id' }
. This is useful for quickly setting many params at
once.
optional
paramsPatcher: (params
) =>Promisable
<void
|Record
<string
,unknown
>>
Defined in: src/index.ts:270
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 automatically.
Record
<string
, unknown
>
Promisable
<void
| Record
<string
, unknown
>>
optional
rejectOnHandlerError:boolean
Defined in: src/index.ts:145
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.
false
NtarhInit
.rejectOnHandlerError
optional
requestPatcher: (request
) =>Promisable
<void
>
Defined in: src/index.ts:280
A function that receives an IncomingMessage
object. Use this function
to edit the request before it's injected into the handler.
Note: all replacement IncomingMessage.header
names must be
lowercase.
IncomingMessage
Promisable
<void
>
optional
responsePatcher: (res
) =>Promisable
<void
>
Defined in: src/index.ts:285
A function that receives a ServerResponse
object. Use this function
to edit the response before it's injected into the handler.
ServerResponse
Promisable
<void
>
test: (
parameters
) =>Promisable
<void
>
Defined in: src/index.ts:151
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.
(customInit
?) => FetchReturnType
<NextResponseJsonType
>
Promisable
<void
>
optional
url:string
Defined in: src/index.ts:290
url: 'your-url'
is shorthand for requestPatcher: (req) => { req.url = 'your-url' }