|
| 1 | +/** |
| 2 | + * Module dependencies. |
| 3 | + */ |
| 4 | +import { describe, test, expect } from '@jest/globals'; |
| 5 | +import configHelper from '../config.js'; |
| 6 | + |
| 7 | +/** |
| 8 | + * Unit tests for the environment predicate — isDevEnv / isProd. |
| 9 | + * |
| 10 | + * These are the shared production-hardening predicate. The deployment model runs |
| 11 | + * downstream apps as NODE_ENV={projectName} (any non-dev label), so hardening must |
| 12 | + * key off "is this NOT a known dev env" rather than the literal "production". |
| 13 | + */ |
| 14 | +describe('config helper — environment predicate (isDevEnv / isProd):', () => { |
| 15 | + describe('isDevEnv', () => { |
| 16 | + test('returns true for development', () => { |
| 17 | + expect(configHelper.isDevEnv('development')).toBe(true); |
| 18 | + }); |
| 19 | + |
| 20 | + test('returns true for test', () => { |
| 21 | + expect(configHelper.isDevEnv('test')).toBe(true); |
| 22 | + }); |
| 23 | + |
| 24 | + test('returns true for local', () => { |
| 25 | + expect(configHelper.isDevEnv('local')).toBe(true); |
| 26 | + }); |
| 27 | + |
| 28 | + test('returns false for production', () => { |
| 29 | + expect(configHelper.isDevEnv('production')).toBe(false); |
| 30 | + }); |
| 31 | + |
| 32 | + test('returns false for an arbitrary project env label', () => { |
| 33 | + expect(configHelper.isDevEnv('someproject')).toBe(false); |
| 34 | + }); |
| 35 | + }); |
| 36 | + |
| 37 | + describe('isProd', () => { |
| 38 | + test('returns true for production', () => { |
| 39 | + expect(configHelper.isProd('production')).toBe(true); |
| 40 | + }); |
| 41 | + |
| 42 | + test('returns true for an arbitrary project env label (downstream deployment model)', () => { |
| 43 | + expect(configHelper.isProd('someproject')).toBe(true); |
| 44 | + }); |
| 45 | + |
| 46 | + test('returns false for development', () => { |
| 47 | + expect(configHelper.isProd('development')).toBe(false); |
| 48 | + }); |
| 49 | + |
| 50 | + test('returns false for test', () => { |
| 51 | + expect(configHelper.isProd('test')).toBe(false); |
| 52 | + }); |
| 53 | + |
| 54 | + test('returns false for local', () => { |
| 55 | + expect(configHelper.isProd('local')).toBe(false); |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + describe('default argument reads process.env.NODE_ENV at call time', () => { |
| 60 | + test('isDevEnv() honors NODE_ENV mutated after import', () => { |
| 61 | + const original = process.env.NODE_ENV; |
| 62 | + try { |
| 63 | + process.env.NODE_ENV = 'production'; |
| 64 | + expect(configHelper.isDevEnv()).toBe(false); |
| 65 | + expect(configHelper.isProd()).toBe(true); |
| 66 | + process.env.NODE_ENV = 'development'; |
| 67 | + expect(configHelper.isDevEnv()).toBe(true); |
| 68 | + expect(configHelper.isProd()).toBe(false); |
| 69 | + } finally { |
| 70 | + process.env.NODE_ENV = original; |
| 71 | + } |
| 72 | + }); |
| 73 | + |
| 74 | + test('isProd() defaults to development (dev) when NODE_ENV is unset', () => { |
| 75 | + const original = process.env.NODE_ENV; |
| 76 | + try { |
| 77 | + delete process.env.NODE_ENV; |
| 78 | + expect(configHelper.isProd()).toBe(false); |
| 79 | + expect(configHelper.isDevEnv()).toBe(true); |
| 80 | + } finally { |
| 81 | + process.env.NODE_ENV = original; |
| 82 | + } |
| 83 | + }); |
| 84 | + }); |
| 85 | +}); |
0 commit comments