ts-mockito
ts-mockito copied to clipboard
thenReject needs Type Error breaking change
Since Version 2.4.0 thenReject returns an Error and expecting an Error, but now tests like this are failing if the ErrorEntity is not like the Error (Property 'name' is missing in type 'ErrorEntity' but required in type 'Error'):
Some Class:
registerAccount(mail: string, password: string) {
return new Promise<RegistrationEntity>((resolve, reject) => {
this.MyService.makeRequest(request).then(() => {
resolve(new RegistrationEntity());
}, (error) => {
reject(new ErrorEntity('some Error'));
});
});
}
The test:
const errorEntity = instance(mock(ErrorEntity));
when(mockedRegistration.registerAccount('mail', 'password')).thenReject(errorEntity);
Here: https://github.com/NagRock/ts-mockito/blob/v2.4.0/src/MethodStubSetter.ts
Its a really bad way to pretend, that every rejection has to be an Error Object.
According to this MS Typescript issue #7588 (If I follow it correctly) reject should support any type and it looks like it's only typed to Error in the MethodStubSetter (i.e. RejectPromiseMethodStub seems OK with any).
I'm new here, so I might be missing something. Is there a benefit to requiring thenReject to receive a type of Error?
No there isn't any benefit, The restriction came with Version 2.4.0. It would be great to support any as rejection type. @NagRock
@NagRock any news on this one?
We rely heavily on rejecting with any types. This issue prevents us from updating to the latest ts-mockito version which now includes a needed bugfix.
I fixed this issue in #199. @NagRock please have a look at my first PR to ts-mockito 😀