jhnns / rewire

Easy monkey-patching for node.js unit tests
MIT License
3.08k stars 128 forks source link

troubles injecting an exported function #157

Open jcotero opened 5 years ago

jcotero commented 5 years ago

Maybe I'm doing something wrong, I don't know... I'm trying to make an injection, but it seems it's not working.

I tried Stackoverflow before with no success: https://stackoverflow.com/questions/54443582/rewire-mocha-how-to-inject-a-function

So I'm trying to inject a mocked function in a module using Rewire. I have a LotsRepo module with

const _incLot = async (params) => {
   // ...
}
exports.incLot = _incLot

Then, following this example, in a Mocha test I'm using:

let rewire = require('rewire')
var injections = {
    _incLot: async (params) => {
        throw new Error('mocked: findOneAndUpdate fake error!')
    }
}
const rewiredLotsRepo = rewire('./lotsRepo.js', null, injections)

And later, on an 'it' part, I'm calling:

let lot = await rewiredLotsRepo.incLot(params)

I was expecting to get the error. But instead, it's accessing the original incLot. What am I doing wrong?