|
| 1 | +import { applyCiFlagOverrides } from '@utils/ci-flag-overrides'; |
| 2 | + |
| 3 | +jest.mock('@utils/debug', () => ({ |
| 4 | + logToFile: jest.fn(), |
| 5 | + debug: jest.fn(), |
| 6 | +})); |
| 7 | + |
| 8 | +const ENV_KEY = 'WIZARD_CI_FLAG_OVERRIDES'; |
| 9 | + |
| 10 | +describe('applyCiFlagOverrides', () => { |
| 11 | + afterEach(() => { |
| 12 | + delete process.env[ENV_KEY]; |
| 13 | + }); |
| 14 | + |
| 15 | + // Jest runs with NODE_ENV=test, so IS_PRODUCTION_BUILD is false and the |
| 16 | + // override path is live — the same shape a `build:ci` bundle has. |
| 17 | + describe('in CI builds', () => { |
| 18 | + it('returns the flags untouched when no override is set', () => { |
| 19 | + const flags = { 'wizard-orchestrator': 'false' }; |
| 20 | + expect(applyCiFlagOverrides(flags)).toEqual(flags); |
| 21 | + }); |
| 22 | + |
| 23 | + it('merges overrides over the fetched flags, stringifying values', () => { |
| 24 | + process.env[ENV_KEY] = JSON.stringify({ |
| 25 | + 'wizard-orchestrator': true, |
| 26 | + 'wizard-next-v2': 'legacy', |
| 27 | + }); |
| 28 | + expect( |
| 29 | + applyCiFlagOverrides({ |
| 30 | + 'wizard-orchestrator': 'false', |
| 31 | + 'wizard-react-router': 'true', |
| 32 | + }), |
| 33 | + ).toEqual({ |
| 34 | + 'wizard-orchestrator': 'true', |
| 35 | + 'wizard-next-v2': 'legacy', |
| 36 | + 'wizard-react-router': 'true', |
| 37 | + }); |
| 38 | + }); |
| 39 | + |
| 40 | + it('fails loudly on malformed JSON instead of testing live flags', () => { |
| 41 | + process.env[ENV_KEY] = 'wizard-orchestrator=true'; |
| 42 | + expect(() => applyCiFlagOverrides({})).toThrow(/not valid JSON/); |
| 43 | + }); |
| 44 | + }); |
| 45 | + |
| 46 | + describe('in production builds', () => { |
| 47 | + it('is inert: overrides are ignored even when the env var is set', () => { |
| 48 | + const prevNodeEnv = process.env.NODE_ENV; |
| 49 | + process.env.NODE_ENV = 'production'; |
| 50 | + process.env[ENV_KEY] = JSON.stringify({ 'wizard-orchestrator': true }); |
| 51 | + let result: Record<string, string> | undefined; |
| 52 | + jest.isolateModules(() => { |
| 53 | + // eslint-disable-next-line @typescript-eslint/no-var-requires |
| 54 | + const prod = require('@utils/ci-flag-overrides') as { |
| 55 | + applyCiFlagOverrides: typeof applyCiFlagOverrides; |
| 56 | + }; |
| 57 | + result = prod.applyCiFlagOverrides({ 'wizard-orchestrator': 'false' }); |
| 58 | + }); |
| 59 | + process.env.NODE_ENV = prevNodeEnv; |
| 60 | + expect(result).toEqual({ 'wizard-orchestrator': 'false' }); |
| 61 | + }); |
| 62 | + }); |
| 63 | +}); |
0 commit comments