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.

Cannot use @UntilDestroy on components in unit tests

See original GitHub issue

When unit-testing components decorated with @UntilDestroy this decorator has no effect since at present the JIT compiler is used for unit tests in Angular and not the AOT compiler. As a result the private properties that this decorator relies on don’t exist on the components and our components behave differently under testing scenarios to real-life scenarios.

I note that in your unit tests, you do not use the real @Component annotation, but create the scenario that the AOT compiler uses. Having experimented with the code, I think it’s possible to simply set properties directly on the target in the decorator in JIT mode to overcome this.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:5
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
euangoddardcommented, Feb 14, 2020

I’ve made a sample project with a timer and have been checking whether it continues after the component is destroyed, but it seems to stop. I’m also not getting the error that @PabloDotcom reported which I have seen before. I can’t recall the tests that were breaking in my codebase as I worked around the issue in all cases. If I can locate them I’ll revert my workaround as maybe the final version of Angular has fixed this. Alternatively I might be missing a setting in angular.json since the current project has been upgraded all the way from Angular 4!

I think it would be good for your test suite to use real components rather than making something that looks like what the compiler does to check that the decorator really works in all situations

1reaction
arturovtcommented, Feb 13, 2020

@euangoddard would it be possible to create a reproduction for us? I have a limited amount of time and I would not want to try to reproduce the problem myself.

Read more comments on GitHub >

github_iconTop Results From Across the Web

After upgrading from Angular 7 to Angular 8 my pipe test ...
I am using Jest to unit test my Angular8 components. I performed upgrade from A7 where the tests passed. Now I get an...
Read more >
@ngneat/until-destroy - npm Package Health Analysis | Snyk
RxJS operator that unsubscribes when Angular component is destroyed For more information about how to use this package see README.
Read more >
akita-state-management/Lobby - Gitter
Now, I am trying to write unit tests for the same. But when i run the tests, i get error saying - "...
Read more >
Angular Standards for 2020 - Medium
Also use it to test Storybook ui components in isolation (for which ... Write unit tests as you develop (or before; TDD) -...
Read more >
ngx-take-until-destroy v5.4.0 - npm.io
Declarative way to unsubscribe from observables when the component destroyed. Installation. npm install ngx-take-until-destroy --save ...
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