Proxyquire calling original file instead of stub

Issue

Hi I’m trying to mock a Model inside my controller using proxyquire. But for some reason when a try to inject a mock, the original file is being called.

This is how my model is being required inside the controller:

var Product = require('./product.model');

And this is how I’, trying to stub the model

  var proxyquire = require('proxyquire').noCallThru().noPreserveCache();
  var productsStub = {findAsync: sinon.stub()};
  productsStub.findAsync.resolves(mockProducts);
  var mockProducts = [{_id:'0001',title: 'title', price: 123, description: 'le description'}];
  var productController = proxyquire('./product.controller',{'./product.model':productsStub});

but when I run my tests I get this error

Running "mochaTest:unit" (mochaTest) task
>> Mocha exploded!
>> OverwriteModelError: Cannot overwrite `Product` model once compiled.
>>     at Mongoose.model (/Users/victor/Documents/projects/estudo/mean/meanshop/node_modules/mongoose/lib/index.js:360:13)
>>     at Object.<anonymous> (/Users/victor/Documents/projects/estudo/mean/meanshop/server/api/product/product.model.js:15:27)
>>     at Module._compile (module.js:409:26)
>>     at normalLoader (/Users/victor/Documents/projects/estudo/mean/meanshop/node_modules/babel-core/lib/api/register/node.js:199:5)
>>     at Object.require.extensions.(anonymous function) [as .js] (/Users/victor/Documents/projects/estudo/mean/meanshop/node_modules/babel-core/lib/api/register/node.js:216:7)
>>     at Module.load (module.js:343:32)
>>     at Function.Module._load (module.js:300:12)
>>     at Module.require (module.js:353:17)
>>     at require (internal/module.js:12:17)
>>     at Object.<anonymous> (/Users/victor/Documents/projects/estudo/mean/meanshop/server/api/product/product.model.spec.js:1:15)
>>     at Module._compile (module.js:409:26)
>>     at normalLoader (/Users/victor/Documents/projects/estudo/mean/meanshop/node_modules/babel-core/lib/api/register/node.js:199:5)
>>     at Object.require.extensions.(anonymous function) [as .js] (/Users/victor/Documents/projects/estudo/mean/meanshop/node_modules/babel-core/lib/api/register/node.js:216:7)
>>     at Module.load (module.js:343:32)
>>     at Function.Module._load (module.js:300:12)
>>     at Module.require (module.js:353:17)
>>     at require (internal/module.js:12:17)
>>     at /Users/victor/Documents/projects/estudo/mean/meanshop/node_modules/mocha/lib/mocha.js:220:27
>>     at Array.forEach (native)
>>     at Mocha.loadFiles (/Users/victor/Documents/projects/estudo/mean/meanshop/node_modules/mocha/lib/mocha.js:217:14)
>>     at MochaWrapper.run (/Users/victor/Documents/projects/estudo/mean/meanshop/node_modules/grunt-mocha-test/tasks/lib/MochaWrapper.js:51:15)
>>     at /Users/victor/Documents/projects/estudo/mean/meanshop/node_modules/grunt-mocha-test/tasks/mocha-test.js:86:20

My question is why I am getting this if in theory the original products.model.js module will not be called.

Solution

proxyquire does NOT stub not listed modules and they will be required as usual, so
it looks like you have more dependencies inside product.controller and they all are being normally required.

I can’t find nice solution for this problem except providing full stub for proxyquire, e.g. add stubs for all files that required from directly imported files.

Answered By – zamuka

Answer Checked By – Candace Johnson (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.