New to rxjs and angular, attempting to write some basic unit tests for a simple rxjs observable with a success / error scenario:
Service method:
export class MyService {
constructor(private http: HttpClient) {}
public getId(id) {
return this.http.get('www.myurl.com/' + id, {
withCredential: true,
responseType: 'json' as 'json'
})
.pipe(
map(response => {
return response;
}),
catchError(() => {
return 'Service returned error';
})
};
}
}
Test:
it('should throw an error',() => {
spyOn(httpClientMock, 'get').and.returnValue(Observable.throw('error));
myService.getId('test1')
.subscribe(
() => {},
error => {
expect(error).toEqual('Service returned error');
}
};
});
The above passes, however, if i change the expect statement to:
expect(error).toEqual('Service returned erro');
The unit test still passes.
If i log out error, i see:
S
e
r
v
i
c
e
r
e
t
u
r
n
e
d
e
r
r
o
r