From 742dbdd0a5c0f0f75d56101b3551077ec06cd53f Mon Sep 17 00:00:00 2001 From: Cody Leff Date: Thu, 1 Sep 2022 08:42:07 -0700 Subject: [PATCH] fix: Fix console errors about feature flags when running tests (#21275) --- .../test/utils/featureFlag.test.ts | 48 +++++++++++++++---- superset-frontend/spec/helpers/shim.ts | 1 + 2 files changed, 39 insertions(+), 10 deletions(-) diff --git a/superset-frontend/packages/superset-ui-core/test/utils/featureFlag.test.ts b/superset-frontend/packages/superset-ui-core/test/utils/featureFlag.test.ts index 52a57909aa386..b2a273d2ed78b 100644 --- a/superset-frontend/packages/superset-ui-core/test/utils/featureFlag.test.ts +++ b/superset-frontend/packages/superset-ui-core/test/utils/featureFlag.test.ts @@ -19,17 +19,45 @@ import { FeatureFlag, isFeatureEnabled } from '@superset-ui/core'; -describe('isFeatureFlagEnabled', () => { +const originalFeatureFlags = window.featureFlags; +// eslint-disable-next-line no-console +const originalConsoleError = console.error; +const reset = () => { + window.featureFlags = originalFeatureFlags; + // eslint-disable-next-line no-console + console.error = originalConsoleError; +}; + +it('returns false and raises console error if feature flags have not been initialized', () => { + // eslint-disable-next-line no-console + console.error = jest.fn(); + delete (window as any).featureFlags; + expect(isFeatureEnabled(FeatureFlag.ALLOW_DASHBOARD_DOMAIN_SHARDING)).toEqual( + false, + ); + + // eslint-disable-next-line no-console + expect(console.error).toHaveBeenNthCalledWith( + 1, + 'Failed to query feature flag ALLOW_DASHBOARD_DOMAIN_SHARDING (see error below)', + ); + + reset(); +}); + +it('returns false for unset feature flag', () => { + expect(isFeatureEnabled(FeatureFlag.ALLOW_DASHBOARD_DOMAIN_SHARDING)).toEqual( + false, + ); + + reset(); +}); + +it('returns true for set feature flag', () => { window.featureFlags = { [FeatureFlag.CLIENT_CACHE]: true, }; - it('returns false for unset feature flag', () => { - expect( - isFeatureEnabled(FeatureFlag.ALLOW_DASHBOARD_DOMAIN_SHARDING), - ).toEqual(false); - }); - - it('returns true for set feature flag', () => { - expect(isFeatureEnabled(FeatureFlag.CLIENT_CACHE)).toEqual(true); - }); + + expect(isFeatureEnabled(FeatureFlag.CLIENT_CACHE)).toEqual(true); + reset(); }); diff --git a/superset-frontend/spec/helpers/shim.ts b/superset-frontend/spec/helpers/shim.ts index 7955ee15e0f0f..cf11e106b7548 100644 --- a/superset-frontend/spec/helpers/shim.ts +++ b/superset-frontend/spec/helpers/shim.ts @@ -53,6 +53,7 @@ g.window.performance = { now: () => new Date().getTime() }; g.window.Worker = Worker; g.window.IntersectionObserver = IntersectionObserver; g.window.ResizeObserver = ResizeObserver; +g.window.featureFlags = {}; g.URL.createObjectURL = () => ''; g.caches = new CacheStorage();