Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch all segments dynamic api routes #988

Closed
rinvii opened this issue Feb 12, 2024 · 1 comment
Closed

Catch all segments dynamic api routes #988

rinvii opened this issue Feb 12, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@rinvii
Copy link

rinvii commented Feb 12, 2024

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 :)

@rinvii rinvii added the enhancement New feature or request label Feb 12, 2024
@Xunnamius
Copy link
Owner

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):

/* 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');
    }
  });
});

Repository owner locked and limited conversation to collaborators Feb 12, 2024
@Xunnamius Xunnamius converted this issue into discussion #989 Feb 12, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants