After I upgraded to new version of Angular, one of my tests that previously worked broke, and I don't know why. Namely I have a function to log errors:
import { Observable, of } from 'rxjs';
export function handleError<T>(operation='operation', result?: T) {
return (error: any): Observable<T> => {
console.error(error);
console.info(`${operation} failed: ${error.message}`);
return of(result as T);
}
}
And I test it with:
it('#handleError return function should return an object', () => {
let errorFunction = handleError('dummyFetch', [{}]);
expect(typeof errorFunction({ message: 'Something went wrong.'})).toEqual('object');
expect(errorFunction({ message: 'Something went wrong.'})).toEqual(of([{}]));
});
The line that fails is expect(errorFunction({ message: 'Something went wrong.'})).toEqual(of([{}]));
and the error reported: Expected $._subscribe = Function to equal Function.
. Could it be that test is failing because of the asynchronous error function?
Edit: This is the solution I settled with:
it('#handleError return function should return an object', () => {
let errorFunction = handleError('dummyFetch', [{}]);
expect(typeof errorFunction({ message: 'Something went wrong.' })).toEqual('object');
let error = errorFunction({ message: 'Something went wrong.' });
error.subscribe(value => {
expect(value).toEqual([{}]);
});
});
If you rewrite your test as
it('#handleError return function should return an object', () => {
let errorFunction = handleError('dummyFetch', [{}]);
expect(typeof errorFunction({ message: 'Something went wrong.'})).toEqual('object');
errorFunction.subscribe((result) => {
expect(result).toEqual([{}]);
});
});
This test failed due to observables and the subscription in your final expect should fix this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With