jsDomWithFetchAPI.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. import JSDOMEnvironment from 'jest-environment-jsdom';
  20. // https://github.com/facebook/jest/blob/v29.4.3/website/versioned_docs/version-29.4/Configuration.md#testenvironment-string
  21. export default class FixJSDOMEnvironment extends JSDOMEnvironment {
  22. constructor(...args: ConstructorParameters<typeof JSDOMEnvironment>) {
  23. super(...args);
  24. // FIXME https://github.com/jsdom/jsdom/issues/1724
  25. this.global.fetch = fetch;
  26. this.global.Headers = Headers;
  27. this.global.Request = Request;
  28. this.global.Response = Response;
  29. this.global.AbortSignal = AbortSignal;
  30. this.global.AbortController = AbortController;
  31. // Mock MessageChannel to prevent hanging Jest tests with rc-overflow@1.4.1
  32. // Forces rc-overflow to use requestAnimationFrame fallback instead
  33. // Can be removed when rc-overflow properly cleans up MessagePorts in test environments
  34. // See: https://github.com/apache/superset/pull/34871
  35. this.global.MessageChannel = undefined as any;
  36. this.global.MessagePort = undefined as any;
  37. }
  38. }