From 2e4e6c350021a28ff44c198cb8118f19fcc6a3a0 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Mon, 5 Sep 2022 23:26:34 +0200 Subject: [PATCH] test fixes to support Jest 29 Signed-off-by: Patrik Oldsberg --- packages/cli/config/jest.js | 1 - .../src/apis/system/ApiProvider.test.tsx | 28 +++++++++++-------- .../TabbedLayout/TabbedLayout.test.tsx | 8 ++++-- .../ErrorBoundary/ErrorBoundary.test.tsx | 4 ++- .../ProxiedSignInIdentity.test.ts | 4 +-- .../src/extensions/extensions.test.tsx | 2 +- packages/integration/src/helpers.test.ts | 2 +- .../src/testUtils/appWrappers.test.tsx | 8 ++++-- plugins/api-docs/src/setupTests.ts | 4 +++ .../src/identity/FirestoreKeyStore.test.ts | 2 +- .../src/utils/getDurationFromDates.test.ts | 2 +- .../src/GitLabDiscoveryProcessor.test.ts | 2 +- .../PreparePullRequestForm.test.tsx | 1 - ...reIdentityKubernetesAuthTranslator.test.ts | 20 +++++++++---- .../src/components/RadarComponent.test.tsx | 6 ++-- 15 files changed, 58 insertions(+), 36 deletions(-) diff --git a/packages/cli/config/jest.js b/packages/cli/config/jest.js index 9d113989ff..4a1a4b79f5 100644 --- a/packages/cli/config/jest.js +++ b/packages/cli/config/jest.js @@ -27,7 +27,6 @@ const envOptions = { const transformIgnorePattern = [ '@material-ui', - '@rjsf', 'ajv', 'core-js', 'jest-.*', diff --git a/packages/core-app-api/src/apis/system/ApiProvider.test.tsx b/packages/core-app-api/src/apis/system/ApiProvider.test.tsx index c08674adfe..a6118f0f68 100644 --- a/packages/core-app-api/src/apis/system/ApiProvider.test.tsx +++ b/packages/core-app-api/src/apis/system/ApiProvider.test.tsx @@ -118,9 +118,10 @@ describe('ApiProvider', () => { }).toThrow(/^API context is not available/); }).error, ).toEqual([ - expect.stringMatching( - /^Error: Uncaught \[Error: API context is not available/, - ), + expect.objectContaining({ + detail: new Error('API context is not available'), + type: 'unhandled exception', + }), expect.stringMatching( /^The above error occurred in the component/, ), @@ -133,9 +134,10 @@ describe('ApiProvider', () => { }).toThrow(/^API context is not available/); }).error, ).toEqual([ - expect.stringMatching( - /^Error: Uncaught \[Error: API context is not available/, - ), + expect.objectContaining({ + detail: new Error('API context is not available'), + type: 'unhandled exception', + }), expect.stringMatching( /^The above error occurred in the component/, ), @@ -154,9 +156,10 @@ describe('ApiProvider', () => { }).toThrow('No implementation available for apiRef{x}'); }).error, ).toEqual([ - expect.stringMatching( - /^Error: Uncaught \[Error: No implementation available for apiRef{x}\]/, - ), + expect.objectContaining({ + detail: new Error('No implementation available for apiRef{x}'), + type: 'unhandled exception', + }), expect.stringMatching( /^The above error occurred in the component/, ), @@ -173,9 +176,10 @@ describe('ApiProvider', () => { }).toThrow('No implementation available for apiRef{x}'); }).error, ).toEqual([ - expect.stringMatching( - /^Error: Uncaught \[Error: No implementation available for apiRef{x}\]/, - ), + expect.objectContaining({ + detail: new Error('No implementation available for apiRef{x}'), + type: 'unhandled exception', + }), expect.stringMatching( /^The above error occurred in the component/, ), diff --git a/packages/core-components/src/components/TabbedLayout/TabbedLayout.test.tsx b/packages/core-components/src/components/TabbedLayout/TabbedLayout.test.tsx index b700739a8b..e103fc2f46 100644 --- a/packages/core-components/src/components/TabbedLayout/TabbedLayout.test.tsx +++ b/packages/core-components/src/components/TabbedLayout/TabbedLayout.test.tsx @@ -47,9 +47,11 @@ describe('TabbedLayout', () => { }); expect(error).toEqual([ - expect.stringMatching( - /Child of TabbedLayout must be an TabbedLayout.Route/, - ), + expect.objectContaining({ + detail: new Error( + 'Child of TabbedLayout must be an TabbedLayout.Route', + ), + }), expect.stringMatching( /The above error occurred in the component/, ), diff --git a/packages/core-components/src/layout/ErrorBoundary/ErrorBoundary.test.tsx b/packages/core-components/src/layout/ErrorBoundary/ErrorBoundary.test.tsx index 361ad123bf..6a16224a02 100644 --- a/packages/core-components/src/layout/ErrorBoundary/ErrorBoundary.test.tsx +++ b/packages/core-components/src/layout/ErrorBoundary/ErrorBoundary.test.tsx @@ -67,7 +67,9 @@ describe('', () => { }); expect(error).toEqual([ - expect.stringMatching(/^Error: Uncaught \[Error: Bomb\]/), + expect.objectContaining({ + detail: new Error('Bomb'), + }), expect.stringMatching( /^The above error occurred in the component:/, ), diff --git a/packages/core-components/src/layout/ProxiedSignInPage/ProxiedSignInIdentity.test.ts b/packages/core-components/src/layout/ProxiedSignInPage/ProxiedSignInIdentity.test.ts index 3c9b9c3f58..a3f6c185f8 100644 --- a/packages/core-components/src/layout/ProxiedSignInPage/ProxiedSignInIdentity.test.ts +++ b/packages/core-components/src/layout/ProxiedSignInPage/ProxiedSignInIdentity.test.ts @@ -29,7 +29,7 @@ const validBackstageToken = describe('ProxiedSignInIdentity', () => { describe('tokenToExpiry', () => { - beforeEach(() => jest.useFakeTimers('modern')); + beforeEach(() => jest.useFakeTimers()); afterEach(() => jest.useRealTimers()); it('handles undefined', async () => { @@ -56,7 +56,7 @@ describe('ProxiedSignInIdentity', () => { }); describe('ProxiedSignInIdentity', () => { - beforeEach(() => jest.useFakeTimers('modern')); + beforeEach(() => jest.useFakeTimers()); afterEach(() => jest.useRealTimers()); const worker = setupServer(); diff --git a/packages/core-plugin-api/src/extensions/extensions.test.tsx b/packages/core-plugin-api/src/extensions/extensions.test.tsx index 0601c01a17..3eca133608 100644 --- a/packages/core-plugin-api/src/extensions/extensions.test.tsx +++ b/packages/core-plugin-api/src/extensions/extensions.test.tsx @@ -117,7 +117,7 @@ describe('extensions', () => { render(); }); screen.getByText('Error in my-plugin'); - expect(errors[0]).toMatch('Test error'); + expect(errors[0]).toMatchObject({ detail: new Error('Test error') }); }); it('should wrap extended component with analytics context', async () => { diff --git a/packages/integration/src/helpers.test.ts b/packages/integration/src/helpers.test.ts index 06feb4104e..7468c83bc6 100644 --- a/packages/integration/src/helpers.test.ts +++ b/packages/integration/src/helpers.test.ts @@ -62,7 +62,7 @@ describe('isValidHost', () => { ['[::]', true], ['[::1]', true], ['[1:2:3:4:5:6:7:8]', true], - ['1.2.3.4.5.6.7.8', true], + ['1.2.3.4.5.6.7.8', false], ['https://example.com', false], ['foo:100000', false], ['FOO', false], diff --git a/packages/test-utils/src/testUtils/appWrappers.test.tsx b/packages/test-utils/src/testUtils/appWrappers.test.tsx index 1b0601d030..a116c8c781 100644 --- a/packages/test-utils/src/testUtils/appWrappers.test.tsx +++ b/packages/test-utils/src/testUtils/appWrappers.test.tsx @@ -92,9 +92,11 @@ describe('wrapInTestApp', () => { }); expect(error).toEqual([ - expect.stringMatching( - /^Error: Uncaught \[Error: MockErrorApi received unexpected error, Error: NOPE\]/, - ), + expect.objectContaining({ + detail: new Error( + 'MockErrorApi received unexpected error, Error: NOPE', + ), + }), expect.stringMatching(/^The above error occurred in the component:/), ]); }); diff --git a/plugins/api-docs/src/setupTests.ts b/plugins/api-docs/src/setupTests.ts index 963c0f188b..a41fe2eeb4 100644 --- a/plugins/api-docs/src/setupTests.ts +++ b/plugins/api-docs/src/setupTests.ts @@ -15,3 +15,7 @@ */ import '@testing-library/jest-dom'; + +Object.defineProperty(global, 'TextDecoder', { + value: require('util').TextDecoder, +}); diff --git a/plugins/auth-backend/src/identity/FirestoreKeyStore.test.ts b/plugins/auth-backend/src/identity/FirestoreKeyStore.test.ts index d28cbfa11a..b04cf8a6b3 100644 --- a/plugins/auth-backend/src/identity/FirestoreKeyStore.test.ts +++ b/plugins/auth-backend/src/identity/FirestoreKeyStore.test.ts @@ -44,7 +44,7 @@ jest.mock('@google-cloud/firestore', () => ({ Firestore: jest.fn().mockImplementation(() => firestoreMock), })); -jest.useFakeTimers('legacy'); +jest.useFakeTimers({ legacyFakeTimers: true }); describe('FirestoreKeyStore', () => { const key = { diff --git a/plugins/azure-devops/src/utils/getDurationFromDates.test.ts b/plugins/azure-devops/src/utils/getDurationFromDates.test.ts index fac0e917d0..2a8ee6e04f 100644 --- a/plugins/azure-devops/src/utils/getDurationFromDates.test.ts +++ b/plugins/azure-devops/src/utils/getDurationFromDates.test.ts @@ -18,7 +18,7 @@ import { getDurationFromDates } from './getDurationFromDates'; describe('getDurationFromDates', () => { beforeAll(() => { - jest.useFakeTimers('modern'); + jest.useFakeTimers(); jest.setSystemTime(new Date('2021-10-15T09:45:25.0000000Z')); }); diff --git a/plugins/catalog-backend-module-gitlab/src/GitLabDiscoveryProcessor.test.ts b/plugins/catalog-backend-module-gitlab/src/GitLabDiscoveryProcessor.test.ts index d0f2eb383d..385ce59bfb 100644 --- a/plugins/catalog-backend-module-gitlab/src/GitLabDiscoveryProcessor.test.ts +++ b/plugins/catalog-backend-module-gitlab/src/GitLabDiscoveryProcessor.test.ts @@ -153,7 +153,7 @@ describe('GitlabDiscoveryProcessor', () => { setupRequestMockHandlers(server); beforeAll(() => { - jest.useFakeTimers('modern'); + jest.useFakeTimers(); jest.setSystemTime(new Date(SERVER_TIME)); }); diff --git a/plugins/catalog-import/src/components/StepPrepareCreatePullRequest/PreparePullRequestForm.test.tsx b/plugins/catalog-import/src/components/StepPrepareCreatePullRequest/PreparePullRequestForm.test.tsx index b439366728..3acb7390a9 100644 --- a/plugins/catalog-import/src/components/StepPrepareCreatePullRequest/PreparePullRequestForm.test.tsx +++ b/plugins/catalog-import/src/components/StepPrepareCreatePullRequest/PreparePullRequestForm.test.tsx @@ -87,7 +87,6 @@ describe('', () => { {formState.errors.main && ( diff --git a/plugins/kubernetes-backend/src/kubernetes-auth-translator/AzureIdentityKubernetesAuthTranslator.test.ts b/plugins/kubernetes-backend/src/kubernetes-auth-translator/AzureIdentityKubernetesAuthTranslator.test.ts index a28f825434..704551eb9f 100644 --- a/plugins/kubernetes-backend/src/kubernetes-auth-translator/AzureIdentityKubernetesAuthTranslator.test.ts +++ b/plugins/kubernetes-backend/src/kubernetes-auth-translator/AzureIdentityKubernetesAuthTranslator.test.ts @@ -46,6 +46,10 @@ describe('AzureIdentityKubernetesAuthTranslator tests', () => { url: 'mycluster.privatelink.westeurope.azmk8s.io', }; + afterEach(() => { + jest.useRealTimers(); + }); + it('should decorate cluster with Azure token', async () => { const authTranslator = new AzureIdentityKubernetesAuthTranslator( logger, @@ -70,6 +74,8 @@ describe('AzureIdentityKubernetesAuthTranslator tests', () => { }); it('should issue new token 15 minutes befory expiry', async () => { + jest.useFakeTimers(); + const authTranslator = new AzureIdentityKubernetesAuthTranslator( logger, new StaticTokenCredential(16 * 60 * 1000), // token expires in 16min @@ -78,13 +84,15 @@ describe('AzureIdentityKubernetesAuthTranslator tests', () => { const response = await authTranslator.decorateClusterDetailsWithAuth(cd); expect(response.serviceAccountToken).toEqual('MY_TOKEN_1'); - jest.useFakeTimers().setSystemTime(Date.now() + 2 * 60 * 1000); // advance time by 2mins + jest.setSystemTime(Date.now() + 2 * 60 * 1000); // advance time by 2mins const response2 = await authTranslator.decorateClusterDetailsWithAuth(cd); expect(response2.serviceAccountToken).toEqual('MY_TOKEN_2'); }); it('should re-use existing token if there is afailure', async () => { + jest.useFakeTimers(); + const authTranslator = new AzureIdentityKubernetesAuthTranslator( logger, new StaticTokenCredential(16 * 60 * 1000), // new tokens expires in 16min @@ -93,12 +101,12 @@ describe('AzureIdentityKubernetesAuthTranslator tests', () => { const response = await authTranslator.decorateClusterDetailsWithAuth(cd); expect(response.serviceAccountToken).toEqual('MY_TOKEN_1'); - jest.useFakeTimers().setSystemTime(Date.now() + 2 * 60 * 1000); // advance time by 2min + jest.setSystemTime(Date.now() + 2 * 60 * 1000); // advance time by 2min const response2 = await authTranslator.decorateClusterDetailsWithAuth(cd); expect(response2.serviceAccountToken).toEqual('MY_TOKEN_2'); - jest.useFakeTimers().setSystemTime(Date.now() + 2 * 60 * 1000); // advance time by 2min + jest.setSystemTime(Date.now() + 2 * 60 * 1000); // advance time by 2min const response3 = await authTranslator.decorateClusterDetailsWithAuth(cd); expect(response3.serviceAccountToken).toEqual('MY_TOKEN_2'); @@ -108,6 +116,8 @@ describe('AzureIdentityKubernetesAuthTranslator tests', () => { }); it('should throw if existing token expired and failed to fetch a new one', async () => { + jest.useFakeTimers(); + const authTranslator = new AzureIdentityKubernetesAuthTranslator( logger, new StaticTokenCredential(16 * 60 * 1000), // new tokens expires in 16min @@ -116,12 +126,12 @@ describe('AzureIdentityKubernetesAuthTranslator tests', () => { const response = await authTranslator.decorateClusterDetailsWithAuth(cd); expect(response.serviceAccountToken).toEqual('MY_TOKEN_1'); - jest.useFakeTimers().setSystemTime(Date.now() + 2 * 60 * 1000); // advance time by 2min + jest.setSystemTime(Date.now() + 2 * 60 * 1000); // advance time by 2min const response2 = await authTranslator.decorateClusterDetailsWithAuth(cd); expect(response2.serviceAccountToken).toEqual('MY_TOKEN_2'); - jest.useFakeTimers().setSystemTime(Date.now() + 17 * 60 * 1000); // advance time by 17min + jest.setSystemTime(Date.now() + 17 * 60 * 1000); // advance time by 17min await expect( authTranslator.decorateClusterDetailsWithAuth(cd), diff --git a/plugins/tech-radar/src/components/RadarComponent.test.tsx b/plugins/tech-radar/src/components/RadarComponent.test.tsx index 6b8bd944b8..3d3b1bfd17 100644 --- a/plugins/tech-radar/src/components/RadarComponent.test.tsx +++ b/plugins/tech-radar/src/components/RadarComponent.test.tsx @@ -124,8 +124,8 @@ describe('RadarComponent', () => { ); }).toThrow(); }).error[0], - ).toMatch( - /^Error: Uncaught \[Error: No implementation available for apiRef{core.error}\]/, - ); + ).toMatchObject({ + detail: new Error('No implementation available for apiRef{core.error}'), + }); }); });