thlorenz/proxyquire

Proxyquire not working with node@16?

ericmorand opened this issue · 1 comments

Hi team,

I gave proxyquire a try and since I'm not sure it is maintained anymore, I don't know if I'm doing something wrong or if proxyquire doesn't work with node.js 16.

Here are two modules:

  • foo.js
module.exports = {
    bar: require('./bar')
};
  • bar.js
module.exports = () => 'bar';
  • index.js
const proxy = require('proxyquire');

proxy('./foo.js', {
    './bar': () => 'replaced'
})

const {bar} = require('./foo');

console.log(bar()); // log "bar" instead of "replaced"

Executing node index.js logs "bar" instead of "replaced" which proves that the bar.js module was required instead of the passed stub.

Is this a known issue?

Making a note to spend some time converting to GH Actions so tests can be kept up to date.

That said, your usage is incorrect, no indication Node 16 is involved here. Testing on Node 14 would have been a nice contribution.

https://github.com/thlorenz/proxyquire#example

proxyquire returns the specified module with the provided overrides. Unless preserveCache is called, which should generally be avoided since it breaks the encapsulation of your test, calling proxyquire does not have side effects on future require calls.