Next.js Discord

Discord Forum

How do I retrieve the http server instance from NextJs?

Unanswered
Large garden bumble bee posted this in #help-forum
Open in Discord
Avatar
Large garden bumble beeOP
I want to test APIs / endpoints using things like supertest, which requires a http server instance. Has anyone done this?

9 Replies

Avatar
can you elaborate on http server instance?
I visited the docs and it shows that you only need the URL
Image
Avatar
Large garden bumble beeOP
URL works but it means my server is running in a separate process and i can't mock external calls
Avatar
external calls are.. naturally in a separate process? wouldnt having it in a separate process mocks the scenario of external calls?
Avatar
Large garden bumble beeOP
need to pass in the server instance like for example express()

but not sure for nextjs how it works
for example

i run a server in 1 terminal

then i run the tests in another

the tests make a call to the server's api lets say /getdata

that /getdata endpoint has logic that calls some other service somewhere unrelated to my app (external-api.com/getOtherData)

i dont want it to call that endpoint, i want to mock that response
Avatar
ahh
Avatar
Large garden bumble beeOP
if i could load and mount the http server instance and run the supertest tests in the same process then i could mock those
heres an example in strapi (https://strapi.io/blog/automated-testing-for-strapi-api-with-jest-and-supertest)

this doesnt use nextjs but i'm trying to do this on the nextjs application