Skip to content
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

Not compatible with the Next.js "app" router #295

Open
scottned opened this issue Feb 20, 2024 · 2 comments
Open

Not compatible with the Next.js "app" router #295

scottned opened this issue Feb 20, 2024 · 2 comments

Comments

@scottned
Copy link

It seems that this package is not compatible with Next's newer "app" router. The older "page" router works fine because its NextApiRequest extends IncomingMessage as your package requires. However, the "app" router's NextRequest does not -- it extends the standard Request interface of the fetch API.

Is this a known issue? Am I missing something obvious? It took awhile today to sort all this out. If it's correct, it would be helpful to others to have a note on the README.md.

By the way, I think you have a typo in your README.md that refers to http.IncomingRequest, but it should say http.IncomingMessage

@DaneSharafinski
Copy link

I'm not sure what we're getting out of mocking the request since we're just passing it into the handler anyways. Might as well just create the request object. This worked for me (just creating the NextRequest object, not mocking anything)

import { POST } from '@/src/app/api/feedback/route';
import { NextRequest } from 'next/server';

describe('SubmitFeedback', () => {
  it('should create a feedback entry', async () => {
    const body : {} = {
      feedback: 'Love your site!'
    }

    const headers = {
      'Content-Type': 'application/json',
      'accept-language': 'en-US'
    }

    const params: RequestInit = {
      headers: headers,
      method: "POST",
      body: JSON.stringify(body)
    }

    const req = new NextRequest('http://doesntmatter', params);

    const response = await POST(req);

    expect(response).toBeDefined();
  });
});

@Centorios
Copy link

I tried to circunvent this problem as @DaneSharafinski said, but i have a problems when uploading binary files, because the NextRequest constructor automatically tries to parse the body to uint8Array, also tried with the usual request constructor but to no avail.
I also tried manually testing the binary upload outside jest with postman, and works fine
maybe its a next14 thing?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants