Skip to content

Mocked Request throwing error when converted to json #1942

Closed

Description

@LachlanNewman

Im currently tring to test a got request using jest using nock to intercept requests.

import { genAlphaNumeric } from '@annaliseai/api-specifications/build/core/Gen';
import express from 'express';
import { constants as httpConstants } from 'http2';
import Request from 'supertest';
import nock from 'nock';
import { flatMap, forEach } from 'lodash';
import { genGetInfoRes } from '@annaliseai/api-specifications';
import { ProxyConfig } from '../../utils/Config';
import { InfoController } from './InfoController';
import { name, version } from '../../../package.json';
import { ANNALISE_CXR_VERSION } from '../../utils/AnnaliseCxrVersion';

jest.setTimeout(30000);

describe('🧪 InfoController', () => {
  beforeEach(() => expect.hasAssertions());

  describe('GET /', () => {
    test('should be able to handle successful info request', async () => {
      const serviceVersionMapping = {
        groot: { res: genGetInfoRes(), url: `http://www.${genAlphaNumeric()}.com` },
        rocket: { res: genGetInfoRes(), url: `http://www.${genAlphaNumeric()}.com` },
        ultron: { res: genGetInfoRes(), url: `http://www.${genAlphaNumeric()}.com` },
        heimdall: { res: genGetInfoRes(), url: `http://www.${genAlphaNumeric()}.com` },
      };

      const proxyConfig: ProxyConfig = {
        grootHost: serviceVersionMapping.groot.url,
        rocketHost: serviceVersionMapping.rocket.url,
        ultronHost: serviceVersionMapping.ultron.url,
        heimdallHost: serviceVersionMapping.heimdall.url,
      };

      const expected = {
        versions: [
          { name: 'annaliseCxrVersion', version: ANNALISE_CXR_VERSION },
          { name, version },
          ...flatMap(serviceVersionMapping, ({ res }) => res.versions),
        ],
      };

      forEach(serviceVersionMapping, ({ url, res }) => {
        nock(url).get('/info').reply(httpConstants.HTTP_STATUS_OK, res);
      });

      const controller = InfoController(proxyConfig);
      const app = express();
      app.use(express.json());
      app.use(controller);

      const result = await Request(app).get('/').expect(httpConstants.HTTP_STATUS_OK);
      expect(result.body.versions).toHaveLength(expected.versions.length);
    });
  });
});

this is the got request

const getVersion = async (baseUrl: string): Promise<GetInfoRes> => {
  return new Promise<GetInfoRes>((resolve, reject) => {
    try{
    got.get(`${baseUrl}/info`).json()
      .then(res => {
        resolve(decodeGetInfoRes(res))
      })
      .catch(e => {
        console.log(e)
        reject(e)
      })
    }catch(e){
      reject(e)
    }
  })
};

However when the request is called its return the error
'got_1.default.get(...).json is not a function'

This error doesnt occur is the request is not mocked
not sure if this is a error or i am doing someting wrong

Activity

  1. Repository owner locked and limited conversation to collaborators on Dec 10, 2021
  2. converted this issue into a discussion #1943 on Dec 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions