1

When I check if my observables are emitting what I need my unit tests. Should I unsubscribe inside my test?

I have always seen stuff like this:

it('my test should return 10', fakeAsync(() => {
  let valueObtained;
  sut.myObservable$.subscribe(newValue => {
    valueObtained = newValue;
  });

  sut.act();
  tick();
  expect(valueObtained).toBe(10)
});

should I use there after the expect an .unsubscribe ?

skyboyer
  • 22,209
  • 7
  • 57
  • 64
distante
  • 6,438
  • 6
  • 48
  • 90
  • Does it cause you any problems? If you don't unsubscribe, you may run into memory leak problems, however I doubt this would be a problem with tests since they run and close the browser instance. – Bunyamin Coskuner Jul 22 '19 at 05:43
  • No that I found. But since jest does not use a browser instance and the project where I work has more than 1000 suites I am now curious. – distante Jul 22 '19 at 06:09
  • Possible duplicate of [Do I need to unsubscribe from subscriptions in my unit tests?](https://stackoverflow.com/questions/43312433/do-i-need-to-unsubscribe-from-subscriptions-in-my-unit-tests) – Християн Христов Jul 22 '19 at 07:07
  • 1
    @ХристиянХристов if the same applies to jest instead of Karma then yes, the question is the same. – distante Jul 22 '19 at 09:50

0 Answers0