Un'altra soluzione è spyOn il modello prototype funzioni.
Ad esempio, questo renderà MyModel.save() fallire :
jest.spyOn(MyModel.prototype, 'save')
.mockImplementationOnce(() => Promise.reject('fail update'))
Puoi usare mockImplementationOnce di non dover mockRestore la spia. Ma puoi anche usare mockImplementation e usa qualcosa come :
afterEach(() => {
jest.restoreAllMocks()
})
Testato con "mongoose": "^4.11.7" e "jest": "^23.6.0" .