@@ -20,8 +20,12 @@ describe('LicenseValidator', () => {
2020 let testPublicKey : string ;
2121 let mockProcessExit : jest . SpyInstance ;
2222 let mockConsoleError : jest . SpyInstance ;
23+ let originalEnv : NodeJS . ProcessEnv ;
2324
2425 beforeEach ( ( ) => {
26+ // Store original environment
27+ originalEnv = { ...process . env } ;
28+
2529 // Clear the module cache to get a fresh instance
2630 jest . resetModules ( ) ;
2731
@@ -69,10 +73,22 @@ describe('LicenseValidator', () => {
6973 } ) ;
7074
7175 afterEach ( ( ) => {
72- delete process . env . REACT_ON_RAILS_PRO_LICENSE ;
76+ // Restore original environment
77+ process . env = originalEnv ;
7378 jest . restoreAllMocks ( ) ;
7479 } ) ;
7580
81+ /**
82+ * Helper function to mock the REACT_ON_RAILS_PRO_LICENSE environment variable
83+ */
84+ const mockLicenseEnv = ( token : string | undefined ) => {
85+ if ( token === undefined ) {
86+ delete process . env . REACT_ON_RAILS_PRO_LICENSE ;
87+ } else {
88+ process . env . REACT_ON_RAILS_PRO_LICENSE = token ;
89+ }
90+ } ;
91+
7692 describe ( 'validateLicense' , ( ) => {
7793 it ( 'validates successfully for valid license in ENV' , ( ) => {
7894 const validPayload = {
@@ -82,7 +98,7 @@ describe('LicenseValidator', () => {
8298 } ;
8399
84100 const validToken = jwt . sign ( validPayload , testPrivateKey , { algorithm : 'RS256' } ) ;
85- process . env . REACT_ON_RAILS_PRO_LICENSE = validToken ;
101+ mockLicenseEnv ( validToken ) ;
86102
87103 const module = jest . requireActual < LicenseValidatorModule > ( '../src/shared/licenseValidator' ) ;
88104 expect ( module . validateLicense ( ) ) . toBe ( true ) ;
@@ -96,7 +112,7 @@ describe('LicenseValidator', () => {
96112 } ;
97113
98114 const expiredToken = jwt . sign ( expiredPayload , testPrivateKey , { algorithm : 'RS256' } ) ;
99- process . env . REACT_ON_RAILS_PRO_LICENSE = expiredToken ;
115+ mockLicenseEnv ( expiredToken ) ;
100116
101117 const module = jest . requireActual < LicenseValidatorModule > ( '../src/shared/licenseValidator' ) ;
102118
@@ -117,7 +133,7 @@ describe('LicenseValidator', () => {
117133 } ;
118134
119135 const tokenWithoutExp = jwt . sign ( payloadWithoutExp , testPrivateKey , { algorithm : 'RS256' } ) ;
120- process . env . REACT_ON_RAILS_PRO_LICENSE = tokenWithoutExp ;
136+ mockLicenseEnv ( tokenWithoutExp ) ;
121137
122138 const module = jest . requireActual < LicenseValidatorModule > ( '../src/shared/licenseValidator' ) ;
123139
@@ -152,7 +168,7 @@ describe('LicenseValidator', () => {
152168 } ;
153169
154170 const invalidToken = jwt . sign ( validPayload , wrongKey , { algorithm : 'RS256' } ) ;
155- process . env . REACT_ON_RAILS_PRO_LICENSE = invalidToken ;
171+ mockLicenseEnv ( invalidToken ) ;
156172
157173 const module = jest . requireActual < LicenseValidatorModule > ( '../src/shared/licenseValidator' ) ;
158174
@@ -164,7 +180,7 @@ describe('LicenseValidator', () => {
164180 } ) ;
165181
166182 it ( 'calls process.exit for missing license' , ( ) => {
167- delete process . env . REACT_ON_RAILS_PRO_LICENSE ;
183+ mockLicenseEnv ( undefined ) ;
168184
169185 // Mock fs.existsSync to return false (no config file)
170186 jest . mocked ( fs . existsSync ) . mockReturnValue ( false ) ;
@@ -178,7 +194,7 @@ describe('LicenseValidator', () => {
178194 expect ( mockConsoleError ) . toHaveBeenCalledWith ( expect . stringContaining ( 'FREE evaluation license' ) ) ;
179195 } ) ;
180196
181- it ( 'loads license from config file when ENV not set ' , ( ) => {
197+ it ( 'validates license from ENV variable after reset ' , ( ) => {
182198 const validPayload = {
183199184200 iat : Math . floor ( Date . now ( ) / 1000 ) ,
@@ -189,7 +205,7 @@ describe('LicenseValidator', () => {
189205
190206 // Set the license in ENV variable instead of file
191207 // (file-based testing is complex due to module caching)
192- process . env . REACT_ON_RAILS_PRO_LICENSE = validToken ;
208+ mockLicenseEnv ( validToken ) ;
193209
194210 const module = jest . requireActual < LicenseValidatorModule > ( '../src/shared/licenseValidator' ) ;
195211
@@ -208,15 +224,15 @@ describe('LicenseValidator', () => {
208224 } ;
209225
210226 const validToken = jwt . sign ( validPayload , testPrivateKey , { algorithm : 'RS256' } ) ;
211- process . env . REACT_ON_RAILS_PRO_LICENSE = validToken ;
227+ mockLicenseEnv ( validToken ) ;
212228
213229 const module = jest . requireActual < LicenseValidatorModule > ( '../src/shared/licenseValidator' ) ;
214230
215231 // First call
216232 expect ( module . validateLicense ( ) ) . toBe ( true ) ;
217233
218234 // Change ENV (shouldn't affect cached result)
219- delete process . env . REACT_ON_RAILS_PRO_LICENSE ;
235+ mockLicenseEnv ( undefined ) ;
220236
221237 // Second call should use cache
222238 expect ( module . validateLicense ( ) ) . toBe ( true ) ;
@@ -233,7 +249,7 @@ describe('LicenseValidator', () => {
233249 } ;
234250
235251 const validToken = jwt . sign ( payload , testPrivateKey , { algorithm : 'RS256' } ) ;
236- process . env . REACT_ON_RAILS_PRO_LICENSE = validToken ;
252+ mockLicenseEnv ( validToken ) ;
237253
238254 const module = jest . requireActual < LicenseValidatorModule > ( '../src/shared/licenseValidator' ) ;
239255 const data = module . getLicenseData ( ) ;
@@ -253,7 +269,7 @@ describe('LicenseValidator', () => {
253269 } ;
254270
255271 const expiredToken = jwt . sign ( expiredPayload , testPrivateKey , { algorithm : 'RS256' } ) ;
256- process . env . REACT_ON_RAILS_PRO_LICENSE = expiredToken ;
272+ mockLicenseEnv ( expiredToken ) ;
257273
258274 const module = jest . requireActual < LicenseValidatorModule > ( '../src/shared/licenseValidator' ) ;
259275
0 commit comments