Dany D
Dany D

Reputation: 1189

RxJS marble test fails when using my own observable instead of creating one with the createHotObservable method

I am trying to create a simple test with RxJS marbles.

I am using mocha and chai.

I am instantiating a new test scheduler and I do not want to use the "testScheduler.createHotObservable method" because I want to use my own Observable, the "Observable.of(4)"

 const testScheduler = new TestScheduler(assert.deepEqual.bind(assert));

      const expected = "a";
      const expectedStateMap = {
        a: 4
      };

      testScheduler.expectObservable(Observable.of(4)).toBe(expected, expectedStateMap);

      testScheduler.flush();

This is the error:

 AssertionError: expected [ Array(2) ] to deeply equal [ Array(1) ]
      + expected - actual

           "notification": {
             "error": [undefined]
             "hasValue": true
             "kind": "N"
      -      "value": 4
      +      "value": "4"
           }
         }
      -  {
      -    "frame": 0
      -    "notification": {
      -      "error": [undefined]
      -      "hasValue": false
      -      "kind": "C"
      -      "value": [undefined]
      -    }
      -  }
       ]

      at TestScheduler.flush (node_modules/rxjs/src/testing/TestScheduler.ts:135:12)

Any ideas what is wrong?

Upvotes: 0

Views: 879

Answers (2)

Gerard Sans
Gerard Sans

Reputation: 2279

You just forgot to complete your Observable.

  const expected = "a";
  const expectedStateMap = {
    a: 4
  };

can be refactored to

  const expected = "4|";

Upvotes: 1

OJ Kwon
OJ Kwon

Reputation: 4641

Yes, it doesn't work. What hot, cold observable creation method does is, creating observable based on given marble and setup internally inside of testscheduler. When testscheduler executes via flush, it iterates all observable and flush. If custom observable is provided, testscheduler doesn't know about existence of those observable and not flush those.

It is just limitation of current testscheduler implementation - unless monkey patch testscheduler to accept custom observable it may not work as expected.

Upvotes: 1

Related Questions