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

798 Views Asked by At

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?

2

There are 2 best solutions below

0
OJ Kwon On

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.

0
Gerard Sans On

You just forgot to complete your Observable.

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

can be refactored to

  const expected = "4|";