Use node-fetch and msw

Signed-off-by: Tomas Dabasinskas <tomas@dabasinskas.net>
This commit is contained in:
Tomas Dabasinskas
2023-02-27 17:00:48 +02:00
parent 2128963db1
commit 518e317b61
4 changed files with 38 additions and 30 deletions
@@ -41,6 +41,7 @@
"@backstage/errors": "workspace:^",
"@backstage/plugin-catalog-backend": "workspace:^",
"@backstage/types": "workspace:^",
"@types/node-fetch": "^2.5.12",
"lodash": "^4.17.21",
"luxon": "^3.0.0",
"node-fetch": "^2.6.7",
@@ -48,6 +49,7 @@
"winston": "^3.2.1"
},
"devDependencies": {
"@backstage/backend-test-utils": "workspace:^",
"@backstage/cli": "workspace:^",
"@types/lodash": "^4.14.151",
"msw": "^0.49.0"
@@ -20,15 +20,18 @@ import {
PuppetDbEntityProviderConfig,
} from '../providers';
import { DEFAULT_NAMESPACE } from '@backstage/catalog-model';
import fetch from 'node-fetch';
import { setupRequestMockHandlers } from '@backstage/backend-test-utils';
import { rest } from 'msw';
import { setupServer } from 'msw/node';
import { ANNOTATION_PUPPET_CERTNAME, ENDPOINT_FACTSETS } from './constants';
jest.mock('node-fetch');
(global as any).fetch = fetch;
const { Response } = jest.requireActual('node-fetch');
describe('readPuppetNodes', () => {
const mockFetch = fetch as unknown as jest.Mocked<any>;
const worker = setupServer();
setupRequestMockHandlers(worker);
beforeEach(() => {
jest.clearAllMocks();
});
describe('where no query is specified', () => {
const config: PuppetDbEntityProviderConfig = {
@@ -37,10 +40,12 @@ describe('readPuppetNodes', () => {
};
beforeEach(async () => {
mockFetch.mockReturnValueOnce(
Promise.resolve(
new Response(
JSON.stringify([
worker.use(
rest.get(`${config.host}/${ENDPOINT_FACTSETS}`, (_req, res, ctx) => {
return res(
ctx.status(200),
ctx.set('Content-Type', 'application/json'),
ctx.json([
{
certname: 'node1',
timestamp: 'time1',
@@ -106,8 +111,8 @@ describe('readPuppetNodes', () => {
},
},
]),
),
),
);
}),
);
});
@@ -188,8 +193,14 @@ describe('readPuppetNodes', () => {
describe('where no results are matched', () => {
beforeEach(async () => {
mockFetch.mockReturnValueOnce(
Promise.resolve(new Response(JSON.stringify([]))),
worker.use(
rest.get(`${config.host}/${ENDPOINT_FACTSETS}`, (_req, res, ctx) => {
return res(
ctx.status(200),
ctx.set('Content-Type', 'application/json'),
ctx.json([]),
);
}),
);
});
@@ -201,10 +212,12 @@ describe('readPuppetNodes', () => {
describe('where results are matched', () => {
beforeEach(async () => {
mockFetch.mockReturnValueOnce(
Promise.resolve(
new Response(
JSON.stringify([
worker.use(
rest.get(`${config.host}/${ENDPOINT_FACTSETS}`, (_req, res, ctx) => {
return res(
ctx.status(200),
ctx.set('Content-Type', 'application/json'),
ctx.json([
{
certname: 'node1',
timestamp: 'time1',
@@ -214,23 +227,13 @@ describe('readPuppetNodes', () => {
environment: 'environment1',
},
]),
),
),
);
}),
);
});
it('should return matched results', async () => {
const entities = await readPuppetNodes(config);
expect(mockFetch).toHaveBeenCalledWith(
`${config.host}/${ENDPOINT_FACTSETS}?query=%5B%22%3D%22%2C+%22certname%22%2C+%22node1%22%5D`,
{
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
method: 'GET',
},
);
expect(entities).toHaveLength(1);
});
});
@@ -18,6 +18,7 @@ import { PuppetDbEntityProviderConfig } from '../providers';
import { PuppetNode, ResourceTransformer } from './types';
import { ResourceEntity } from '@backstage/catalog-model/';
import { defaultResourceTransformer } from './transformers';
import fetch from 'node-fetch';
import { ResponseError } from '@backstage/errors';
import { ENDPOINT_FACTSETS } from './constants';
import { Logger } from 'winston';
+2
View File
@@ -5212,6 +5212,7 @@ __metadata:
dependencies:
"@backstage/backend-common": "workspace:^"
"@backstage/backend-tasks": "workspace:^"
"@backstage/backend-test-utils": "workspace:^"
"@backstage/catalog-model": "workspace:^"
"@backstage/cli": "workspace:^"
"@backstage/config": "workspace:^"
@@ -5219,6 +5220,7 @@ __metadata:
"@backstage/plugin-catalog-backend": "workspace:^"
"@backstage/types": "workspace:^"
"@types/lodash": ^4.14.151
"@types/node-fetch": ^2.5.12
lodash: ^4.17.21
luxon: ^3.0.0
msw: ^0.49.0