3

I am trying to figure out how to structure a jest unit test for the following:

I have a bunch of existing observables and subjects I want to test in a project. So I have started off with what I would assume to be a simple test case using RXjs marbles inside jest:

      const testScheduler = new TestScheduler((actual, expected) => {
        expect(actual).toEqual(expected);
      });

      testScheduler.run(({cold, expectObservable}) => {
        const inputSubject = new Subject();
        const outputSubject = new Subject();
        inputSubject.pipe(map(val => val * 2)).subscribe(outputSubject);

        cold('a|', {a:1}).subscribe(inputSubject);

        expectObservable(outputSubject).toBe('a|', {a:2});
      });
    }

While this test does run, it fails. I am having trouble understanding the output from the test run. I would like to know if I am doing something wrong, or if I am misunderstanding things in general incorrectly. Or perhaps just some explanation of the output would be helpful.

    - Expected
    + Received

    @@ -1,16 +1,7 @@
      Array [
        Object {
    -     "frame": 0,
    -     "notification": Notification {
    -       "error": undefined,
    -       "hasValue": true,
    -       "kind": "N",
    -       "value": 2,
    -     },
    -   },
    -   Object {
          "frame": 1,
          "notification": Notification {
            "error": undefined,
            "hasValue": false,
            "kind": "C",

...... the code ......

      at TestScheduler.assertDeepEqual (common/chat/newMessages/ratifiedSendMessages/twilioRatifiedSendMessages.unit.test.js:27:24)
      at node_modules/rxjs/src/internal/testing/TestScheduler.ts:159:14
          at Array.filter (<anonymous>)
      at TestScheduler.Object.<anonymous>.TestScheduler.flush (node_modules/rxjs/src/internal/testing/TestScheduler.ts:157:39)
      at TestScheduler.run (node_modules/rxjs/src/internal/testing/TestScheduler.ts:392:12)
      at Object.<anonymous> (common/chat/newMessages/ratifiedSendMessages/twilioRatifiedSendMessages.unit.test.js:30:21)
skyboyer
  • 22,209
  • 7
  • 57
  • 64
user2765977
  • 491
  • 4
  • 18

1 Answers1

1

NVM. Looks like I was supposed to use 'hot' not 'cold'. As soon as I switched the 'cold' call out for 'hot' instead, it worked fine. Although if anyone else feel like explaining why this is, I am all ears.

user2765977
  • 491
  • 4
  • 18