-
I'm not seeing any documentation related to catch all api routes. Is there support for this? Thank you so much for this amazing library :) |
Beta Was this translation helpful? Give feedback.
Answered by
Xunnamius
Feb 12, 2024
Replies: 1 comment
-
Hi there! NTARH has support for all API route types. Testing catch-all routes shouldn't be too different from any other route, you just have to provide the expected Using the Next.js docs as an example (given URL /* file: pages/api/post/[...slug].ts */
import type { NextApiRequest, NextApiResponse } from 'next'
export default function handler(req: NextApiRequest, res: NextApiResponse) {
const { slug } = req.query
res.end(`Post: ${slug.join(', ')}`)
} /* file: test/main.test.ts */
import { testApiHandler } from 'next-test-api-route-handler';
// Import the handler under test from the pages/api directory
import * as pagesHandler from '../pages/api/[...slug]';
it('does what I want', async () => {
expect.hasAssertions();
await testApiHandler({
pagesHandler,
params: { slug: ['a', 'b', 'c'] },
test: async ({ fetch }) => {
const res = await fetch({ method: 'POST' });
await expect(res.text()).resolves.toBe('Post: a, b, c');
}
});
}); |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
Xunnamius
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi there! NTARH has support for all API route types. Testing catch-all routes shouldn't be too different from any other route, you just have to provide the expected
params
object.Using the Next.js docs as an example (given URL
/api/post/a/b/c
):