I'm trying to work with simple-git. I need to write some unit tests, and to do so I need to mock out some functions using sinon. The problem I'm having is that my mocks don't propogate into my file under test.
For example, in the file under test, I have this:
const git = require('simple-git/promise')
function func () {
var promise
if (repo_exists()) {
promise = git().silent(true).clone('http://github.com/me/my-repo.git')
} else {
promise = git('my-repo').silent(true).pull('origin','master')
}
promise.then(() => {
// do more stuff
})
}
In my test file, I've tried this:
const git = require('simple-git/promise')()
sinon.stub(git, 'silent').callsFake(() => {
return {
clone: () => {
console.log('~~~~~~~~~~~~~~~~~~~~~~~~~~~')
console.log('calling clone')
console.log('~~~~~~~~~~~~~~~~~~~~~~~~~~~')
return new Promise((resolve, reject) => {
console.log('clone')
resolve()
})
},
pull: () => {
console.log('~~~~~~~~~~~~~~~~~~~~~~~~~~~')
console.log('calling pull')
console.log('~~~~~~~~~~~~~~~~~~~~~~~~~~~')
return new Promise((resolve, reject) => {
console.log('pull')
resolve()
})
}
}
})
But the mocked functions don't get called. I assume the reason is that require('simple-git/promise') returns a function, which itself returns the object containing functions I want to mock, but I don't know how to deal with this.