You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
bug: Testing Ionic component in Angular with NgIf using fakeAsync throws an error #22684
Using a template like this <ion-text *ngIf="attribute">text</ion-text>
in Angular breaks a test when it is using fakeAsync()
Ionic version:
[ ] 4.x
[x] 5.x
Current behavior:
When testing a component using Angular's TestBed and it's fakeAsync() utility function, if the component contains an Ionic component in its template together with ngIf, running a test with this set up results in a ChunkLoadError.
Expected behavior:
No error should be raised.
Steps to reproduce:
Download the repo and run tests. There is one focused test that should fail.
Other information:
I'm not 100% sure that this is an Ionic bug but changing the Ionic component to a different one (or just a plain <div>) in the example fixes it. That's why I think it is appropriate to post this issue here.
When the component is present, it registers one or more timers for some reason (typically set to 2 minutes - 120000 ms) but I have no clue where does this timer come from. Angular's flush() function drains the macrotask queue and returns the time that would have elapsed. So when I call flush() in test function, I expect it to equal 0 as I have not yet scheduled any timer myself. Yet this is not true and the test fails.
This bug prevents us from running the tests and thus testing the component. We haven't find a workaround for this issue yet.
Please comment if you have any suggestion or tip how to temporarily fix this or how to better trace where does this bug come from. This bug has been haunting us in our company for the past few days 🙂
Ionic info:
Ionic:
Ionic CLI : 6.12.1 (/usr/local/lib/node_modules/@ionic/cli)
Ionic Framework : @ionic/angular 5.5.2
@angular-devkit/build-angular : 0.1000.8
@angular-devkit/schematics : 10.0.8
@angular/cli : 10.0.8
@ionic/angular-toolkit : 2.3.3
Utility:
cordova-res : not installed
native-run : not installed
System:
NodeJS : v12.14.1 (/Users/josefkriz/.nvm/versions/node/v12.14.1/bin/node)
npm : 6.13.4
OS : macOS Big Sur
Hey. This is similar to #23173. On our project the only way we've got around this so far is to not use fakeAsync and instead use waitForAsync + fixture.whenStable.
Thanks for the report. I can reproduce the reported behavior. This is due to an incompatibility with how Ionic components are loaded using IonicModule. Our components are lazily loaded using Stencil as needed. This can cause issues if the state of the app changes before loading is done (in this case calling detectChanges).
While this error is coming from Ionic/Stencil, avoiding lazy loading in the IonicModule will likely come with breaking changes that will disrupt existing applications. I recommend using Ionic's standalone components as a solution for this instead. This approach avoids Stencil's lazy loading, so the reported error should go away when using standalone components. See https://ionicframework.com/docs/angular/build-options#standalone for more information.
Alternatively, if you are unable to migrate to standalone components, then I recommend giving #22684 (comment) a try.
Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Ionic, please create a new issue and ensure the template is fully filled out.
Activity
studioromeo commentedon Nov 29, 2021
Hey. This is similar to #23173. On our project the only way we've got around this so far is to not use
fakeAsyncand instead usewaitForAsync+fixture.whenStable.liamdebeasi commentedon Dec 13, 2023
Thanks for the report. I can reproduce the reported behavior. This is due to an incompatibility with how Ionic components are loaded using
IonicModule. Our components are lazily loaded using Stencil as needed. This can cause issues if the state of the app changes before loading is done (in this case callingdetectChanges).While this error is coming from Ionic/Stencil, avoiding lazy loading in the
IonicModulewill likely come with breaking changes that will disrupt existing applications. I recommend using Ionic's standalone components as a solution for this instead. This approach avoids Stencil's lazy loading, so the reported error should go away when using standalone components. See https://ionicframework.com/docs/angular/build-options#standalone for more information.Alternatively, if you are unable to migrate to standalone components, then I recommend giving #22684 (comment) a try.
ionitron-bot commentedon Jan 12, 2024
Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Ionic, please create a new issue and ensure the template is fully filled out.