Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

182
Views
Do I need to unsubscribe from subscriptions in my unit tests?

If I have a test such as the following:

it('should return some observable', async(() => {
  mockBackend.connections.subscribe((mockConnection: MockConnection) => {
    const responseOptions = new ResponseOptions({
      body: JSON.stringify(/* some response */)
    });

    mockConnection.mockRespond(new Response(responseOptions));
  });

  service.getSomeObservable()
    .subscribe(result => {
      expect(result).toBe(/* expected response */);
    });
}));

Do I need to unsubscribe from the subscription in an afterEach or afterAll block to avoid memory issues? Or will it be automatically removed?

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

@estus is correct, you do not need to unsubscribe.

tl;dr;

  • Observables/Subjects hold refererences to all subscribers until complete.
  • Objects with active references cannot be marked for Garbage collection. Basic JS.
  • Http Observables do complete, disgarding subscriptions at that time, and freeing them up for GC.

  • Until the Observable/Subject gets torn down or completes, no subscription can be GC'd unless you unsubscribe.

Consider this simple Angular TestBed setup:

describe(`DataService`, () => {
  let service: DataService;
  let httpMock: HttpTestingController;

  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [HttpClientTestingModule],
      providers: [
        DataService,
      ]
    });

    service = TestBed.get(DataService);
    httpMock = TestBed.get(HttpTestingController);
  });

  it(`should get a response`, () => {
    service.getData().subscribe(response => {
      expect(response).toBeTruthy();
    });
  });

In the case of an Http request via the Angular HttpClient, the Observable will complete, and the references to Subscribers will be released for GC. In the case of general Angular Jasmine Testing as shown above--even if you had an Observable that didn't complete--the DataService object being referenced by service gets reset every time beforeEach is called, and the old DataService object will have lost all references and is marked for GC. Once that happens, any subscriptions referenced by the Observable/Subject in the DataService will lose references to them, and they'll be GC'd as well.

Remember, memory leaks happen with Observables because they retain references to all Subscribers and unless the Subscriber unsubscribes. They will continue to have valid references to them and their containing functions, and cannot be marked for GC until The Subject/Observable is destroyed. If the Subject continues to live and you keep adding subscribers--such as new instance of the same Subscriber--you will keep adding a bunch of old, unused objects to memory.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!