From aa318a0b6c9c799ada087c7be76592da2d544c9f Mon Sep 17 00:00:00 2001 From: blam Date: Tue, 13 Jun 2023 15:51:34 +0200 Subject: [PATCH] feat: provide a decorated fetch environment for js-dom Signed-off-by: blam --- packages/cli/config/jest.js | 2 +- packages/cli/config/jsdomEnvironment.js | 30 +++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 packages/cli/config/jsdomEnvironment.js diff --git a/packages/cli/config/jest.js b/packages/cli/config/jest.js index 0e74b00d5a..8977c94cdf 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('jest-environment-jsdom') }; + return { testEnvironment: require.resolve('./jsdomEnvironment') }; case 'cli': case 'backend': case 'node-library': diff --git a/packages/cli/config/jsdomEnvironment.js b/packages/cli/config/jsdomEnvironment.js new file mode 100644 index 0000000000..bef457ea40 --- /dev/null +++ b/packages/cli/config/jsdomEnvironment.js @@ -0,0 +1,30 @@ +/* + * 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 } 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; + } +}