Recursive noPreserveCache: deps should also be reloaded
axelpale opened this issue · 0 comments
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':
const getPort = require('getPort')
const host = process.env.HOST
module.exports = () => { return host + getPort() }
Next, here is a unit test suite:
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.