RxJS: Don’t Unsubscribe
Ben Lesh

I found takeUntil() useful in tests to make sure the streams have completed so that different tests don’t interfere with each other.

For example, if you have streams listening to global events (like keyboard events), it’s quite easy to accidentally write tests so that the streams from previous tests are still active.

Without takeUntil() the following test set would fail. The “yields B” test would fail because the subscriber in “yields A” test is still listening to the keyup events.

const stopTest$ = new Rx.Subject();
afterEach(() => {
const uppercaseKey = () => {
return Rx.Observable.fromEvent(document, 'keyup')
.map(key => key.toUpperCase())

it('yields A', (done) => {
.do(value => expect(value).toBe('A'))
.subscribe(done, done.fail);
document.dispatchEvent(new KeyboardEvent('keyup', { key: 'a' }));

it('yields B', (done) => {
.do(value => expect(value).toBe('B'))
.subscribe(done, done.fail);
document.dispatchEvent(new KeyboardEvent('keyup', { key: 'b' }));
One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.