From 5f599539c2c484c502181edc2acd532e039879a2 Mon Sep 17 00:00:00 2001 From: blam Date: Wed, 14 Jun 2023 10:10:56 +0200 Subject: [PATCH] feat: fix this another way Signed-off-by: blam --- packages/cli/config/jest.js | 11 +++++-- packages/cli/config/jestJsdomEnvironment.js | 30 ------------------- packages/cli/package.json | 1 + .../airbrake/src/api/ProductionApi.test.ts | 3 +- 4 files changed, 11 insertions(+), 34 deletions(-) delete mode 100644 packages/cli/config/jestJsdomEnvironment.js diff --git a/packages/cli/config/jest.js b/packages/cli/config/jest.js index 68f4b77d94..1b32f954ef 100644 --- a/packages/cli/config/jest.js +++ b/packages/cli/config/jest.js @@ -48,7 +48,7 @@ function getRoleConfig(role) { case 'common-library': case 'frontend-plugin': case 'frontend-plugin-module': - return { testEnvironment: require.resolve('./jestJsdomEnvironment') }; + return { testEnvironment: require.resolve('jest-environment-jsdom') }; case 'cli': case 'backend': case 'node-library': @@ -194,9 +194,16 @@ async function getProjectConfig(targetPath, extraConfig) { ...getRoleConfig(closestPkgJson?.backstage?.role), }; + options.setupFilesAfterEnv = []; + + if (options.testEnvironment === require.resolve('jest-environment-jsdom')) { + // FIXME https://github.com/jsdom/jsdom/issues/1724 + options.setupFilesAfterEnv.push(require.resolve('cross-fetch/polyfill')); + } + // Use src/setupTests.ts as the default location for configuring test env if (fs.existsSync(path.resolve(targetPath, 'src/setupTests.ts'))) { - options.setupFilesAfterEnv = ['/setupTests.ts']; + options.setupFilesAfterEnv.push('/setupTests.ts'); } const config = Object.assign(options, ...pkgJsonConfigs); diff --git a/packages/cli/config/jestJsdomEnvironment.js b/packages/cli/config/jestJsdomEnvironment.js deleted file mode 100644 index 14cdf929eb..0000000000 --- a/packages/cli/config/jestJsdomEnvironment.js +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright 2023 The Backstage Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -import JSDOMEnvironment from 'jest-environment-jsdom'; -import { Headers, Request, Response, default as fetch } from 'node-fetch'; - -// https://github.com/facebook/jest/blob/v29.4.3/website/versioned_docs/version-29.4/Configuration.md#testenvironment-string -export default class FixJSDOMEnvironment extends JSDOMEnvironment { - constructor(...args) { - super(...args); - - // FIXME https://github.com/jsdom/jsdom/issues/1724 - this.global.fetch = fetch; - this.global.Headers = Headers; - this.global.Request = Request; - this.global.Response = Response; - } -} diff --git a/packages/cli/package.json b/packages/cli/package.json index 4da91b0870..8a0c0508e8 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -74,6 +74,7 @@ "chalk": "^4.0.0", "chokidar": "^3.3.1", "commander": "^9.1.0", + "cross-fetch": "^3.1.5", "cross-spawn": "^7.0.3", "css-loader": "^6.5.1", "diff": "^5.0.0", diff --git a/plugins/airbrake/src/api/ProductionApi.test.ts b/plugins/airbrake/src/api/ProductionApi.test.ts index bb0fab7ee0..92593e42bc 100644 --- a/plugins/airbrake/src/api/ProductionApi.test.ts +++ b/plugins/airbrake/src/api/ProductionApi.test.ts @@ -37,8 +37,7 @@ describe('The production Airbrake API', () => { ); const groups = await productionApi.fetchGroups('123456'); - - expect(groups).toEqual(mockGroupsData); + expect(groups).toStrictEqual(mockGroupsData); }); it('throws if fetching groups was unsuccessful', async () => {