Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recursive noPreserveCache: deps should also be reloaded #226

Open
axelpale opened this issue Nov 25, 2018 · 0 comments
Open

Recursive noPreserveCache: deps should also be reloaded #226

axelpale opened this issue Nov 25, 2018 · 0 comments

Comments

@axelpale
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant