Testing for API in NextJS

import { createMocks, RequestMethod } from 'node-mocks-http';
import type { NextApiRequest, NextApiResponse } from 'next';


import apiGetEnv from '../pages/api/get-env';


describe("test api", () => {
 function mockRequestResponse(method: RequestMethod = 'GET', body: any = {}) {
   const { req, res }: { req: NextApiRequest; res: any } = createMocks({ method, body });
   req.headers = {
     'Content-Type': 'application/json',
   };
   return { req, res };
 }




 it('should return a 400 if Gateway ID is missing', async () => {
   const { req, res } = mockRequestResponse();
   req.query = {}; // Equivalent to a null gateway ID


   await apiGetEnv(req, res);


   expect(res.statusCode).toBe(200);
   expect(res._getJSONData()).toEqual({
     "env": "test",
   });
 });
})