NagRock / ts-mockito

Mocking library for TypeScript
MIT License
974 stars 93 forks source link

thenReject() seems to not be working when returning Promise.resolve()/.reject() #145

Closed tom-mayer closed 5 years ago

tom-mayer commented 5 years ago

Hey

I have a test that looks like this:

it('should reject', async (done) => {
  when(mongoMock.start()).thenReject();
  try{
     await instance(mongoMock).start();
     console.log('fulfilled');
  }catch(err){
     console.log('rejected');
  }
  //....
});

This does not go into the catch block as I would expect. If I substitute the when line with

 when(mongoMock.start()).thenReturn(Promise.reject());

the code works as expected.

The start funciton of the mongo class looks something like this:

protected async startServices(): Promise<void> {
  try {
    // await some async stuff 
    return Promise.resolve();
  }catch(err){
    return Promise.reject();
  }
}
NagRock commented 5 years ago

Should be fixed in 2.3.2 version. thenReject method requires error to be passed to it. But now (in new version) it throws default error if none has been passed.

Thanks for reporting this.