Repository navigation
Closed
Description
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
Metadata
Metadata
Assignees
Labels
No labels
Activity