diff --git a/superset-frontend/packages/superset-ui-core/src/utils/featureFlags.ts b/superset-frontend/packages/superset-ui-core/src/utils/featureFlags.ts index 380704f0180b6..4eb90ea0afffa 100644 --- a/superset-frontend/packages/superset-ui-core/src/utils/featureFlags.ts +++ b/superset-frontend/packages/superset-ui-core/src/utils/featureFlags.ts @@ -87,14 +87,11 @@ declare global { } } -export function isFeatureEnabled(feature: FeatureFlag) { +export function isFeatureEnabled(feature: FeatureFlag): boolean { try { return !!window.featureFlags[feature]; } catch (error) { - // eslint-disable-next-line no-console - console.error(`Failed to query feature flag ${feature} (see error below)`); - // eslint-disable-next-line no-console - console.error(error); - return false; + console.error(`Failed to query feature flag ${feature}`); } + return false; } 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 b2a273d2ed78b..66c58e79af567 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 @@ -16,48 +16,41 @@ * specific language governing permissions and limitations * under the License. */ - -import { FeatureFlag, isFeatureEnabled } from '@superset-ui/core'; - -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; -}; +import mockConsole from 'jest-mock-console'; +import { isFeatureEnabled, FeatureFlag } from '@superset-ui/core'; 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; + mockConsole(); + Object.defineProperty(window, 'featureFlags', { + value: undefined, + }); + 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)', + expect(console.error).toHaveBeenCalled(); + // @ts-expect-error + expect(console.error.mock.calls[0][0]).toEqual( + 'Failed to query feature flag ALLOW_DASHBOARD_DOMAIN_SHARDING', ); - - reset(); }); it('returns false for unset feature flag', () => { + Object.defineProperty(window, 'featureFlags', { + value: {}, + }); + expect(isFeatureEnabled(FeatureFlag.ALLOW_DASHBOARD_DOMAIN_SHARDING)).toEqual( false, ); - - reset(); }); it('returns true for set feature flag', () => { - window.featureFlags = { - [FeatureFlag.CLIENT_CACHE]: true, - }; + Object.defineProperty(window, 'featureFlags', { + value: { + CLIENT_CACHE: true, + }, + }); expect(isFeatureEnabled(FeatureFlag.CLIENT_CACHE)).toEqual(true); - reset(); });