From 9212439d15822687cefbaba6fc6b4fe5774fe181 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20Adel=C3=B6w?= Date: Wed, 24 Aug 2022 11:26:12 +0200 Subject: [PATCH] just some more use of setupRequestMockHandlers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fredrik Adelöw --- .changeset/big-dolls-reflect.md | 6 ++++++ plugins/auth-node/package.json | 1 + plugins/auth-node/src/IdentityClient.test.ts | 6 +++--- .../src/lib/BitbucketServerClient.test.ts | 5 ++--- .../src/BitbucketDiscoveryProcessor.test.ts | 5 ++--- .../src/providers/GerritEntityProvider.test.ts | 7 ++++--- .../src/GitLabDiscoveryProcessor.test.ts | 7 ++++--- .../src/search/DefaultCatalogCollator.test.ts | 5 ++--- .../src/search/DefaultCatalogCollatorFactory.test.ts | 11 ++--------- .../src/components/GithubDeploymentsCard.test.tsx | 1 - plugins/permission-node/package.json | 1 + .../src/ServerPermissionClient.test.ts | 6 ++---- 12 files changed, 29 insertions(+), 32 deletions(-) create mode 100644 .changeset/big-dolls-reflect.md diff --git a/.changeset/big-dolls-reflect.md b/.changeset/big-dolls-reflect.md new file mode 100644 index 0000000000..e57fb74e09 --- /dev/null +++ b/.changeset/big-dolls-reflect.md @@ -0,0 +1,6 @@ +--- +'@backstage/plugin-auth-node': patch +'@backstage/plugin-permission-node': patch +--- + +Minor update to tests diff --git a/plugins/auth-node/package.json b/plugins/auth-node/package.json index 86973e1012..2f8509889d 100644 --- a/plugins/auth-node/package.json +++ b/plugins/auth-node/package.json @@ -31,6 +31,7 @@ "winston": "^3.2.1" }, "devDependencies": { + "@backstage/backend-test-utils": "^0.1.28-next.0", "@backstage/cli": "^0.18.2-next.0", "lodash": "^4.17.21", "msw": "^0.45.0", diff --git a/plugins/auth-node/src/IdentityClient.test.ts b/plugins/auth-node/src/IdentityClient.test.ts index 5451bd3f9e..bdc7dd5cd7 100644 --- a/plugins/auth-node/src/IdentityClient.test.ts +++ b/plugins/auth-node/src/IdentityClient.test.ts @@ -13,7 +13,9 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + import { PluginEndpointDiscovery } from '@backstage/backend-common'; +import { setupRequestMockHandlers } from '@backstage/backend-test-utils'; import { decodeProtectedHeader, exportJWK, @@ -100,9 +102,7 @@ describe('IdentityClient', () => { let factory: FakeTokenFactory; const keyDurationSeconds = 5; - beforeAll(() => server.listen({ onUnhandledRequest: 'error' })); - afterAll(() => server.close()); - afterEach(() => server.resetHandlers()); + setupRequestMockHandlers(server); beforeEach(() => { client = IdentityClient.create({ discovery, issuer: mockBaseUrl }); diff --git a/plugins/catalog-backend-module-bitbucket-server/src/lib/BitbucketServerClient.test.ts b/plugins/catalog-backend-module-bitbucket-server/src/lib/BitbucketServerClient.test.ts index 66ccdc8c9a..d205b7434e 100644 --- a/plugins/catalog-backend-module-bitbucket-server/src/lib/BitbucketServerClient.test.ts +++ b/plugins/catalog-backend-module-bitbucket-server/src/lib/BitbucketServerClient.test.ts @@ -14,6 +14,7 @@ * limitations under the License. */ +import { setupRequestMockHandlers } from '@backstage/backend-test-utils'; import { BitbucketServerIntegrationConfig } from '@backstage/integration'; import { rest } from 'msw'; import { setupServer } from 'msw/node'; @@ -49,9 +50,7 @@ describe('BitbucketServerClient', () => { config: config, }); - beforeAll(() => server.listen({ onUnhandledRequest: 'error' })); - afterAll(() => server.close()); - afterEach(() => server.resetHandlers()); + setupRequestMockHandlers(server); it('listProjects', async () => { server.use( diff --git a/plugins/catalog-backend-module-bitbucket/src/BitbucketDiscoveryProcessor.test.ts b/plugins/catalog-backend-module-bitbucket/src/BitbucketDiscoveryProcessor.test.ts index ad893e7c87..c2f98bf549 100644 --- a/plugins/catalog-backend-module-bitbucket/src/BitbucketDiscoveryProcessor.test.ts +++ b/plugins/catalog-backend-module-bitbucket/src/BitbucketDiscoveryProcessor.test.ts @@ -15,6 +15,7 @@ */ import { getVoidLogger } from '@backstage/backend-common'; +import { setupRequestMockHandlers } from '@backstage/backend-test-utils'; import { ConfigReader } from '@backstage/config'; import { Models } from '@backstage/plugin-bitbucket-cloud-common'; import { @@ -175,9 +176,7 @@ function setupBitbucketCloudSearchStubs( } describe('BitbucketDiscoveryProcessor', () => { - beforeAll(() => server.listen()); - afterEach(() => server.resetHandlers()); - afterAll(() => server.close()); + setupRequestMockHandlers(server); afterEach(() => jest.resetAllMocks()); diff --git a/plugins/catalog-backend-module-gerrit/src/providers/GerritEntityProvider.test.ts b/plugins/catalog-backend-module-gerrit/src/providers/GerritEntityProvider.test.ts index ee65d536c7..26aacb9126 100644 --- a/plugins/catalog-backend-module-gerrit/src/providers/GerritEntityProvider.test.ts +++ b/plugins/catalog-backend-module-gerrit/src/providers/GerritEntityProvider.test.ts @@ -13,9 +13,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + import { getVoidLogger } from '@backstage/backend-common'; import { ConfigReader } from '@backstage/config'; import { TaskInvocationDefinition, TaskRunner } from '@backstage/backend-tasks'; +import { setupRequestMockHandlers } from '@backstage/backend-test-utils'; import { EntityProviderConnection } from '@backstage/plugin-catalog-backend'; import { rest } from 'msw'; import fs from 'fs-extra'; @@ -49,12 +51,11 @@ class PersistingTaskRunner implements TaskRunner { const logger = getVoidLogger(); describe('GerritEntityProvider', () => { - beforeAll(() => server.listen()); + setupRequestMockHandlers(server); + afterEach(() => { jest.resetAllMocks(); - server.resetHandlers(); }); - afterAll(() => server.close()); const config = new ConfigReader({ catalog: { diff --git a/plugins/catalog-backend-module-gitlab/src/GitLabDiscoveryProcessor.test.ts b/plugins/catalog-backend-module-gitlab/src/GitLabDiscoveryProcessor.test.ts index 341167f279..d0f2eb383d 100644 --- a/plugins/catalog-backend-module-gitlab/src/GitLabDiscoveryProcessor.test.ts +++ b/plugins/catalog-backend-module-gitlab/src/GitLabDiscoveryProcessor.test.ts @@ -15,6 +15,7 @@ */ import { getVoidLogger } from '@backstage/backend-common'; +import { setupRequestMockHandlers } from '@backstage/backend-test-utils'; import { ConfigReader } from '@backstage/config'; import { LocationSpec } from '@backstage/plugin-catalog-backend'; import { rest, RestRequest } from 'msw'; @@ -149,14 +150,14 @@ function getProcessor({ } describe('GitlabDiscoveryProcessor', () => { + setupRequestMockHandlers(server); + beforeAll(() => { - server.listen(); jest.useFakeTimers('modern'); jest.setSystemTime(new Date(SERVER_TIME)); }); - afterEach(() => server.resetHandlers()); + afterAll(() => { - server.close(); jest.useRealTimers(); }); diff --git a/plugins/catalog-backend/src/search/DefaultCatalogCollator.test.ts b/plugins/catalog-backend/src/search/DefaultCatalogCollator.test.ts index 81abb91328..8c8e3f1a25 100644 --- a/plugins/catalog-backend/src/search/DefaultCatalogCollator.test.ts +++ b/plugins/catalog-backend/src/search/DefaultCatalogCollator.test.ts @@ -18,6 +18,7 @@ import { PluginEndpointDiscovery, TokenManager, } from '@backstage/backend-common'; +import { setupRequestMockHandlers } from '@backstage/backend-test-utils'; import { Entity } from '@backstage/catalog-model'; import { DefaultCatalogCollator } from './DefaultCatalogCollator'; import { setupServer } from 'msw/node'; @@ -61,6 +62,7 @@ describe('DefaultCatalogCollator', () => { let mockTokenManager: jest.Mocked; let collator: DefaultCatalogCollator; + setupRequestMockHandlers(server); beforeAll(() => { mockDiscoveryApi = { getBaseUrl: jest.fn().mockResolvedValue('http://localhost:7007'), @@ -74,7 +76,6 @@ describe('DefaultCatalogCollator', () => { discovery: mockDiscoveryApi, tokenManager: mockTokenManager, }); - server.listen(); }); beforeEach(() => { server.use( @@ -91,9 +92,7 @@ describe('DefaultCatalogCollator', () => { }), ); }); - afterEach(() => server.resetHandlers()); afterAll(() => { - server.close(); jest.useRealTimers(); }); diff --git a/plugins/catalog-backend/src/search/DefaultCatalogCollatorFactory.test.ts b/plugins/catalog-backend/src/search/DefaultCatalogCollatorFactory.test.ts index ccb61b8680..5fc8ca6fbf 100644 --- a/plugins/catalog-backend/src/search/DefaultCatalogCollatorFactory.test.ts +++ b/plugins/catalog-backend/src/search/DefaultCatalogCollatorFactory.test.ts @@ -17,6 +17,7 @@ import { PluginEndpointDiscovery, TokenManager, } from '@backstage/backend-common'; +import { setupRequestMockHandlers } from '@backstage/backend-test-utils'; import { Entity } from '@backstage/catalog-model'; import { ConfigReader } from '@backstage/config'; import { TestPipeline } from '@backstage/plugin-search-backend-node'; @@ -72,9 +73,7 @@ describe('DefaultCatalogCollatorFactory', () => { tokenManager: mockTokenManager, }; - beforeAll(() => { - server.listen(); - }); + setupRequestMockHandlers(server); beforeEach(() => { server.use( @@ -96,12 +95,6 @@ describe('DefaultCatalogCollatorFactory', () => { ); }); - afterAll(() => { - server.close(); - }); - - afterEach(() => server.resetHandlers()); - it('has expected type', () => { const factory = DefaultCatalogCollatorFactory.fromConfig(config, options); expect(factory.type).toBe('software-catalog'); diff --git a/plugins/github-deployments/src/components/GithubDeploymentsCard.test.tsx b/plugins/github-deployments/src/components/GithubDeploymentsCard.test.tsx index dedd2c3082..1f354914bb 100644 --- a/plugins/github-deployments/src/components/GithubDeploymentsCard.test.tsx +++ b/plugins/github-deployments/src/components/GithubDeploymentsCard.test.tsx @@ -130,7 +130,6 @@ describe('github-deployments', () => { setupRequestMockHandlers(worker); beforeEach(() => { - worker.resetHandlers(); jest.resetAllMocks(); }); diff --git a/plugins/permission-node/package.json b/plugins/permission-node/package.json index 150a9aabdf..f2056cdffe 100644 --- a/plugins/permission-node/package.json +++ b/plugins/permission-node/package.json @@ -44,6 +44,7 @@ "zod": "^3.11.6" }, "devDependencies": { + "@backstage/backend-test-utils": "^0.1.28-next.0", "@backstage/cli": "^0.18.2-next.0", "@types/supertest": "^2.0.8", "msw": "^0.45.0", diff --git a/plugins/permission-node/src/ServerPermissionClient.test.ts b/plugins/permission-node/src/ServerPermissionClient.test.ts index e531bd3556..bcdd5053b0 100644 --- a/plugins/permission-node/src/ServerPermissionClient.test.ts +++ b/plugins/permission-node/src/ServerPermissionClient.test.ts @@ -22,6 +22,7 @@ import { DefinitivePolicyDecision, ConditionalPolicyDecision, } from '@backstage/plugin-permission-common'; +import { setupRequestMockHandlers } from '@backstage/backend-test-utils'; import { ConfigReader } from '@backstage/config'; import { getVoidLogger, @@ -60,8 +61,7 @@ const config = new ConfigReader({ const logger = getVoidLogger(); describe('ServerPermissionClient', () => { - beforeAll(() => server.listen({ onUnhandledRequest: 'error' })); - afterAll(() => server.close()); + setupRequestMockHandlers(server); it('should error if permissions are enabled but a no-op token manager is configured', async () => { expect(() => @@ -91,7 +91,6 @@ describe('ServerPermissionClient', () => { server.use(rest.post(`${mockBaseUrl}/authorize`, mockAuthorizeHandler)); }); - afterEach(() => server.resetHandlers()); it('should bypass the permission backend if permissions are disabled', async () => { const client = ServerPermissionClient.fromConfig(new ConfigReader({}), { @@ -154,7 +153,6 @@ describe('ServerPermissionClient', () => { server.use(rest.post(`${mockBaseUrl}/authorize`, mockAuthorizeHandler)); }); - afterEach(() => server.resetHandlers()); it('should bypass the permission backend if permissions are disabled', async () => { const client = ServerPermissionClient.fromConfig(new ConfigReader({}), {