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

More accurate subscriber for observe helper is added #65

Closed sgrishchenko closed 4 years ago

sgrishchenko commented 4 years ago

It prevents appearance of successful test with fails in finalize operator. When you try add some assertions in finalize operator, fails on this assertions lead to succeful tests with big warning in console. For example, it looks like this in Jest. Test:

test(
  "it should fail on assertions in finalize operator",
  observe(() => {
    expect.assertions(1);
    const mock = jest.fn();
    return of("fail").pipe(
      tap(() => mock()),
      finalize(() => expect(mock).not.toHaveBeenCalled())
    );
  })
);

Output:

    ✓ it should fail on assertions in finalize operator (10ms)

console.warn node_modules/rxjs/internal/Observable.js:55
    Error {
      message: '1 errors occurred during unsubscription:\n' +
        '1) Error: \u001b[2mexpect(\u001b[22m\u001b[31mjest.fn()\u001b[39m\u001b[2m).\u001b[22mnot\u001b[2m.\u001b[22mtoHaveBeenCalled\u001b[2m()\u001b[22m\n' +
        '\n' +
        'Expected number of calls: \u001b[32m0\u001b[39m\n' +
        'Received number of calls: \u001b[31m1\u001b[39m\n' +
        '\n' +
        '1: called with 0 arguments',
      name: 'UnsubscriptionError',
      errors: [
        JestAssertionError: expect(jest.fn()).not.toHaveBeenCalled()

        Expected number of calls: 0
        Received number of calls: 1

        1: called with 0 arguments
            at Subscription._unsubscribe {
          matcherResult: [Object]
        }
      ]
    }

After changes made in this PR, this code leads to obvious test failure. It is linked with this code in RxJS. The reason of this behaviour is that stream already closed when we throw error.

cartant commented 4 years ago

rxjs-marbles@5.0.4 has been published.