Confidently test your Next.js API routes in an isolated Next-like environment
next-test-api-route-handler
Trying to unit test your Next.js API routes? Tired of hacking something
together with express or node-mocks-http or writing a bunch of boring dummy
infra just to get some passing tests? And what does a "passing test" mean anyway
when your handlers aren't receiving actual NextRequest objects and
aren't being run by Next.js itself?
Next.js patches the global fetch function, for instance. If your tests
aren't doing the same, you're making space for bugs!
Is it vexing that everything explodes when your App Router handlers call
headers() or cookies() or any of the other route-specific helper
functions? Or maybe you want your Pages Router handlers to receive
actual NextApiRequest and NextApiResponse objects?
Sound interesting? Then want no longer! 🤩
next-test-api-route-handler (NTARH) uses Next.js's
internal resolvers to precisely emulate route handling. To guarantee stability,
this package is automatically tested against each release of Next.js
and Node.js. Go forth and test confidently!
✨
✨
Note that App Router support begins with next@14.0.4 (why?)
Install
To install:
npm install --save-dev next-test-api-route-handler
See the appendix for legacy Next.js support options.
Also see the appendix if you're using jest (or vitest) with
jest-environment-jsdom / jsdom / jest-fixed-jsdom. Note that you
should not be using a dom-like environment to test your API handlers.
Usage
[!IMPORTANT]
NTARH must always be the first import in your test file. This is due to
the way Next.js is written and distributed. See the appendix for
technical details.
import { testApiHandler } from 'next-test-api-route-handler';
... all other imports ...
const { testApiHandler } = require('next-test-api-route-handler');
... all other imports ...
If you're using fancy import sorting like eslint-plugin-import's "order"
rule, import NTARH as a side-effect first, then
perform the real import later:
import 'next-test-api-route-handler';
... all other imports ordered before NTARH ...
import { testApiHandler } from 'next-test-api-route-handler';
... all other imports ordered after NTARH ...
Or, you can configure eslint-plugin-import to put the NTARH import above the
others (example).
Quick Start: App Router
import { testApiHandler } from 'next-test-api-route-handler';
import * as appHandler from '../app/your-endpoint/route';
it('does what I want', async () => {
await testApiHandler({
appHandler,
requestPatcher(request) {
request.headers.set('key', process.env.SPECIAL_TOKEN);
},
async responsePatcher(response) {
const json = await response.json();
return Response.json(
json.apiSuccess ? { hello: 'world!' } : { goodbye: 'cruel world' }
);
},
async test({ fetch }) {
const res = await fetch({ method: 'POST', body: 'dummy-data' });
await expect(res.json()).resolves.toStrictEqual({ hello: 'world!' });
}
});
});
Quick Start: Edge Runtime
import { testApiHandler } from 'next-test-api-route-handler';
import * as edgeHandler from '../app/your-edge-endpoint/route';
it('does what I want', async function () {
await testApiHandler<{ success: boolean }>({
appHandler: edgeHandler,
requestPatcher(request) {
return new Request(request, {
body: dummyReadableStream,
duplex: 'half'
});
},
async test({ fetch }) {
await expect((await fetch()).json()).resolves.toStrictEqual({
success: true
});
}
});
});
Quick Start: Pages Router
import { testApiHandler } from 'next-test-api-route-handler';
import * as pagesHandler from '../pages/api/your-endpoint';
it('does what I want', async () => {
await testApiHandler<{ hello: string }>({
pagesHandler,
requestPatcher: (req) => {
req.headers = { key: process.env.SPECIAL_TOKEN };
},
test: async ({ fetch }) => {
const res = await fetch({ method: 'POST', body: 'data' });
const { hello } = await res.json();
expect(hello).toBe('world');
}
});
});
API
NTARH exports a single function, testApiHandler(options), that accepts an
options object as its only parameter.
At minimum, options must contain the following properties:
- At least one of the
appHandler or pagesHandler options, but not both.
- The
test option.
For example:
[!CAUTION]
Ensuring testApiHandler is imported before any Next.js package (like
'next/headers' below) is crucial to the proper function of NTARH. Doing
otherwise will result in undefined behavior.
import { testApiHandler } from 'next-test-api-route-handler';
import { headers } from 'next/headers';
await testApiHandler({
appHandler: {
dynamic: 'force-dynamic',
async GET(_request) {
return Response.json(
{
hello: (await headers()).get('x-hello')
},
{ status: 200 }
);
}
},
async test({ fetch }) {
await expect(
(await fetch({ headers: { 'x-hello': 'world' } })).json()
).resolves.toStrictEqual({
hello: 'world'
});
}
});
appHandler
⪢ API reference: appHandler
The actual route handler under test (usually imported from app/*). It should
be an object and/or exported module containing one or more valid uppercase HTTP
method names as keys, each with an async handling function that
accepts a NextRequest and "segment data" (i.e. { params }) as its
two parameters. The object or module can also export other configuration
settings recognized by Next.js.
await testApiHandler({
params: { id: 5 },
appHandler: {
async POST(request, { params: { id } }) {
return Response.json(
{ special: request.headers.get('x-special-header'), id },
{ status: 200 }
);
}
},
async test({ fetch }) {
expect((await fetch({ method: 'POST' })).status).toBe(200);
const result2 = await fetch({
method: 'POST',
headers: { 'x-special-header': 'x' }
});
expect(result2.json()).toStrictEqual({ special: 'x', id: 5 });
}
});
See also: rejectOnHandlerError and the section Working Around Next.js
fetch Patching.
pagesHandler
⪢ API reference: pagesHandler
The actual route handler under test (usually imported from pages/api/*). It
should be an async function that accepts NextApiRequest and
NextApiResponse objects as its two parameters.
await testApiHandler({
params: { id: 5 },
pagesHandler: (req, res) => res.status(200).send({ id: req.query.id }),
test: async ({ fetch }) =>
expect((await fetch({ method: 'POST' })).json()).resolves.toStrictEqual({
id: 5
})
});
See also: rejectOnHandlerError.
test
⪢ API reference: test
An async or promise-returning function wherein test assertions can be run. This
function receives one destructured parameter: fetch, which is a wrapper around
Node's global fetch function. Use this to send HTTP requests to the
handler under test.
[!CAUTION]
Note that fetch's resource parameter, i.e. the first parameter in
fetch(...), is omitted.
⚙ Handling Redirections
Starting with version 4.0.4, NTARH sets the fetch(...) options
parameter's redirect property to 'manual' by default. This prevents
the WHATWG/undici fetch function from throwing a
fetch failed/redirect count exceeded error.
If you want to change this value, call fetch with your own custom options
parameter, e.g. fetch({ redirect: 'error' }).
⚙ Compatibility with Mock Service Worker
Starting with version 4.0.0, NTARH ships with Mock Service Worker (msw)
support by adding the x-msw-intention: "bypass" and
x-msw-bypass: "true" headers to all requests.
If necessary, you can override this behavior by setting the appropriate headers
to some other value (e.g. "none") via fetch's customInit parameter (not
requestPatcher). This comes in handy when testing functionality like
arbitrary response redirection (or via the Pages Router).
For example:
import { testApiHandler } from 'next-test-api-route-handler';
import { http, passthrough, HttpResponse } from 'msw';
import { setupServer } from 'msw/node';
const server = setupServer();
beforeAll(() => server.listen({ onUnhandledRequest: 'error' }));
afterEach(() => {
server.resetHandlers();
});
afterAll(() => server.close());
it('redirects a shortened URL to the real URL', async () => {
expect.hasAssertions();
const { shortId, realLink } = getUriEntry();
const realUrl = new URL(realLink);
await testApiHandler({
appHandler,
params: { shortId },
test: async ({ fetch }) => {
server.use(
http.get('*', async ({ request }) => {
return request.url === realUrl.href
? HttpResponse.json({ it: 'worked' }, { status: 200 })
: passthrough();
})
);
const res = await fetch({
headers: { 'x-msw-intention': 'none' }
});
await expect(res.json()).resolves.toMatchObject({ it: 'worked' });
expect(res.status).toBe(200);
}
});
});
⚙ response.cookies
As of version 2.3.0, the response object returned by fetch() includes a
non-standard cookies field containing an array of objects representing
set-cookie response header(s) parsed by the cookie package. Use
the cookies field to easily access a response's cookie data in your tests.
Here's an example taken straight from the unit tests:
import { testApiHandler } from 'next-test-api-route-handler';
it('handles multiple set-cookie headers', async () => {
expect.hasAssertions();
await testApiHandler({
pagesHandler: (_, res) => {
res
.setHeader('Set-Cookie', [
serializeCookieHeader('access_token', '1234', {
expires: new Date()
}),
serializeCookieHeader('REFRESH_TOKEN', '5678')
])
.status(200)
.send({});
},
test: async ({ fetch }) => {
expect((await fetch()).status).toBe(200);
await expect((await fetch()).json()).resolves.toStrictEqual({});
expect((await fetch()).cookies).toStrictEqual([
{
access_token: '1234',
expires: expect.any(String),
Expires: expect.any(String)
},
{ refresh_token: '5678', REFRESH_TOKEN: '5678' }
]);
}
});
});
rejectOnHandlerError
⪢ API reference: rejectOnHandlerError
As of version 2.3.0, unhandled errors in the pagesHandler/appHandler
function are kicked up to Next.js to handle.
[!IMPORTANT]
This means testApiHandler will NOT reject or throw if an unhandled error
occurs in pagesHandler/appHandler, which typically includes failing
expect() assertions.
Instead, the response returned by fetch() in your test function will have a
HTTP 500 status thanks to how Next.js deals with unhandled errors in
production. Prior to 2.3.0, NTARH's behavior on unhandled errors and
elsewhere was inconsistent. Version 3.0.0 further improved error handling,
ensuring no errors slip by uncaught.
To guard against false negatives, you can do either of the following:
- Make sure the status of the
fetch() response is what you're expecting:
const res = await fetch();
...
expect(res.status).toBe(403);
...
const res2 = await fetch();
...
expect(res2.status).toBe(500);
- If you're using version
>=3.0.0, you can use rejectOnHandlerError to tell
NTARH to intercept unhandled handler errors and reject the promise returned
by testApiHandler instead of relying on Next.js to respond with
HTTP 500. This is especially useful if you have expect() assertions
inside your handler function:
await expect(
testApiHandler({
rejectOnHandlerError: true,
pagesHandler: (_req, res) => {
res.status(200);
throw new Error('bad bad not good');
},
test: async ({ fetch }) => {
const res = await fetch();
}
})
).rejects.toThrow('bad not good');
await testApiHandler({
rejectOnHandlerError: true,
appHandler: {
async GET(request) {
await expect(backend.getSomeStuff(request)).resolves.toStrictEqual(
someStuff
);
return new Response(null, { status: 200 });
}
},
test: async ({ fetch }) => {
await fetch();
}
});
requestPatcher (url)
[!TIP]
Manually setting the request url is usually unnecessary. Only set the url if
your handler expects it or you want to rely on query string parsing
instead of params/paramsPatcher.
💎 Using appHandler
⪢ API reference: requestPatcher, url
requestPatcher is 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.
[!CAUTION]
Be wary returning a brand new request from requestPatcher (i.e.
new NextRequest(newUrl) instead of new NextRequest(newUrl, oldRequest)),
especially one that is missing standard headers added by fetch(...). If
you're getting strange JSON-related errors or hanging tests, ensure this is
not the cause.
The returned Request instance will be wrapped with NextRequest if
it is not already an instance of NextRequest, i.e.:
const returnedRequest = (await requestPatcher?.(request)) || request;
const nextRequest = new NextRequest(returnedRequest, { ... });
If you're only setting the request url, use the url shorthand instead:
await testApiHandler({
url: '/my-url?some=query'
});
[!NOTE]
Unlike the Pages Router's NextApiRequest type, the App Router's
NextRequest class does not support relative URLs. Therefore, whenever
you pass a relative url string via the url shorthand (e.g.
{ url: '/my-url?some=query' }), NTARH will wrap that url like so:
new URL(url, 'ntarh://'). In this case, your requests will have urls like
ntarh:///my-url?some=query.
URL Normalization
By default, when initializing the NextRequest object passed to your handler,
if a URL with an empty pathname is encountered, NTARH sets said URL's
pathname to "/" on your behalf. Additionally, if said URL is missing host
and/or protocol, NTARH sets host to "" and protocol to "ntarh:".
If you want your handler to receive the URL string and resulting
NextRequest::nextUrl object exactly as you've typed it, use requestPatcher,
which is executed after NTARH does URL normalization.
🔷 Using pagesHandler
⪢ API reference: requestPatcher, url
requestPatcher is a function that receives an IncomingMessage. Use
this function to modify the request before it's injected into Next.js's
resolver.
If you're only setting the request url, use the url shorthand instead:
await testApiHandler({
url: '/my-url?some=query'
});
Note that, unlike with the URL class, the url string can be relative.
responsePatcher
💎 Using appHandler
⪢ API reference: responsePatcher
responsePatcher is 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.
🔷 Using pagesHandler
⪢ API reference: responsePatcher
responsePatcher is a function that receives a ServerResponse object.
Use this function to edit the response before it's injected into the handler.
paramsPatcher (params)
paramsPatcher is a function that receives an object representing "processed"
dynamic segments (aka: routes, slugs).
For example, to test a handler normally accessible from /api/user/:id requires
passing that handler a value for the "id" dynamic segment:
await testApiHandler({
paramsPatcher(params) {
params.id = 'test-id';
}
});
Or:
await testApiHandler({
paramsPatcher: (params) => ({ id: 'test-id' })
});
Parameters can also be passed using the params shorthand:
await testApiHandler({
params: {
id: 'test-id'
}
});
[!TIP]
Due to its simplicity, favor the params shorthand over paramsPatcher.
💎 Using appHandler
⪢ API reference: paramsPatcher, params
[!IMPORTANT]
Note that, starting with next@15, the params object passed to handlers via
the context parameter is now a (Frankensteinian) promise. This means
tests like expect(params).toStrictEqual(...) will no longer work unless
params is first await-ed. More information here.
If both paramsPatcher and the params shorthand are used, paramsPatcher
will receive params as its first argument.
Route parameters should not be confused with query string parameters,
which are automatically parsed out from the url and made available via the
NextRequest argument passed to your handler.
🔷 Using pagesHandler
⪢ API reference: paramsPatcher, params
If both paramsPatcher and the params shorthand are used, paramsPatcher
will receive an object like { ...queryStringURLParams, ...params } as its
first argument.
Route parameters should not be confused with query string parameters,
which are automatically parsed out from the url and added to the params
object before paramsPatcher is evaluated.
Examples
What follows are several examples that demonstrate using NTARH with the App
Router and the Pages Router.
Check out the tests for even more examples.
Using the App Router
These examples use Next.js's App Router API.
Testing Apollo's Official Next.js Integration @ app/api/graphql
This example is based on the official Apollo Next.js App Router
integration. You can run it yourself by copying and pasting the following
commands into your terminal.
The following should be run in a nix-like environment. On Windows, that's
WSL. Requires curl, node, and git.
mkdir -p /tmp/ntarh-test/test
cd /tmp/ntarh-test
npm install --force next @apollo/server @as-integrations/next graphql-tag next-test-api-route-handler jest babel-jest @babel/core @babel/preset-env
echo 'module.exports={presets:["@babel/preset-env"]};' > babel.config.js
mkdir -p app/api/graphql
curl -o app/api/graphql/route.js https://raw.githubusercontent.com/Xunnamius/next-test-api-route-handler/main/apollo_test_raw_app_route
curl -o test/integration.test.js https://raw.githubusercontent.com/Xunnamius/next-test-api-route-handler/main/apollo_test_raw_app_test
npx jest
This script creates a new temporary directory, installs NTARH and configures
dependencies, downloads the app route and jest test files shown
below, and runs the test using jest.
The following is our new app route:
import { ApolloServer } from '@apollo/server';
import { startServerAndCreateNextHandler } from '@as-integrations/next';
import { gql } from 'graphql-tag';
const resolvers = {
Query: {
hello: () => 'world'
}
};
const typeDefs = gql`
type Query {
hello: String
}
`;
const server = new ApolloServer({
resolvers,
typeDefs
});
const handler = startServerAndCreateNextHandler(server);
export { handler as GET, handler as POST };
And with the following jest test, we ensure our route integrates with Apollo
correctly:
import { testApiHandler } from 'next-test-api-route-handler';
import * as appHandler from '../app/api/graphql/route';
describe('my-test (app router)', () => {
it('does what I want 1', async () => {
expect.hasAssertions();
await testApiHandler({
appHandler,
test: async ({ fetch }) => {
const query = `query { hello }`;
const res = await fetch({
method: 'POST',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify({ query })
});
await expect(res.json()).resolves.toStrictEqual({
data: { hello: 'world' }
});
}
});
});
it('does what I want 2', async () => {
});
it('does what I want 3', async () => {
});
});
Testing Clerk's Official Next.js Integration @ app/api/authed
Suppose we created a new authenticated API endpoint at app/api/authed by
cloning the Clerk App Router demo repo and following Clerk's quick-start
guide for Next.js:
import { auth } from '@clerk/nextjs';
export async function GET() {
const { userId } = auth();
return Response.json({ isAuthed: !!userId, userId });
}
How might we test that this endpoint functions as we expect?
import { testApiHandler } from 'next-test-api-route-handler';
import * as appHandler from '../app/api/authed/route';
import type { auth } from '@clerk/nextjs';
let mockedClerkAuthReturnValue: Partial<ReturnType<typeof auth>> | undefined =
undefined;
jest.mock('@clerk/nextjs', () => {
return {
auth() {
return mockedClerkAuthReturnValue;
}
};
});
afterEach(() => {
mockedClerkAuthReturnValue = undefined;
});
it('returns isAuthed: true and a userId when authenticated', async () => {
expect.hasAssertions();
mockedClerkAuthReturnValue = { userId: 'winning' };
await testApiHandler({
appHandler,
test: async ({ fetch }) => {
await expect((await fetch()).json()).resolves.toStrictEqual({
isAuthed: true,
userId: 'winning'
});
}
});
});
it('returns isAuthed: false and nothing else when unauthenticated', async () => {
expect.hasAssertions();
mockedClerkAuthReturnValue = { userId: null };
await testApiHandler({
appHandler,
test: async ({ fetch }) => {
await expect((await fetch()).json()).resolves.toStrictEqual({
isAuthed: false,
userId: null
});
}
});
});
If you're feeling more adventurous, you can transform this unit test into an
integration test (like the Apollo example above) by calling Clerk's
authMiddleware function in appHandler instead of mocking
@clerk/nextjs:
import { default as middleware } from '../middleware';
import type { NextRequest } from 'next/server';
const DUMMY_CLERK_USER_ID = 'user_2aqlGWnjdTRRbbBk9OdBHHbniyK';
it('returns isAuthed: true and a userId when authenticated', async () => {
expect.hasAssertions();
await testApiHandler({
rejectOnHandlerError: true,
url: 'ntarh://app/api/authed',
appHandler: {
get GET() {
return async function (...args: Parameters<typeof appHandler.GET>) {
const request = args.at(0) as unknown as NextRequest;
const middlewareResponse = await middleware(request, {
});
expect(middlewareResponse.headers.get('location')).toBeNull();
expect(middlewareResponse.ok).toBe(true);
const handlerResponse = await appHandler.GET(...args);
return handlerResponse;
};
}
},
test: async ({ fetch }) => {
await expect((await fetch()).json()).resolves.toStrictEqual({
isAuthed: true,
userId: DUMMY_CLERK_USER_ID
});
}
});
});
You can also try calling authMiddleware in requestPatcher; however,
Clerk's middleware does its magic by importing the headers helper function
from 'next/headers', and only functions invoked within appHandler have
access to the storage context that allows Next.js's helper functions to work.
For insight into what you'd need to do to make authMiddleware callable
in requestPatcher, check out Clerk's own tests.
Testing an Unreliable Handler on the Edge @ app/api/unreliable
Suppose we have an API endpoint we use to test our application's error handling.
The endpoint responds with status code HTTP 200 for every request except the
10th, where status code HTTP 555 is returned instead.
How might we test that this endpoint responds with HTTP 555 once for every
nine HTTP 200 responses?
import { testApiHandler } from 'next-test-api-route-handler';
import * as edgeHandler from '../app/api/unreliable';
const expectedReqPerError = 10;
it('injects contrived errors at the required rate', async () => {
expect.hasAssertions();
process.env.REQUESTS_PER_CONTRIVED_ERROR = expectedReqPerError.toString();
await testApiHandler({
appHandler: edgeHandler,
requestPatcher(request) {
return new NextRequest(request, {
geo: { city: 'Chicago', country: 'United States' },
ip: '110.10.77.7'
});
},
test: async ({ fetch }) => {
const results1 = await Promise.all(
[
...Array.from({ length: expectedReqPerError - 1 }).map(() =>
fetch({ method: 'GET' })
),
fetch({ method: 'POST' }),
...Array.from({ length: expectedReqPerError - 1 }).map(() =>
fetch({ method: 'PUT' })
),
fetch({ method: 'DELETE' })
].map((p) => p.then((r) => r.status))
);
process.env.REQUESTS_PER_CONTRIVED_ERROR = '0';
const results2 = await Promise.all(
Array.from({ length: expectedReqPerError }).map(() =>
fetch().then((r) => r.status)
)
);
expect(results1).toIncludeSameMembers([
...Array.from({ length: expectedReqPerError - 1 }).map(() => 200),
555,
...Array.from({ length: expectedReqPerError - 1 }).map(() => 200),
555
]);
expect(results2).toStrictEqual([
...Array.from({ length: expectedReqPerError }).map(() => 200)
]);
}
});
});
Using the Pages Router
These examples use Next.js's Pages Router API.
Testing Next.js's Official Apollo Example @ pages/api/graphql
This example uses the official Next.js Apollo demo. You can easily run it
yourself by copying and pasting the following commands into your terminal.
The following should be run in a nix-like environment. On Windows, that's
WSL. Requires curl, node, and git.
git clone --depth=1 https://github.com/vercel/next.js /tmp/ntarh-test
cd /tmp/ntarh-test/examples/api-routes-apollo-server-and-client
npm install --force
npm install --force next-test-api-route-handler jest babel-jest @babel/core @babel/preset-env
echo 'module.exports={presets:["@babel/preset-env"]};' > babel.config.js
mkdir test
curl -o test/integration.test.js https://raw.githubusercontent.com/Xunnamius/next-test-api-route-handler/main/apollo_test_raw
npx jest
This script clones the Next.js repository, installs NTARH and configures
dependencies, downloads the jest test file shown below, and runs it using
jest to ensure our route integrates with Apollo correctly.
[!IMPORTANT]
Note that passing the route configuration object (imported below as
config) through to NTARH and setting request.url to the proper value may
be necessary when testing Apollo endpoints using the Pages Router.
import { testApiHandler } from 'next-test-api-route-handler';
import * as pagesHandler from '../pages/api/graphql';
describe('my-test (pages router)', () => {
it('does what I want 1', async () => {
expect.hasAssertions();
await testApiHandler({
pagesHandler,
url: '/api/graphql',
test: async ({ fetch }) => {
const query = `query ViewerQuery {
viewer {
id
name
status
}
}`;
const res = await fetch({
method: 'POST',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify({ query })
});
await expect(res.json()).resolves.toStrictEqual({
data: { viewer: { id: '1', name: 'John Smith', status: 'cached' } }
});
}
});
});
it('does what I want 2', async () => {
});
it('does what I want 3', async () => {
});
});
Testing an Authenticated Flight Search Handler @ pages/api/v3/flights/search
Suppose we have an authenticated API endpoint our application uses to search
for flights. The endpoint responds with an array of flights satisfying the
query.
How might we test that this endpoint returns flights in our database as
expected?
import { testApiHandler } from 'next-test-api-route-handler';
import { DUMMY_API_KEY as KEY, getFlightData, RESULT_SIZE } from '../backend';
import * as pagesHandler from '../pages/api/v3/flights/search';
import type { PageConfig } from 'next';
it('returns expected public flights with respect to match', async () => {
expect.hasAssertions();
const expectedFlights = getFlightData();
const encode = (o: Record<string, unknown>) =>
encodeURIComponent(JSON.stringify(o));
const genUrl = (function* () {
yield `/?match=${encode({ airline: 'Spirit' })}`;
yield `/?match=${encode({ type: 'departure' })}`;
yield `/?match=${encode({ landingAt: 'F1A' })}`;
yield `/?match=${encode({ seatPrice: 500 })}`;
yield `/?match=${encode({ seatPrice: { $gt: 500 } })}`;
yield `/?match=${encode({ seatPrice: { $gte: 500 } })}`;
yield `/?match=${encode({ seatPrice: { $lt: 500 } })}`;
yield `/?match=${encode({ seatPrice: { $lte: 500 } })}`;
})();
await testApiHandler({
requestPatcher: (req) => {
req.url = genUrl.next().value || undefined;
},
pagesHandler,
test: async ({ fetch }) => {
const responses = await Promise.all(
Array.from({ length: 8 }).map(() =>
fetch({ headers: { KEY } }).then(async (r) => [
r.status,
await r.json()
])
)
);
expect(responses.some(([status]) => status !== 200)).toBe(false);
expect(responses.map(([, r]) => r.flights)).toIncludeSameMembers([
expectedFlights
.filter((f) => f.airline === 'Spirit')
.slice(0, RESULT_SIZE),
expectedFlights
.filter((f) => f.type === 'departure')
.slice(0, RESULT_SIZE),
expectedFlights
.filter((f) => f.landingAt === 'F1A')
.slice(0, RESULT_SIZE),
expectedFlights
.filter((f) => f.seatPrice === 500)
.slice(0, RESULT_SIZE),
expectedFlights.filter((f) => f.seatPrice > 500).slice(0, RESULT_SIZE),
expectedFlights.filter((f) => f.seatPrice >= 500).slice(0, RESULT_SIZE),
expectedFlights.filter((f) => f.seatPrice < 500).slice(0, RESULT_SIZE),
expectedFlights.filter((f) => f.seatPrice <= 500).slice(0, RESULT_SIZE)
]);
}
});
await testApiHandler({
pagesHandler,
url: `/?match=${encode({ ffms: { $eq: 500 } })}`,
test: async ({ fetch }) =>
expect((await fetch({ headers: { KEY } })).status).toBe(400)
});
await testApiHandler({
pagesHandler,
url: `/?match=${encode({ bad: 500 })}`,
test: async ({ fetch }) =>
expect((await fetch({ headers: { KEY } })).status).toBe(400)
});
});
Testing an Unreliable Handler @ pages/api/unreliable
Suppose we have an API endpoint we use to test our application's error handling.
The endpoint responds with status code HTTP 200 for every request except the
10th, where status code HTTP 555 is returned instead.
How might we test that this endpoint responds with HTTP 555 once for every
nine HTTP 200 responses?
import { testApiHandler } from 'next-test-api-route-handler';
import * as pagesHandler from '../pages/api/unreliable';
const expectedReqPerError = 10;
it('injects contrived errors at the required rate', async () => {
expect.hasAssertions();
process.env.REQUESTS_PER_CONTRIVED_ERROR = expectedReqPerError.toString();
await testApiHandler({
pagesHandler,
test: async ({ fetch }) => {
const results1 = await Promise.all(
[
...Array.from({ length: expectedReqPerError - 1 }).map(() =>
fetch({ method: 'GET' })
),
fetch({ method: 'POST' }),
...Array.from({ length: expectedReqPerError - 1 }).map(() =>
fetch({ method: 'PUT' })
),
fetch({ method: 'DELETE' })
].map((p) => p.then((r) => r.status))
);
process.env.REQUESTS_PER_CONTRIVED_ERROR = '0';
const results2 = await Promise.all(
Array.from({ length: expectedReqPerError }).map(() =>
fetch().then((r) => r.status)
)
);
expect(results1).toIncludeSameMembers([
...Array.from({ length: expectedReqPerError - 1 }).map(() => 200),
555,
...Array.from({ length: expectedReqPerError - 1 }).map(() => 200),
555
]);
expect(results2).toStrictEqual([
...Array.from({ length: expectedReqPerError }).map(() => 200)
]);
}
});
});
Appendix
Further documentation can be found under docs/.
Limitations with App Router and Edge Runtime Emulation
Since NTARH is meant for unit testing API routes rather than faithfully
recreating Next.js functionality, NTARH's feature set comes with some caveats.
Namely: no Next.js features will be available that are external to processing
API routes and executing their handlers. This includes middleware and
NextResponse.next (see requestPatcher if you need to mutate the
Request before it gets to the handler under test), metadata, static
assets, OpenTelemetry and instrumentation, caching,
styling, server actions and mutations, helper functions
(except: cookies, fetch (global), headers, NextRequest, NextResponse,
notFound, permanentRedirect, redirect, and userAgent), and anything
related to React or components.
NTARH is for testing your API route handlers only.
Further, any support NTARH appears to have for any "edge runtime" (or any
other runtime) beyond what is provided by AppRouteRouteModule is merely
cosmetic. Your tests will always run in Node.js (or your runner of choice)
and never in a different runtime, realm, or VM. This means unit testing like
with NTARH must be done in addition to, and not in lieu of, more holistic
testing practices (e.g. end-to-end).
If you're having trouble with your App Router and/or Edge Runtime routes,
consider opening a new issue!
Also note that Next.js's middleware only supports the Edge runtime, even
if the Next.js application is being run entirely by Node.js. This is an
artificial constraint imposed by Next.js; when running the middleware locally
(via npm run dev or something similar), the middleware will still run on
Node.js.
Next.js's middleware limitation is discussed at length here.
Working around the App Router Patching the Global fetch Function
Next.js's current App Router implementation mutates the global fetch function,
redefining it entirely. This can cause problems in testing environments
where the global fetch is to be mocked by something else.
Internally, NTARH sidesteps this issue entirely by caching the value of
globalThis.fetch upon import. This also means NTARH completely sidesteps other
tools that rely on interception through rewriting the global fetch function,
such as Mock Service Worker (MSW). We still include the MSW bypass headers
with NTARH requests since we cannot guarantee that NTARH will not be imported
after MSW has finished patching global fetch.
Similarly, it is impossible for NTARH to meaningfully track mutations to the
global fetch function; NTARH cannot tell the difference between Next.js
overwriting fetch and, say, a Jest spy overwriting fetch. Therefore, NTARH
does not restore the cached fetch after the function returns.
If Next.js's patching of fetch is causing trouble for you, you can do what
NTARH does: capture the current global fetch (perhaps after setting up MSW)
and then restore it after each test:
const originalGlobalFetch = fetch;
afterEach(function () {
globalThis.fetch = originalGlobalFetch;
});
Working around Global AsyncLocalStorage Availability
AppRouteRouteModule and its dependents want AsyncLocalStorage to be
available globally and immediately. Unfortunately, Node.js does not place
AsyncLocalStorage in globalThis natively.
NTARH handles this by ensuring AsyncLocalStorage is added to globalThis
before Next.js needs it. This is why NTARH should always be the very first
import in any test file.
Legacy Runtime Support
As of version 4.0.0, NTARH supports both the App Router (for next@>=14.0.4)
and the "legacy" Pages Router Next.js APIs. However, due to the code churn with
next@13, NTARH's support for the App Router begins at next@14.0.4. See
here and here for more information.
Additionally, as of version 2.1.0, NTARH's Pages Router support is fully
backwards compatible with Next.js going allll the way back to next@9.0.0
when API routes were first introduced!
[!NOTE]
Due to an import failure in Next.js's vendored dependencies, node@>=25 is
incompatible with next@^12.
If you're working with the Pages Router and next@<9.0.6 (so: before
next-server was merged into next), you might need to install
next-server manually:
npm install --save-dev next-server
Similarly, if you are using npm@<7 or node@<15, you must install Next.js
and its peer dependencies manually. This is because npm@<7 does not install
peer dependencies by default.
npm install --save-dev next@latest react
If you're also using an older version of Next.js, ensure you install the peer
dependencies (like react) that your specific Next.js version requires!
Jsdom Support
Note that jsdom does not support global fetch natively. This should not be
an issue, however, since neither your API code nor your API test code should be
executed in a browser-like environment.
For projects configured to use jsdom by default, you can use an annotation to
switch environments only in the files housing your API tests:
import { testApiHandler } from 'next-test-api-route-handler';
test('use the node test environment for all tests in this file', () => {
});
If you're seeing strange behavior or weird errors like
TypeError: RequestInit: Expected signal ("CustomAbortControllerSignal [EventTarget]") to be an instance of AbortSignal,
this is because you're using jsdom or something else that messes with node's
globals. The fix is to not use jsdom to test your API
routes since they'll never* run in a DOM-ready environment anyway.
[!NOTE]
If you're dead set on using jsdom with NTARH instead of a node or node-like
testing environment, see here and here for workarounds.
Inspiration
I'm constantly creating things with Next.js. Most of these applications have a
major API component. Unfortunately, Next.js doesn't make unit testing your APIs
very easy. After a while, I noticed some conventions forming around how I liked
to test my APIs and NTARH was born 🙂
Of course, this all was back before the app router or edge routes existed. NTARH
got app router and edge route support in version 4.
My hope is that NTARH gets obsoleted because Vercel provided developers with
some officially supported tooling/hooks for lightweight route execution where
handlers are passed fully initialized instances of
NextRequest/NextResponse/NextApiRequest/NextApiResponse without
ballooning the execution time of the tests. That is: no spinning up the entire
Next.js runtime just to run a single test in isolation.
It doesn't seem like it'd be such a lift to surface a wrapped version of the
Pages Router's apiResolver function and a pared-down subclass of the
App Router's AppRouteRouteModule, both accessible with something like
import { ... } from 'next/test'. This is essentially what NTARH does.
History: The Very Very First Version of Ntarh
Was looking over some ancient Next.js projects and found some of the very first
versions of what would eventually become NTARH. My inner code hoarder requires I
note this code's existence somewhere.
Oh how far we've come 🙂

Published Package Details
This is a CJS2 package with statically-analyzable exports
built by Babel for use in Node.js versions that are not end-of-life. For
TypeScript users, this package supports both "Node10" and "Node16" module
resolution strategies.
Expand details
That means both CJS2 (via require(...)) and ESM (via import { ... } from ...
or await import(...)) source will load this package from the same entry points
when using Node. This has several benefits, the foremost being: less code
shipped/smaller package size, avoiding dual package
hazard entirely, distributables are not
packed/bundled/uglified, a drastically less complex build process, and CJS
consumers aren't shafted.
Each entry point (i.e. ENTRY) in package.json's
exports[ENTRY] object includes one or more export
conditions. These entries may or may not include: an
exports[ENTRY].types condition pointing to a type
declaration file for TypeScript and IDEs, a
exports[ENTRY].module condition pointing to
(usually ESM) source for Webpack/Rollup, a exports[ENTRY].node and/or
exports[ENTRY].default condition pointing to (usually CJS2) source for Node.js
require/import and for browsers and other environments, and other
conditions not enumerated here. Check the
package.json file to see which export conditions are
supported.
Note that, regardless of the { "type": "..." } specified in
package.json, any JavaScript files written in ESM
syntax (including distributables) will always have the .mjs extension. Note
also that package.json may include the
sideEffects key, which is almost always false for
optimal tree shaking where appropriate.
License
See LICENSE.
Contributing and Support
New issues and pull requests
are always welcome and greatly appreciated! 🤩 Just as well, you can star 🌟
this project to let me know you found it useful! ✊🏿 Or buy
me a beer, I'd appreciate it. Thank you!
See CONTRIBUTING.md and SUPPORT.md for
more information.
Contributors

Thanks goes to these wonderful people (emoji
key):
This project follows the all-contributors
specification. Contributions of any kind welcome!