Closed dhensby closed 1 year ago
Damn, that sucks. Didn't know Node does this :)
Assignments to properties using the dot notation are discouraged (as noted in the caveats section) as they mutate the actual process
object (you could alternatively just override process.env
in your test).
Does it work for you if you call __set__
like this?
const revert = rewiredModule.__set__({
process: {
...process,
env: {
...process.env,
MY_ENV: "my value"
}
}
});
I hadn't read the caveats, so hadn't seen that recommendation 🤦♂️
That is essentially how I modify the env object now, so if that's the right approach, then we can just close this. Cheers.
When trying to set
process.env
values with rewire the revert attempts to roll-back the changes by settingprocess.env[var] = undefined
. However, Node seems to perform atoString()
manipulation on any value set on the.env
props meaning that this turns a previously undefined environment variable into "undefined".eg:
Would it be prudent to manually handle environment variables, either by manually running
delete process.env[name]
on any assignments the library sees or (a bit more lazy approach) automatically reset the env vars on every revert.ie: every
__set__
call caches the env vars values at that time and just pushes a special assignment in the revert?