Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TestScheduler expectObservable not respecting subscription marbles when using toEqual #7399

Closed
djonestru opened this issue Dec 5, 2023 · 0 comments · Fixed by #7403
Closed
Assignees
Labels
bug Confirmed bug

Comments

@djonestru
Copy link

Describe the bug

When using expectObservable from the TestScheduler with subscription marbles:

const actual = createTestObservable();
const expected = createExpectedObservable();
expectObservable(actual,  '^---!').toEqual(expected);

When the ! marble is scheduled, only the expected observable will be unsubscribed and not the actual observable. This seems to be because the implementation shares the same subscription variable, so when expected is subscribed it overwrites the subscription from actual. Which means any custom teardown logic won't be run for actual.

Expected behavior

I'd expect the following asserts to be synonymous:

expectObservable(actual,  '^---!').toBe('1234');
expectObservable(actual,  '^---!').toEqual(cold('1234'));

Reproduction code

import { TestScheduler } from 'rxjs/testing';
import { isEqual } from 'lodash';
import { Observable } from 'rxjs';

describe('TestScheduler', () => {
  let testScheduler: TestScheduler;
  let disposed = false;

  beforeEach(() => {
    testScheduler = new TestScheduler(isEqual);
    disposed = false;
  });

  it('should unsubscribe toBe', () => {
    testScheduler.run(({ expectObservable }) => {
      expectObservable(
        new Observable((_) => () => (disposed = true)),
        '^!'
      ).toBe('');
    });
    expect(disposed).toBeTruthy();
  });

  // This fails.
  it('should unsubscribe toEqual', () => {
    testScheduler.run(({ cold, expectObservable }) => {
      expectObservable(
        new Observable((_) => () => (disposed = true)),
        '^!'
      ).toEqual(cold(''));
    });
    expect(disposed).toBeTruthy();
  });
});

Reproduction URL

https://stackblitz.com/edit/stackblitz-starters-bs79vc?file=TestScheduler.spec.ts

Version

8.0.0-alpha.12 and 7.8.1

Environment

No response

Additional context

No response

@benlesh benlesh self-assigned this Dec 11, 2023
@benlesh benlesh added the bug Confirmed bug label Dec 11, 2023
benlesh added a commit that referenced this issue Dec 12, 2023
…7403)

* fix(TestScheduler): explicit unsubscribe works properly with toEqual

Fixes #7399

* chore: clean up tests, add expected observable test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Confirmed bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants