Fix: Allow mock.property() to work with process.env variables#60499
Fix: Allow mock.property() to work with process.env variables#60499sakina1303 wants to merge 4 commits intonodejs:mainfrom
Conversation
|
Review requested:
|
|
The The 🤨 |
|
Thanks for catching that, @regseb! I’ve updated the implementation so that the process.env case now correctly returns a ProxyConstructor instead of the environment object. Also cleaned up the test file to properly verify both the mocking and restoring behavior using mock.property(). Please have a look at the latest changes and let me know if any further improvements are needed. |
| validateObject(object, 'object'); | ||
| validateStringOrSymbol(propertyName, 'propertyName'); | ||
|
|
||
| if (object === process.env) { |
There was a problem hiding this comment.
This seems very specific to process.env. What does it make this object special that prevents mocking? Can we have a generic system?
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #60499 +/- ##
==========================================
+ Coverage 88.57% 88.58% +0.01%
==========================================
Files 704 704
Lines 207828 207868 +40
Branches 40040 40052 +12
==========================================
+ Hits 184079 184138 +59
+ Misses 15802 15766 -36
- Partials 7947 7964 +17
🚀 New features to boost your workflow:
|
Fix: Allow
mock.property()to work withprocess.envvariablesDescription
This PR fixes a
TypeErrorthat occurs when usingmock.property()from thenode:testmodule onprocess.envproperties.Error before:
TypeError: 'process.env' does not accept an accessor(getter/setter) descriptor
Fix:
When the target is
process.env,mock.property()now performs a direct value assignment instead of using an accessor descriptor.This ensures environment variables can be safely mocked and restored without throwing errors.
Reproduction Steps
With this fix:
{ value: '/usr/local/bin:...', writable: true, enumerable: true, configurable: true }
foo
Tests Added:
Added a new test file:
test/parallel/test-env-mock-process-env.js
The test validates:
Example output:
Mocked process.env.TEST_ENV successfully
Restored original process.env.TEST_ENV
Checklist:
Developer’s Certificate of Origin 1.1
By making a contribution to this project, I certify that:
(a) The contribution was created in whole or in part by me and I
have the right to submit it under the open source license indicated in the file; or
(b) The contribution is based upon previous work that, to the best of my knowledge, is covered under an appropriate open source license; or
(c) The contribution was provided directly to me by another person who certified (a), (b), or (c); and
(d) I understand and agree that this project and the contribution are public and that a record of the contribution (including my sign-off) is maintained indefinitely.
fixes #60486