You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am using proxyquire to unit test a module. I test how the module behaves when certain environment variables are absent or invalid. A dependency of the module also relies on some relevant environment variables. For each test case, I change the env vars and call proxyquire on the module. For the module to reload the new env vars, I use var proxyquire = require('proxyquire').noPreserveCache().
The problem: Even though the env vars of the module become reloaded, the env vars of the dependency do not seem to change after the first `proxyquire(...)' call. It looks like that the dependency becomes cached. I expected the deps to be reloaded also.
Consider a minimal example. Here is a locally available module 'getPort':
const port = process.env.PORT
module.exports = () => { return port }
Then, here is a module 'getHost' that depends on 'getPort':
var test = require('tape')
var proxyquire = require('proxyquire').noPreserveCache()
var unit
test('env vars', (t) => {
process.env.PORT = 8888
process.env.HOST = 'localhost'
unit = proxyquire('getHost', {})
t.equal(unit(), 'localhost:8888') // success
process.env.PORT = 8000
process.env.HOST = 'example.com'
unit = proxyquire('getHost', {})
t.equal(unit(), 'example.com:8000')
// fails, "example.com:8888" does not equal "example:8000"
t.end()
})
There seems to be an inconvenient way to hack this through. I had to use the following approach after the first proxyquire call to force also the deps to reload:
unit = proxyquire('getHost', {
'getPort': proxyquire('getPort', {})
})
I hope this issue brings you some insight how people are using the module, in addition to the problem.
The text was updated successfully, but these errors were encountered:
I am using
proxyquire
to unit test a module. I test how the module behaves when certain environment variables are absent or invalid. A dependency of the module also relies on some relevant environment variables. For each test case, I change the env vars and call proxyquire on the module. For the module to reload the new env vars, I usevar proxyquire = require('proxyquire').noPreserveCache()
.The problem: Even though the env vars of the module become reloaded, the env vars of the dependency do not seem to change after the first `proxyquire(...)' call. It looks like that the dependency becomes cached. I expected the deps to be reloaded also.
Consider a minimal example. Here is a locally available module 'getPort':
Then, here is a module 'getHost' that depends on 'getPort':
Next, here is a unit test suite:
There seems to be an inconvenient way to hack this through. I had to use the following approach after the first
proxyquire
call to force also the deps to reload:I hope this issue brings you some insight how people are using the module, in addition to the problem.
The text was updated successfully, but these errors were encountered: