Exercise - Error
Exercise
In this exercise we will create two tests that assert error notifications.
The first test will assert that the source Observable emits an error notification:
- Open exercise on codesandbox.
I've already imported the
TestSchedulerclass and set up the test. - Invoke the
run()method of theTestSchedulerinstance. You'll need thecoldandexpectObservableproperties from theRunHelperobject that is passed to the callback function. - Create a new
sourcecold Observable using thecold()function. The source Observable emits a next notification on frame 100, and then an error notification on frame 200. - Define the
expectedstring that represents the behavior of the source cold Observable. - Use the
expectObservable()function to create the assertion.
The second test will use a source Observable from an interval() that uses the throwError() operator when the value is greater than 0.
Here is the source Observable:
const source = interval(100).pipe(
mergeMap((value) => {
if (value > 0) {
return throwError('error');
}
return of(value);
})
);
Write a unit test for this source Observable.