thlorenz / proxyquire

🔮 Proxies nodejs require in order to allow overriding dependencies during testing.
MIT License
2.75k stars 100 forks source link

Dependency path hasn't been resolved #129

Closed tamtakoe closed 8 years ago

tamtakoe commented 8 years ago

smth.js

const utils = require('../utils/index');
//... module code

smth.test.js

const smth = proxyquire('./modules/smth/index', {
    '../utils/index': utils
});
//... test code

It works. But if I change proxyquire dependency path for './../utils/index': utils or '../utils/../utils/index': utils it doesn't work, because path hasn't been resolved

bendrucker commented 8 years ago

Proxyquire isn't doing anything fancy with your paths. It expects exact matches between your require calls and your stub objects.

https://github.com/thlorenz/proxyquire/blob/572b4c9b84c760462a0af47ba18cec27cfcec901/lib/proxyquire.js#L144

tamtakoe commented 8 years ago

It is strange, because node.js always resolves paths and a developer expects the same behavior from node.js modules.

The second strange thing that I encountered is merging native module with mock module. F.e. If I have

module.js

module.exports = {a: 1, b: 1};

module.mock.js

module.exports = {a: 2};

index.js

var module = require('./module');
console.log(module);

index.test.js

proxyquire('./index', {
    './module': require('./module.mock')
});

I will get in console:

{a: 2, b: 1};

It is very strange behavior. Of course I will get error if module.js contains dependencies which can't be resolved with test framework. Every adequate developer expects that his mock will change particular dependency and original module will never be launched.

Unfortunately proxyquire can't be used for tests now :(