6

Here is forkJoin operator jasmine marble test:

it('test1', () => {

 const a = cold('---a|', { a: 1 });
 const b = cold('---b|', { b: 2 });

 const observable = forkJoin(
   a,
   b
 );

 const expected = cold('---21');
 expect(observable).toBeObservable(expected);
});

The test produces the following errors:

Expected $[0].frame = 40 to equal 30.
Expected $[0].notification.value = [ 1, 2 ] to equal '2'.
Expected $[1].frame = 40 to equal 50.
Expected $[1].notification.kind = 'C' to equal 'N'.
Expected $[1].notification.value = undefined to equal '1'.
Expected $[1].notification.hasValue = false to equal true.

Could anyone tell me what I am doing wrong?

tsiro
  • 2,323
  • 3
  • 24
  • 46
  • What's `cold` ? Some custom operator ? Never have I seen it. –  May 27 '19 at 14:47
  • Anyway, please provide a [mcve] of your issue –  May 27 '19 at 14:47
  • 1
    https://github.com/ReactiveX/rxjs/blob/master/docs_app/content/guide/testing/marble-testing.md#time-progression-syntax...it is from rxjs-marble-testing – tsiro May 27 '19 at 14:48

1 Answers1

6

First ---21 will wait three frames, then emit '2' and then emit '1. Fork join is not a merge it will emit once, and it will emit [1, 2].

Alphanumeric marbles advance one frame when they emit. So both a and b will complete at frame 4. The forkJoin will then resolve emit. The forkJoin will then immediately complete (also at frame 4).

So the errors you are getting:

Expected $[0].frame = 40 to equal 30. The first item emitted from the fork-join is at time 4 (because a and b complete at time 4) not at time 3.

Expected $[0].notification.value = [ 1, 2 ] to equal '2'. The value emitted is [1, 2] and not '2' as described above.

Expected $[1].frame = 40 to equal 50.
Expected $[1].notification.kind = 'C' to equal 'N'.
Expected $[1].notification.value = undefined to equal '1'.
Expected $[1].notification.hasValue = false to equal true.

You are expecting a '1' at time 5. It is getting a complete at time 4.

So you probably want...

const expected = cold('----(a|)', { a: [1, 2] });
expect(observable).toBeObservable(expected);
Pace
  • 41,875
  • 13
  • 113
  • 156