diff --git a/plugins/catalog-backend-module-puppetdb/package.json b/plugins/catalog-backend-module-puppetdb/package.json index 0ccb2fc24d..93646e01e7 100644 --- a/plugins/catalog-backend-module-puppetdb/package.json +++ b/plugins/catalog-backend-module-puppetdb/package.json @@ -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" diff --git a/plugins/catalog-backend-module-puppetdb/src/puppet/read.test.ts b/plugins/catalog-backend-module-puppetdb/src/puppet/read.test.ts index e0bf5c6d43..78fc856066 100644 --- a/plugins/catalog-backend-module-puppetdb/src/puppet/read.test.ts +++ b/plugins/catalog-backend-module-puppetdb/src/puppet/read.test.ts @@ -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; + 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); }); }); diff --git a/plugins/catalog-backend-module-puppetdb/src/puppet/read.ts b/plugins/catalog-backend-module-puppetdb/src/puppet/read.ts index 67247d73a5..601ee28207 100644 --- a/plugins/catalog-backend-module-puppetdb/src/puppet/read.ts +++ b/plugins/catalog-backend-module-puppetdb/src/puppet/read.ts @@ -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'; diff --git a/yarn.lock b/yarn.lock index 2b88e6ce8c..55750f13be 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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