Ionic-framework: bug: Ionic Angular was already initialized. Make sure IonicModule.forRoot() is just called once.

Created on 16 Nov 2019  路  5Comments  路  Source: ionic-team/ionic-framework

Bug Report

Ionic version:
[x] 4.x

Current behavior:
Create a new ionic angular app. Run the tests.

Expected behavior:
No warnings would be displayed.

This becomes more pervasive as the test suite grows. I see this warning numerous times throughout my real apps. Why is it there and how do we remove it? Is the generator setting up tests incorrectly?

Steps to reproduce:
git clone https://github.com/josh-m-sharpe/bareCapacitorApp.git
yarn install
npm run test

> [email protected] test /Users/jsharpe/bareCapacitorApp
> ng test

 10% building 1/1 modules 0 active16 11 2019 06:46:05.349:WARN [karma]: No captured browser, open http://localhost:9876/
16 11 2019 06:46:05.420:INFO [karma-server]: Karma v4.1.0 server started at http://0.0.0.0:9876/
16 11 2019 06:46:05.420:INFO [launcher]: Launching browsers Chrome with concurrency unlimited
16 11 2019 06:46:05.426:INFO [launcher]: Starting browser Chrome
16 11 2019 06:46:13.256:WARN [karma]: No captured browser, open http://localhost:9876/
16 11 2019 06:46:13.490:INFO [Chrome 78.0.3904 (Mac OS X 10.14.6)]: Connected on socket YbYWbp2Wnno-nsT5AAAA with id 37625611
Chrome 78.0.3904 (Mac OS X 10.14.6): Executed 0 of 6 SUCCESS (0 secs / 0 secs)
16 11 2019 06:46:16.551:WARN [web-server]: 404: /svg/md-book.svg
16 11 2019 06:46:16.553:WARN [web-server]: 404: /svg/md-build.svg
16 11 2019 06:46:16.555:WARN [web-server]: 404: /svg/md-grid.svg
WARN: 'Ionic Angular was already initialized. Make sure IonicModule.forRoot() is just called once.'
Chrome 78.0.3904 (Mac OS X 10.14.6): Executed 1 of 6 SUCCESS (0 secs / 0.465 secs)
WARN: 'Ionic Angular was already initialized. Make sure IonicModule.forRoot() is just called once.'
Chrome 78.0.3904 (Mac OS X 10.14.6): Executed 2 of 6 SUCCESS (0 secs / 0.698 secs)
Chrome 78.0.3904 (Mac OS X 10.14.6): Executed 4 of 6 SUCCESS (0 secs / 0.954 secs)
16 11 2019 06:46:17.093:WARN [web-server]: 404: /svg/md-flash.svg
16 11 2019 06:46:17.096:WARN [web-server]: 404: /svg/md-apps.svg
Chrome 78.0.3904 (Mac OS X 10.14.6): Executed 6 of 6 SUCCESS (1.07 secs / 0.995 secs)
TOTAL: 6 SUCCESS
TOTAL: 6 SUCCESS
angular bug

Most helpful comment

@Rajatsoni9 if only IonicModule is used it will not build up the Shadow DOM correctly.
Then a lot of things, like the text content of ion-text-area, cannot be tested.

All 5 comments

I'm seeing this in a new app I just created a few weeks ago. I have been doing Ionic / Stencil (no framework) for a couple years so I'm not sure if this is a new warning, something I'm doing wrong, etc.

In a current Ionic 4 / Angular 8 application with two page components, each with a test suite, when run either individually there is no WARN. However, run them both together, and WARN is output for second test.

I just upgraded my application to Ionic 4 / Angular 8 from Ionic 4 / Angular 6 and am getting the same.

I didn't notice this warning message before but it's popping up now when I run ionic serve. I haven't tested it yet on production builds.

These warnings are due to multiple calls to IonicModule.forRoot() in each test suite. If you replace it with just IonicModule in the imports array in the testing module, the warnings disappear.

@Rajatsoni9 if only IonicModule is used it will not build up the Shadow DOM correctly.
Then a lot of things, like the text content of ion-text-area, cannot be tested.

Since we currently can't prevent the warning, our workaround is to patch the console with a check for the specific warning message and mute it:

function muteIonicReInitializeWarning() {
    const originalWarn = console.warn;
    const patchedWarn = (warning: any, ...optionalParams: any[]) => {
      const suppress = `Ionic Angular was already initialized. Make sure IonicModule.forRoot() is just called once.`;
      if (warning !== suppress) originalWarn(warning, ...optionalParams);
    };
    console.warn = patchedWarn;
  }
Was this page helpful?
0 / 5 - 0 ratings

Related issues

alexbainbridge picture alexbainbridge  路  3Comments

brandyscarney picture brandyscarney  路  3Comments

fdnhkj picture fdnhkj  路  3Comments

MrBokeh picture MrBokeh  路  3Comments

danbucholtz picture danbucholtz  路  3Comments