1

I'm trying to stub a constructor in node but I really can't.

I found this , that is quite similiar to what I need to do but I have an error that I could not solve.

//file.js
var foo = require('foo-client')
function toTest () {
     var bar = foo()
    returns config = bar.foo2(a,b)  // returns a Promise
}

what I am trying to do in the test file is

//file-sepc.js
var stub = sinon.stub()
stub.returns(Promise.resolve('config'))// config it's just an example

    var file = proxyquire('./file.js', {
        'foo-client':{foo2: stub}
    })
file.toTest()
.then((result) => {
     console.log(result)
     done()
})

supposing the node syntax is correct, I am getting this output:

 TypeError: foo is not a function

Can anyone help me telling me where is my error or another way to mock/stub this things?

Thanks a lot!

Julian Mendez
  • 3,162
  • 2
  • 13
  • 36

1 Answers1

0

Haven't tried running your code but it looks like foo-client should be a function rather than an object in order for the var bar = foo() not to throw an error you are seeing. Try the following:

var file = proxyquire('./file.js', {'foo-client': sinon.stub.returns({ foo2: stub }) })
Lisa Gagarina
  • 693
  • 5
  • 7