question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Calling ngOnDestroy on test does not complete subscription

See original GitHub issue
"@angular/core": "9.0.2",
"rxjs": "6.5.4",
"@ngneat/until-destroy": "7.1.1",
"jasmine-core": "3.5.0",

On a component defined as

@UntilDestroy()
@Component({
  templateUrl: 'none-improvement.html',
  selector: 'cv-none-improvement-page',
  styleUrls: ['none-improvement.scss']
})
export class NoneImprovementPageComponent implements OnInit, OnDestroy {
  public firstName: string;

  constructor(
    private _user: UserProfileStore,
  ) {
  }

  public ngOnInit() {
    this._user.profile$
      .pipe(
        untilDestroyed(this)
      )
      .subscribe(u => this.firstName = u.get('firstName'));
  }

  public ngOnDestroy() {
    //
  }
}

I write this test:

  it('stops getting first name from user profile after destroy', () => {
    expect(sut.firstName).toBeUndefined();
    sut.ngOnInit();
    mockUserProfileStore.profileSubject$.next(new UserProfile(<any>{ firstName: 'testName' }).toImmutable());
    expect(sut.firstName).toEqual('testName');
    sut.ngOnDestroy();
    mockUserProfileStore.profileSubject$.next(new UserProfile(<any>{ firstName: 'newName' }).toImmutable());
    expect(sut.firstName).toEqual('testName');
  });

which fails. But if I use untilDestroyed(this, 'ngOnDestroy') passes. Is ngOnDestroy not the default method that triggers the unsubscription?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:16 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
arturovtcommented, Mar 17, 2020

I tested, it works for me… If you don’t provide any reproduction then I see no reason to continue the dialogue, because it’s like “shooting a stone”. 😉

1reaction
masimplocommented, Mar 3, 2020

Well, as many other teams, we are testing many components (those that have no need to test UI) in unit tests without using Testbed. The component is instantiated manually using new and triggering the lifecycles hooks on demand. The component does have ngOnDestroy defined.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to unit test unsubscribe function in angular
I figure out a way to make it work. First of all, I do not create a subscription instance for each subscription, I...
Read more >
Unsubscribing in Angular, the right way | by Ashwin Sathian
All we do in this method is calling unsubscribe on the Subscription if it's defined, as and when the component is destroyed. Simple,...
Read more >
Best practices and patterns
The best way to work with subscription properties is to initialize it with an empty subscription so you do not need to check...
Read more >
Angular – Auto-unsubscribe for RxJS | by Ildar Timerbaev
What does it mean? With this subscription, we can't say that the current state is valid because it can be changed from an...
Read more >
Angular: Managing RxJS Observable Subscriptions with ...
Approach Three: ngneat/until-destroy. If you haven't come across this operator yet, you definitely need to check out this repo. It's called ngneat/until-destroy ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found