diff --git a/goldens/public-api/core/index.md b/goldens/public-api/core/index.md index 2b0ad6df626..211c73c7c7d 100644 --- a/goldens/public-api/core/index.md +++ b/goldens/public-api/core/index.md @@ -53,6 +53,7 @@ export interface ApplicationConfig { // @public export class ApplicationInitStatus { + constructor(); // (undocumented) readonly done = false; // (undocumented) diff --git a/packages/core/src/application_init.ts b/packages/core/src/application_init.ts index 24d7f571ae3..597540fc9ae 100644 --- a/packages/core/src/application_init.ts +++ b/packages/core/src/application_init.ts @@ -9,6 +9,7 @@ import {Observable} from 'rxjs'; import {inject, Injectable, InjectionToken} from './di'; +import {RuntimeError, RuntimeErrorCode} from './errors'; import {isPromise, isSubscribable} from './util/lang'; /** @@ -104,9 +105,19 @@ export class ApplicationInitStatus { this.reject = rej; }); - // TODO: Throw RuntimeErrorCode.INVALID_MULTI_PROVIDER if appInits is not an array private readonly appInits = inject(APP_INITIALIZER, {optional: true}) ?? []; + constructor() { + if ((typeof ngDevMode === 'undefined' || ngDevMode) && !Array.isArray(this.appInits)) { + throw new RuntimeError( + RuntimeErrorCode.INVALID_MULTI_PROVIDER, + 'Unexpected type of the `APP_INITIALIZER` token value ' + + `(expected an array, but got ${typeof this.appInits}). ` + + 'Please check that the `APP_INITIALIZER` token is configured as a ' + + '`multi: true` provider.'); + } + } + /** @internal */ runInitializers() { if (this.initialized) { diff --git a/packages/core/test/application_init_spec.ts b/packages/core/test/application_init_spec.ts index f4872e32b17..f1002421fc4 100644 --- a/packages/core/test/application_init_spec.ts +++ b/packages/core/test/application_init_spec.ts @@ -166,4 +166,23 @@ describe('ApplicationInitStatus', () => { } }); }); + + describe('wrong initializers', () => { + beforeEach(() => { + TestBed.configureTestingModule( + {providers: [{provide: APP_INITIALIZER, useValue: 'notAnArray'}]}); + }); + + it('should throw', () => { + try { + TestBed.inject(ApplicationInitStatus); + } catch (e: any) { + expect(e.message).toBe( + 'NG0209: Unexpected type of the `APP_INITIALIZER` token value ' + + `(expected an array, but got string). ` + + 'Please check that the `APP_INITIALIZER` token is configured as a ' + + '`multi: true` provider. Find more at https://angular.io/errors/NG0209'); + } + }); + }); });