Skip to content

bug: Testing Ionic component in Angular with NgIf using fakeAsync throws an error #22684

Closed as not planned

Description

@josef-kriz

Bug Report

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.

Related code:

There is a repository with the bug reproduced: https://github.com/TheodorCon/ionic-fakeasync-bug

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

Activity

  1. added
    package: core
    type: bug
    on Dec 18, 2020
  2. studioromeo commented on Nov 29, 2021

    @studioromeo
    Contributor

    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.

  3. liamdebeasi commented on Dec 13, 2023

    @liamdebeasi
    Contributor

    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.

  4. ionitron-bot commented on Jan 12, 2024

    @ionitron-bot

    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.

  5. locked and limited conversation to collaborators on Jan 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    package: coretype: bug

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions