ts-mockito icon indicating copy to clipboard operation
ts-mockito copied to clipboard

thenReject needs Type Error breaking change

Open Larsrdev opened this issue 6 years ago • 4 comments

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.

Larsrdev avatar Jul 23 '19 13:07 Larsrdev

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?

mousedownmike avatar Aug 06 '19 21:08 mousedownmike

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

Larsrdev avatar Feb 17 '20 16:02 Larsrdev

@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.

cornr avatar Jul 24 '20 14:07 cornr

I fixed this issue in #199. @NagRock please have a look at my first PR to ts-mockito 😀

cornr avatar Jul 24 '20 15:07 cornr