Proxyquire with a nested dependency
So I've got a test file and 2 modules.
moduleA has a dependency, moduleB
My test file stubs out functionB (returned from moduleB) using proxyquire:
So it outputs 50 (stubbed functionB 30 + functionA 20)
Now I'm trying to take this example into my code:
moduleA in this case is a file called validation.js. It is dependent on moduleB, in this case a sequelize model, Person.
validation.js exports module.exports = { validateLogin };, a function that calls validate, which returns a function that uses Person.findOne()
So in my mind, as with the simple example, I need to create a stub, point to the validation module in proxyquire, and reference the dependency and its findOne function. Like this:
1 Reply
This should stub Person.findOne in validation.js. But it doesn't seem to. And I have no idea why.
And this is my validation.js file:
--------------------------------------------------
Answer
So this:
Is basically the correct code to stub the nested dependency as far as i can see now (it's working, i'm never touching the code again)
My idiocy was here:
stubbedFindOne.resolves();
(or rejects()
) both failed for me
stubbedFindOne.return(null)
was the winner. If you look at the above code, I shouldn't be rejecting a promise - the sequelize method doesn't do that, it returns null
resolves()
was just straight up wrong and I think I was blindly changing code to try and get any different result. rejects()
doesn't work because it jumps to the catch block.