File tree Expand file tree Collapse file tree 2 files changed +15
-19
lines changed
internal/test_runner/mock Expand file tree Collapse file tree 2 files changed +15
-19
lines changed Original file line number Diff line number Diff line change @@ -707,14 +707,15 @@ class MockTracker {
707707 validateStringOrSymbol ( propertyName , 'propertyName' ) ;
708708
709709 if ( object === process . env ) {
710- Object . defineProperty ( object , propertyName , {
711- value,
712- writable : true ,
713- enumerable : true ,
714- configurable : true
715- } ) ;
716- return object ;
717- }
710+
711+ const originalValue = object [ propertyName ] ;
712+ object [ propertyName ] = value ;
713+
714+ return new ProxyConstructor ( { restore : ( ) => {
715+ object [ propertyName ] = originalValue ;
716+ } } ) ;
717+ }
718+
718719
719720 const ctx = arguments . length > 2 ?
720721 new MockPropertyContext ( object , propertyName , value ) :
Original file line number Diff line number Diff line change 11'use strict' ;
22
3- // Flags: --expose-internals
43require ( '../common' ) ;
54const assert = require ( 'assert' ) ;
5+ const { mock } = require ( 'node:test' ) ;
66
7- // This test verifies mocking and restoring process.env.TEST_ENV
7+ console . log ( 'Testing mock.property() with process.env...' ) ;
88
99const hadOriginal = Object . prototype . hasOwnProperty . call ( process . env , 'TEST_ENV' ) ;
1010const original = process . env . TEST_ENV ;
1111
12- // Mock environment variable
13- process . env . TEST_ENV = 'mocked' ;
12+
13+ const tracker = mock . property ( process . env , 'TEST_ENV' , 'mocked' ) ;
14+
1415assert . strictEqual ( process . env . TEST_ENV , 'mocked' ) ;
1516console . log ( 'Mocked process.env.TEST_ENV successfully' ) ;
1617
17- // Restore original variable
18- if ( hadOriginal ) {
19- process . env . TEST_ENV = original ;
20- } else {
21- delete process . env . TEST_ENV ;
22- }
23-
18+ tracker . restore ( ) ;
2419
2520if ( hadOriginal ) {
2621 assert . strictEqual ( process . env . TEST_ENV , original ) ;
You can’t perform that action at this time.
0 commit comments