cartant / rxjs-marbles

An RxJS marble testing library for any test framework
https://cartant.github.io/rxjs-marbles/
MIT License
301 stars 18 forks source link

What is the difference berween of() and m.cold()? #60

Closed olessavluk closed 5 years ago

olessavluk commented 5 years ago

Hello,

I have the following test (example repo with it https://github.com/olessavluk/rxmarbles-issue/blob/master/src/marble.test.js):

it(
  "renders without crashing",
  marbles(m => {
    const expected$ = m.cold("(r|)", { r: [1, 2] });

    const two$ = m.cold("t", { t: 2 }); // of(2);
    const res$ = of(1).pipe(withLatestFrom(two$));

    m.expect(res$).toBeObservable(expected$);
  })
);

When I use of(2) it works fine, but when I use m.cold result is never returned.

Why is that, aren't they supposed to behave in the same way?

cartant commented 5 years ago

Your two$ observable is not equivalent to of as it doesn't complete. Was the cold observable you replaced with of(1) similar? That is, did it complete?

olessavluk commented 5 years ago

Yes, I have tried to make it completed, this didn't helped (updated example repo to include both cases). Take a looks at this two examples:

  1. when using of() works fine and return [1, 2]:

    const two$ = of(2);
    const res$ = of(1).pipe(withLatestFrom(two$));
  2. when using m.cold it does not return anything:

    const two$ = m.cold("(t|)", { t: 2 });
    const res$ = of(1).pipe(withLatestFrom(two$));

Is this a bug or a proper behavior?

cartant commented 5 years ago

The behavioural difference will likely be down to your source observable being a synchronous, non-TestScheduler-managed observable. I suspect if you change your source from of(1) to be cold("(1|)") it will be fine. In general, I make sure to use either a cold or hold observable as the source.