Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

251
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda