@angular/core
Version:
Angular - the core framework
1,168 lines (1,158 loc) • 135 kB
JavaScript
/**
* @license Angular v19.2.7
* (c) 2010-2025 Google LLC. https://angular.io/
* License: MIT
*/
import * as i0 from '@angular/core';
import { ɵDeferBlockState as _DeferBlockState, ɵtriggerResourceLoading as _triggerResourceLoading, ɵrenderDeferBlockState as _renderDeferBlockState, ɵCONTAINER_HEADER_OFFSET as _CONTAINER_HEADER_OFFSET, ɵgetDeferBlocks as _getDeferBlocks, InjectionToken, ɵDeferBlockBehavior as _DeferBlockBehavior, inject as inject$1, NgZone, ErrorHandler, Injectable, ɵNoopNgZone as _NoopNgZone, ApplicationRef, ɵPendingTasksInternal as _PendingTasksInternal, ɵZONELESS_ENABLED as _ZONELESS_ENABLED, ɵChangeDetectionScheduler as _ChangeDetectionScheduler, ɵEffectScheduler as _EffectScheduler, ɵMicrotaskEffectScheduler as _MicrotaskEffectScheduler, getDebugNode, RendererFactory2, ɵstringify as _stringify, Pipe, Directive, Component, NgModule, ɵReflectionCapabilities as _ReflectionCapabilities, ɵUSE_RUNTIME_DEPS_TRACKER_FOR_JIT as _USE_RUNTIME_DEPS_TRACKER_FOR_JIT, ɵdepsTracker as _depsTracker, ɵgetInjectableDef as _getInjectableDef, resolveForwardRef, ɵisComponentDefPendingResolution as _isComponentDefPendingResolution, ɵgetAsyncClassMetadataFn as _getAsyncClassMetadataFn, ɵresolveComponentResources as _resolveComponentResources, ɵRender3NgModuleRef as _Render3NgModuleRef, ApplicationInitStatus, LOCALE_ID, ɵDEFAULT_LOCALE_ID as _DEFAULT_LOCALE_ID, ɵsetLocaleId as _setLocaleId, ɵRender3ComponentFactory as _Render3ComponentFactory, ɵNG_COMP_DEF as _NG_COMP_DEF, ɵcompileComponent as _compileComponent, ɵNG_DIR_DEF as _NG_DIR_DEF, ɵcompileDirective as _compileDirective, ɵNG_PIPE_DEF as _NG_PIPE_DEF, ɵcompilePipe as _compilePipe, ɵNG_MOD_DEF as _NG_MOD_DEF, ɵpatchComponentDefWithScope as _patchComponentDefWithScope, ɵNG_INJ_DEF as _NG_INJ_DEF, ɵcompileNgModuleDefs as _compileNgModuleDefs, ɵclearResolutionOfComponentResourcesQueue as _clearResolutionOfComponentResourcesQueue, ɵrestoreComponentResolutionQueue as _restoreComponentResolutionQueue, ɵinternalProvideZoneChangeDetection as _internalProvideZoneChangeDetection, ɵChangeDetectionSchedulerImpl as _ChangeDetectionSchedulerImpl, Compiler, ɵDEFER_BLOCK_CONFIG as _DEFER_BLOCK_CONFIG, ɵINTERNAL_APPLICATION_ERROR_HANDLER as _INTERNAL_APPLICATION_ERROR_HANDLER, COMPILER_OPTIONS, Injector, ɵtransitiveScopesFor as _transitiveScopesFor, ɵgenerateStandaloneInDeclarationsError as _generateStandaloneInDeclarationsError, ɵNgModuleFactory as _NgModuleFactory, ModuleWithComponentFactories, ɵisEnvironmentProviders as _isEnvironmentProviders, ɵconvertToBitFlags as _convertToBitFlags, InjectFlags, ɵsetAllowDuplicateNgModuleIdsForTest as _setAllowDuplicateNgModuleIdsForTest, ɵresetCompiledComponents as _resetCompiledComponents, ɵsetUnknownElementStrictMode as _setUnknownElementStrictMode, ɵsetUnknownPropertyStrictMode as _setUnknownPropertyStrictMode, ɵgetUnknownElementStrictMode as _getUnknownElementStrictMode, ɵgetUnknownPropertyStrictMode as _getUnknownPropertyStrictMode, runInInjectionContext, EnvironmentInjector, ɵflushModuleScopingQueueAsMuchAsPossible as _flushModuleScopingQueueAsMuchAsPossible } from '@angular/core';
export { ɵDeferBlockBehavior as DeferBlockBehavior, ɵDeferBlockState as DeferBlockState } from '@angular/core';
import { Subscription } from 'rxjs';
import { ResourceLoader } from '@angular/compiler';
/**
* Wraps a test function in an asynchronous test zone. The test will automatically
* complete when all asynchronous calls within this zone are done. Can be used
* to wrap an {@link inject} call.
*
* Example:
*
* ```ts
* it('...', waitForAsync(inject([AClass], (object) => {
* object.doSomething.then(() => {
* expect(...);
* })
* })));
* ```
*
* @publicApi
*/
function waitForAsync(fn) {
const _Zone = typeof Zone !== 'undefined' ? Zone : null;
if (!_Zone) {
return function () {
return Promise.reject('Zone is needed for the waitForAsync() test helper but could not be found. ' +
'Please make sure that your environment includes zone.js');
};
}
const asyncTest = _Zone && _Zone[_Zone.__symbol__('asyncTest')];
if (typeof asyncTest === 'function') {
return asyncTest(fn);
}
return function () {
return Promise.reject('zone-testing.js is needed for the async() test helper but could not be found. ' +
'Please make sure that your environment includes zone.js/testing');
};
}
/**
* Represents an individual defer block for testing purposes.
*
* @publicApi
*/
class DeferBlockFixture {
block;
componentFixture;
/** @nodoc */
constructor(block, componentFixture) {
this.block = block;
this.componentFixture = componentFixture;
}
/**
* Renders the specified state of the defer fixture.
* @param state the defer state to render
*/
async render(state) {
if (!hasStateTemplate(state, this.block)) {
const stateAsString = getDeferBlockStateNameFromEnum(state);
throw new Error(`Tried to render this defer block in the \`${stateAsString}\` state, ` +
`but there was no @${stateAsString.toLowerCase()} block defined in a template.`);
}
if (state === _DeferBlockState.Complete) {
await _triggerResourceLoading(this.block.tDetails, this.block.lView, this.block.tNode);
}
// If the `render` method is used explicitly - skip timer-based scheduling for
// `@placeholder` and `@loading` blocks and render them immediately.
const skipTimerScheduling = true;
_renderDeferBlockState(state, this.block.tNode, this.block.lContainer, skipTimerScheduling);
this.componentFixture.detectChanges();
}
/**
* Retrieves all nested child defer block fixtures
* in a given defer block.
*/
getDeferBlocks() {
const deferBlocks = [];
// An LContainer that represents a defer block has at most 1 view, which is
// located right after an LContainer header. Get a hold of that view and inspect
// it for nested defer blocks.
const deferBlockFixtures = [];
if (this.block.lContainer.length >= _CONTAINER_HEADER_OFFSET) {
const lView = this.block.lContainer[_CONTAINER_HEADER_OFFSET];
_getDeferBlocks(lView, deferBlocks);
for (const block of deferBlocks) {
deferBlockFixtures.push(new DeferBlockFixture(block, this.componentFixture));
}
}
return Promise.resolve(deferBlockFixtures);
}
}
function hasStateTemplate(state, block) {
switch (state) {
case _DeferBlockState.Placeholder:
return block.tDetails.placeholderTmplIndex !== null;
case _DeferBlockState.Loading:
return block.tDetails.loadingTmplIndex !== null;
case _DeferBlockState.Error:
return block.tDetails.errorTmplIndex !== null;
case _DeferBlockState.Complete:
return true;
default:
return false;
}
}
function getDeferBlockStateNameFromEnum(state) {
switch (state) {
case _DeferBlockState.Placeholder:
return 'Placeholder';
case _DeferBlockState.Loading:
return 'Loading';
case _DeferBlockState.Error:
return 'Error';
default:
return 'Main';
}
}
/** Whether test modules should be torn down by default. */
const TEARDOWN_TESTING_MODULE_ON_DESTROY_DEFAULT = true;
/** Whether unknown elements in templates should throw by default. */
const THROW_ON_UNKNOWN_ELEMENTS_DEFAULT = false;
/** Whether unknown properties in templates should throw by default. */
const THROW_ON_UNKNOWN_PROPERTIES_DEFAULT = false;
/** Whether defer blocks should use manual triggering or play through normally. */
const DEFER_BLOCK_DEFAULT_BEHAVIOR = _DeferBlockBehavior.Playthrough;
/**
* An abstract class for inserting the root test component element in a platform independent way.
*
* @publicApi
*/
class TestComponentRenderer {
insertRootElement(rootElementId) { }
removeAllRootElements() { }
}
/**
* @publicApi
*/
const ComponentFixtureAutoDetect = new InjectionToken('ComponentFixtureAutoDetect');
/**
* @publicApi
*/
const ComponentFixtureNoNgZone = new InjectionToken('ComponentFixtureNoNgZone');
const RETHROW_APPLICATION_ERRORS_DEFAULT = true;
class TestBedApplicationErrorHandler {
zone = inject$1(NgZone);
userErrorHandler = inject$1(ErrorHandler);
whenStableRejectFunctions = new Set();
handleError(e) {
try {
this.zone.runOutsideAngular(() => this.userErrorHandler.handleError(e));
}
catch (userError) {
e = userError;
}
// Instead of throwing the error when there are outstanding `fixture.whenStable` promises,
// reject those promises with the error. This allows developers to write
// expectAsync(fix.whenStable()).toBeRejected();
if (this.whenStableRejectFunctions.size > 0) {
for (const fn of this.whenStableRejectFunctions.values()) {
fn(e);
}
this.whenStableRejectFunctions.clear();
}
else {
throw e;
}
}
static ɵfac = function TestBedApplicationErrorHandler_Factory(__ngFactoryType__) { return new (__ngFactoryType__ || TestBedApplicationErrorHandler)(); };
static ɵprov = /*@__PURE__*/ i0.ɵɵdefineInjectable({ token: TestBedApplicationErrorHandler, factory: TestBedApplicationErrorHandler.ɵfac });
}
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(TestBedApplicationErrorHandler, [{
type: Injectable
}], null, null); })();
/**
* Fixture for debugging and testing a component.
*
* @publicApi
*/
class ComponentFixture {
componentRef;
/**
* The DebugElement associated with the root element of this component.
*/
debugElement;
/**
* The instance of the root component class.
*/
componentInstance;
/**
* The native element at the root of the component.
*/
nativeElement;
/**
* The ElementRef for the element at the root of the component.
*/
elementRef;
/**
* The ChangeDetectorRef for the component
*/
changeDetectorRef;
_renderer;
_isDestroyed = false;
/** @internal */
_noZoneOptionIsSet = inject$1(ComponentFixtureNoNgZone, { optional: true });
/** @internal */
_ngZone = this._noZoneOptionIsSet ? new _NoopNgZone() : inject$1(NgZone);
// Inject ApplicationRef to ensure NgZone stableness causes after render hooks to run
// This will likely happen as a result of fixture.detectChanges because it calls ngZone.run
// This is a crazy way of doing things but hey, it's the world we live in.
// The zoneless scheduler should instead do this more imperatively by attaching
// the `ComponentRef` to `ApplicationRef` and calling `appRef.tick` as the `detectChanges`
// behavior.
/** @internal */
_appRef = inject$1(ApplicationRef);
_testAppRef = this._appRef;
pendingTasks = inject$1(_PendingTasksInternal);
appErrorHandler = inject$1(TestBedApplicationErrorHandler);
zonelessEnabled = inject$1(_ZONELESS_ENABLED);
scheduler = inject$1(_ChangeDetectionScheduler);
rootEffectScheduler = inject$1(_EffectScheduler);
microtaskEffectScheduler = inject$1(_MicrotaskEffectScheduler);
autoDetectDefault = this.zonelessEnabled ? true : false;
autoDetect = inject$1(ComponentFixtureAutoDetect, { optional: true }) ?? this.autoDetectDefault;
subscriptions = new Subscription();
// TODO(atscott): Remove this from public API
ngZone = this._noZoneOptionIsSet ? null : this._ngZone;
/** @nodoc */
constructor(componentRef) {
this.componentRef = componentRef;
this.changeDetectorRef = componentRef.changeDetectorRef;
this.elementRef = componentRef.location;
this.debugElement = getDebugNode(this.elementRef.nativeElement);
this.componentInstance = componentRef.instance;
this.nativeElement = this.elementRef.nativeElement;
this.componentRef = componentRef;
if (this.autoDetect) {
this._testAppRef.externalTestViews.add(this.componentRef.hostView);
this.scheduler?.notify(8 /* ɵNotificationSource.ViewAttached */);
this.scheduler?.notify(0 /* ɵNotificationSource.MarkAncestorsForTraversal */);
}
this.componentRef.hostView.onDestroy(() => {
this._testAppRef.externalTestViews.delete(this.componentRef.hostView);
});
// Create subscriptions outside the NgZone so that the callbacks run outside
// of NgZone.
this._ngZone.runOutsideAngular(() => {
this.subscriptions.add(this._ngZone.onError.subscribe({
next: (error) => {
throw error;
},
}));
});
}
/**
* Trigger a change detection cycle for the component.
*/
detectChanges(checkNoChanges = true) {
this.microtaskEffectScheduler.flush();
const originalCheckNoChanges = this.componentRef.changeDetectorRef.checkNoChanges;
try {
if (!checkNoChanges) {
this.componentRef.changeDetectorRef.checkNoChanges = () => { };
}
if (this.zonelessEnabled) {
try {
this._testAppRef.externalTestViews.add(this.componentRef.hostView);
this._appRef.tick();
}
finally {
if (!this.autoDetect) {
this._testAppRef.externalTestViews.delete(this.componentRef.hostView);
}
}
}
else {
// Run the change detection inside the NgZone so that any async tasks as part of the change
// detection are captured by the zone and can be waited for in isStable.
this._ngZone.run(() => {
// Flush root effects before `detectChanges()`, to emulate the sequencing of `tick()`.
this.rootEffectScheduler.flush();
this.changeDetectorRef.detectChanges();
this.checkNoChanges();
});
}
}
finally {
this.componentRef.changeDetectorRef.checkNoChanges = originalCheckNoChanges;
}
this.microtaskEffectScheduler.flush();
}
/**
* Do a change detection run to make sure there were no changes.
*/
checkNoChanges() {
this.changeDetectorRef.checkNoChanges();
}
/**
* Set whether the fixture should autodetect changes.
*
* Also runs detectChanges once so that any existing change is detected.
*
* @param autoDetect Whether to autodetect changes. By default, `true`.
*/
autoDetectChanges(autoDetect = true) {
if (this._noZoneOptionIsSet && !this.zonelessEnabled) {
throw new Error('Cannot call autoDetectChanges when ComponentFixtureNoNgZone is set.');
}
if (autoDetect !== this.autoDetect) {
if (autoDetect) {
this._testAppRef.externalTestViews.add(this.componentRef.hostView);
}
else {
this._testAppRef.externalTestViews.delete(this.componentRef.hostView);
}
}
this.autoDetect = autoDetect;
this.detectChanges();
}
/**
* Return whether the fixture is currently stable or has async tasks that have not been completed
* yet.
*/
isStable() {
return !this.pendingTasks.hasPendingTasks.value;
}
/**
* Get a promise that resolves when the fixture is stable.
*
* This can be used to resume testing after events have triggered asynchronous activity or
* asynchronous change detection.
*/
whenStable() {
if (this.isStable()) {
return Promise.resolve(false);
}
return new Promise((resolve, reject) => {
this.appErrorHandler.whenStableRejectFunctions.add(reject);
this._appRef.whenStable().then(() => {
this.appErrorHandler.whenStableRejectFunctions.delete(reject);
resolve(true);
});
});
}
/**
* Retrieves all defer block fixtures in the component fixture.
*/
getDeferBlocks() {
const deferBlocks = [];
const lView = this.componentRef.hostView['_lView'];
_getDeferBlocks(lView, deferBlocks);
const deferBlockFixtures = [];
for (const block of deferBlocks) {
deferBlockFixtures.push(new DeferBlockFixture(block, this));
}
return Promise.resolve(deferBlockFixtures);
}
_getRenderer() {
if (this._renderer === undefined) {
this._renderer = this.componentRef.injector.get(RendererFactory2, null);
}
return this._renderer;
}
/**
* Get a promise that resolves when the ui state is stable following animations.
*/
whenRenderingDone() {
const renderer = this._getRenderer();
if (renderer && renderer.whenRenderingDone) {
return renderer.whenRenderingDone();
}
return this.whenStable();
}
/**
* Trigger component destruction.
*/
destroy() {
this.subscriptions.unsubscribe();
this._testAppRef.externalTestViews.delete(this.componentRef.hostView);
if (!this._isDestroyed) {
this.componentRef.destroy();
this._isDestroyed = true;
}
}
}
const _Zone = typeof Zone !== 'undefined' ? Zone : null;
const fakeAsyncTestModule = _Zone && _Zone[_Zone.__symbol__('fakeAsyncTest')];
const fakeAsyncTestModuleNotLoadedErrorMessage = `zone-testing.js is needed for the fakeAsync() test helper but could not be found.
Please make sure that your environment includes zone.js/testing`;
/**
* Clears out the shared fake async zone for a test.
* To be called in a global `beforeEach`.
*
* @publicApi
*/
function resetFakeAsyncZone() {
if (fakeAsyncTestModule) {
return fakeAsyncTestModule.resetFakeAsyncZone();
}
throw new Error(fakeAsyncTestModuleNotLoadedErrorMessage);
}
function resetFakeAsyncZoneIfExists() {
if (fakeAsyncTestModule) {
fakeAsyncTestModule.resetFakeAsyncZone();
}
}
/**
* Wraps a function to be executed in the `fakeAsync` zone:
* - Microtasks are manually executed by calling `flushMicrotasks()`.
* - Timers are synchronous; `tick()` simulates the asynchronous passage of time.
*
* Can be used to wrap `inject()` calls.
*
* @param fn The function that you want to wrap in the `fakeAsync` zone.
* @param options
* - flush: When true, will drain the macrotask queue after the test function completes.
* When false, will throw an exception at the end of the function if there are pending timers.
*
* @usageNotes
* ### Example
*
* {@example core/testing/ts/fake_async.ts region='basic'}
*
*
* @returns The function wrapped to be executed in the `fakeAsync` zone.
* Any arguments passed when calling this returned function will be passed through to the `fn`
* function in the parameters when it is called.
*
* @publicApi
*/
function fakeAsync(fn, options) {
if (fakeAsyncTestModule) {
return fakeAsyncTestModule.fakeAsync(fn, options);
}
throw new Error(fakeAsyncTestModuleNotLoadedErrorMessage);
}
/**
* Simulates the asynchronous passage of time for the timers in the `fakeAsync` zone.
*
* The microtasks queue is drained at the very start of this function and after any timer callback
* has been executed.
*
* @param millis The number of milliseconds to advance the virtual timer.
* @param tickOptions The options to pass to the `tick()` function.
*
* @usageNotes
*
* The `tick()` option is a flag called `processNewMacroTasksSynchronously`,
* which determines whether or not to invoke new macroTasks.
*
* If you provide a `tickOptions` object, but do not specify a
* `processNewMacroTasksSynchronously` property (`tick(100, {})`),
* then `processNewMacroTasksSynchronously` defaults to true.
*
* If you omit the `tickOptions` parameter (`tick(100))`), then
* `tickOptions` defaults to `{processNewMacroTasksSynchronously: true}`.
*
* ### Example
*
* {@example core/testing/ts/fake_async.ts region='basic'}
*
* The following example includes a nested timeout (new macroTask), and
* the `tickOptions` parameter is allowed to default. In this case,
* `processNewMacroTasksSynchronously` defaults to true, and the nested
* function is executed on each tick.
*
* ```ts
* it ('test with nested setTimeout', fakeAsync(() => {
* let nestedTimeoutInvoked = false;
* function funcWithNestedTimeout() {
* setTimeout(() => {
* nestedTimeoutInvoked = true;
* });
* };
* setTimeout(funcWithNestedTimeout);
* tick();
* expect(nestedTimeoutInvoked).toBe(true);
* }));
* ```
*
* In the following case, `processNewMacroTasksSynchronously` is explicitly
* set to false, so the nested timeout function is not invoked.
*
* ```ts
* it ('test with nested setTimeout', fakeAsync(() => {
* let nestedTimeoutInvoked = false;
* function funcWithNestedTimeout() {
* setTimeout(() => {
* nestedTimeoutInvoked = true;
* });
* };
* setTimeout(funcWithNestedTimeout);
* tick(0, {processNewMacroTasksSynchronously: false});
* expect(nestedTimeoutInvoked).toBe(false);
* }));
* ```
*
*
* @publicApi
*/
function tick(millis = 0, tickOptions = {
processNewMacroTasksSynchronously: true,
}) {
if (fakeAsyncTestModule) {
return fakeAsyncTestModule.tick(millis, tickOptions);
}
throw new Error(fakeAsyncTestModuleNotLoadedErrorMessage);
}
/**
* Flushes any pending microtasks and simulates the asynchronous passage of time for the timers in
* the `fakeAsync` zone by
* draining the macrotask queue until it is empty.
*
* @param maxTurns The maximum number of times the scheduler attempts to clear its queue before
* throwing an error.
* @returns The simulated time elapsed, in milliseconds.
*
* @publicApi
*/
function flush(maxTurns) {
if (fakeAsyncTestModule) {
return fakeAsyncTestModule.flush(maxTurns);
}
throw new Error(fakeAsyncTestModuleNotLoadedErrorMessage);
}
/**
* Discard all remaining periodic tasks.
*
* @publicApi
*/
function discardPeriodicTasks() {
if (fakeAsyncTestModule) {
return fakeAsyncTestModule.discardPeriodicTasks();
}
throw new Error(fakeAsyncTestModuleNotLoadedErrorMessage);
}
/**
* Flush any pending microtasks.
*
* @publicApi
*/
function flushMicrotasks() {
if (fakeAsyncTestModule) {
return fakeAsyncTestModule.flushMicrotasks();
}
throw new Error(fakeAsyncTestModuleNotLoadedErrorMessage);
}
let _nextReferenceId = 0;
class MetadataOverrider {
_references = new Map();
/**
* Creates a new instance for the given metadata class
* based on an old instance and overrides.
*/
overrideMetadata(metadataClass, oldMetadata, override) {
const props = {};
if (oldMetadata) {
_valueProps(oldMetadata).forEach((prop) => (props[prop] = oldMetadata[prop]));
}
if (override.set) {
if (override.remove || override.add) {
throw new Error(`Cannot set and add/remove ${_stringify(metadataClass)} at the same time!`);
}
setMetadata(props, override.set);
}
if (override.remove) {
removeMetadata(props, override.remove, this._references);
}
if (override.add) {
addMetadata(props, override.add);
}
return new metadataClass(props);
}
}
function removeMetadata(metadata, remove, references) {
const removeObjects = new Set();
for (const prop in remove) {
const removeValue = remove[prop];
if (Array.isArray(removeValue)) {
removeValue.forEach((value) => {
removeObjects.add(_propHashKey(prop, value, references));
});
}
else {
removeObjects.add(_propHashKey(prop, removeValue, references));
}
}
for (const prop in metadata) {
const propValue = metadata[prop];
if (Array.isArray(propValue)) {
metadata[prop] = propValue.filter((value) => !removeObjects.has(_propHashKey(prop, value, references)));
}
else {
if (removeObjects.has(_propHashKey(prop, propValue, references))) {
metadata[prop] = undefined;
}
}
}
}
function addMetadata(metadata, add) {
for (const prop in add) {
const addValue = add[prop];
const propValue = metadata[prop];
if (propValue != null && Array.isArray(propValue)) {
metadata[prop] = propValue.concat(addValue);
}
else {
metadata[prop] = addValue;
}
}
}
function setMetadata(metadata, set) {
for (const prop in set) {
metadata[prop] = set[prop];
}
}
function _propHashKey(propName, propValue, references) {
let nextObjectId = 0;
const objectIds = new Map();
const replacer = (key, value) => {
if (value !== null && typeof value === 'object') {
if (objectIds.has(value)) {
return objectIds.get(value);
}
// Record an id for this object such that any later references use the object's id instead
// of the object itself, in order to break cyclic pointers in objects.
objectIds.set(value, `ɵobj#${nextObjectId++}`);
// The first time an object is seen the object itself is serialized.
return value;
}
else if (typeof value === 'function') {
value = _serializeReference(value, references);
}
return value;
};
return `${propName}:${JSON.stringify(propValue, replacer)}`;
}
function _serializeReference(ref, references) {
let id = references.get(ref);
if (!id) {
id = `${_stringify(ref)}${_nextReferenceId++}`;
references.set(ref, id);
}
return id;
}
function _valueProps(obj) {
const props = [];
// regular public props
Object.keys(obj).forEach((prop) => {
if (!prop.startsWith('_')) {
props.push(prop);
}
});
// getters
let proto = obj;
while ((proto = Object.getPrototypeOf(proto))) {
Object.keys(proto).forEach((protoProp) => {
const desc = Object.getOwnPropertyDescriptor(proto, protoProp);
if (!protoProp.startsWith('_') && desc && 'get' in desc) {
props.push(protoProp);
}
});
}
return props;
}
const reflection = new _ReflectionCapabilities();
/**
* Allows to override ivy metadata for tests (via the `TestBed`).
*/
class OverrideResolver {
overrides = new Map();
resolved = new Map();
addOverride(type, override) {
const overrides = this.overrides.get(type) || [];
overrides.push(override);
this.overrides.set(type, overrides);
this.resolved.delete(type);
}
setOverrides(overrides) {
this.overrides.clear();
overrides.forEach(([type, override]) => {
this.addOverride(type, override);
});
}
getAnnotation(type) {
const annotations = reflection.annotations(type);
// Try to find the nearest known Type annotation and make sure that this annotation is an
// instance of the type we are looking for, so we can use it for resolution. Note: there might
// be multiple known annotations found due to the fact that Components can extend Directives (so
// both Directive and Component annotations would be present), so we always check if the known
// annotation has the right type.
for (let i = annotations.length - 1; i >= 0; i--) {
const annotation = annotations[i];
const isKnownType = annotation instanceof Directive ||
annotation instanceof Component ||
annotation instanceof Pipe ||
annotation instanceof NgModule;
if (isKnownType) {
return annotation instanceof this.type ? annotation : null;
}
}
return null;
}
resolve(type) {
let resolved = this.resolved.get(type) || null;
if (!resolved) {
resolved = this.getAnnotation(type);
if (resolved) {
const overrides = this.overrides.get(type);
if (overrides) {
const overrider = new MetadataOverrider();
overrides.forEach((override) => {
resolved = overrider.overrideMetadata(this.type, resolved, override);
});
}
}
this.resolved.set(type, resolved);
}
return resolved;
}
}
class DirectiveResolver extends OverrideResolver {
get type() {
return Directive;
}
}
class ComponentResolver extends OverrideResolver {
get type() {
return Component;
}
}
class PipeResolver extends OverrideResolver {
get type() {
return Pipe;
}
}
class NgModuleResolver extends OverrideResolver {
get type() {
return NgModule;
}
}
var TestingModuleOverride;
(function (TestingModuleOverride) {
TestingModuleOverride[TestingModuleOverride["DECLARATION"] = 0] = "DECLARATION";
TestingModuleOverride[TestingModuleOverride["OVERRIDE_TEMPLATE"] = 1] = "OVERRIDE_TEMPLATE";
})(TestingModuleOverride || (TestingModuleOverride = {}));
function isTestingModuleOverride(value) {
return (value === TestingModuleOverride.DECLARATION || value === TestingModuleOverride.OVERRIDE_TEMPLATE);
}
function assertNoStandaloneComponents(types, resolver, location) {
types.forEach((type) => {
if (!_getAsyncClassMetadataFn(type)) {
const component = resolver.resolve(type);
if (component && (component.standalone == null || component.standalone)) {
throw new Error(_generateStandaloneInDeclarationsError(type, location));
}
}
});
}
class TestBedCompiler {
platform;
additionalModuleTypes;
originalComponentResolutionQueue = null;
// Testing module configuration
declarations = [];
imports = [];
providers = [];
schemas = [];
// Queues of components/directives/pipes that should be recompiled.
pendingComponents = new Set();
pendingDirectives = new Set();
pendingPipes = new Set();
// Set of components with async metadata, i.e. components with `@defer` blocks
// in their templates.
componentsWithAsyncMetadata = new Set();
// Keep track of all components and directives, so we can patch Providers onto defs later.
seenComponents = new Set();
seenDirectives = new Set();
// Keep track of overridden modules, so that we can collect all affected ones in the module tree.
overriddenModules = new Set();
// Store resolved styles for Components that have template overrides present and `styleUrls`
// defined at the same time.
existingComponentStyles = new Map();
resolvers = initResolvers();
// Map of component type to an NgModule that declares it.
//
// There are a couple special cases:
// - for standalone components, the module scope value is `null`
// - when a component is declared in `TestBed.configureTestingModule()` call or
// a component's template is overridden via `TestBed.overrideTemplateUsingTestingModule()`.
// we use a special value from the `TestingModuleOverride` enum.
componentToModuleScope = new Map();
// Map that keeps initial version of component/directive/pipe defs in case
// we compile a Type again, thus overriding respective static fields. This is
// required to make sure we restore defs to their initial states between test runs.
// Note: one class may have multiple defs (for example: ɵmod and ɵinj in case of an
// NgModule), store all of them in a map.
initialNgDefs = new Map();
// Array that keeps cleanup operations for initial versions of component/directive/pipe/module
// defs in case TestBed makes changes to the originals.
defCleanupOps = [];
_injector = null;
compilerProviders = null;
providerOverrides = [];
rootProviderOverrides = [];
// Overrides for injectables with `{providedIn: SomeModule}` need to be tracked and added to that
// module's provider list.
providerOverridesByModule = new Map();
providerOverridesByToken = new Map();
scopesWithOverriddenProviders = new Set();
testModuleType;
testModuleRef = null;
deferBlockBehavior = DEFER_BLOCK_DEFAULT_BEHAVIOR;
rethrowApplicationTickErrors = RETHROW_APPLICATION_ERRORS_DEFAULT;
constructor(platform, additionalModuleTypes) {
this.platform = platform;
this.additionalModuleTypes = additionalModuleTypes;
class DynamicTestModule {
}
this.testModuleType = DynamicTestModule;
}
setCompilerProviders(providers) {
this.compilerProviders = providers;
this._injector = null;
}
configureTestingModule(moduleDef) {
// Enqueue any compilation tasks for the directly declared component.
if (moduleDef.declarations !== undefined) {
// Verify that there are no standalone components
assertNoStandaloneComponents(moduleDef.declarations, this.resolvers.component, '"TestBed.configureTestingModule" call');
this.queueTypeArray(moduleDef.declarations, TestingModuleOverride.DECLARATION);
this.declarations.push(...moduleDef.declarations);
}
// Enqueue any compilation tasks for imported modules.
if (moduleDef.imports !== undefined) {
this.queueTypesFromModulesArray(moduleDef.imports);
this.imports.push(...moduleDef.imports);
}
if (moduleDef.providers !== undefined) {
this.providers.push(...moduleDef.providers);
}
if (moduleDef.schemas !== undefined) {
this.schemas.push(...moduleDef.schemas);
}
this.deferBlockBehavior = moduleDef.deferBlockBehavior ?? DEFER_BLOCK_DEFAULT_BEHAVIOR;
this.rethrowApplicationTickErrors =
moduleDef.rethrowApplicationErrors ?? RETHROW_APPLICATION_ERRORS_DEFAULT;
}
overrideModule(ngModule, override) {
if (_USE_RUNTIME_DEPS_TRACKER_FOR_JIT) {
_depsTracker.clearScopeCacheFor(ngModule);
}
this.overriddenModules.add(ngModule);
// Compile the module right away.
this.resolvers.module.addOverride(ngModule, override);
const metadata = this.resolvers.module.resolve(ngModule);
if (metadata === null) {
throw invalidTypeError(ngModule.name, 'NgModule');
}
this.recompileNgModule(ngModule, metadata);
// At this point, the module has a valid module def (ɵmod), but the override may have introduced
// new declarations or imported modules. Ingest any possible new types and add them to the
// current queue.
this.queueTypesFromModulesArray([ngModule]);
}
overrideComponent(component, override) {
this.verifyNoStandaloneFlagOverrides(component, override);
this.resolvers.component.addOverride(component, override);
this.pendingComponents.add(component);
// If this is a component with async metadata (i.e. a component with a `@defer` block
// in a template) - store it for future processing.
this.maybeRegisterComponentWithAsyncMetadata(component);
}
overrideDirective(directive, override) {
this.verifyNoStandaloneFlagOverrides(directive, override);
this.resolvers.directive.addOverride(directive, override);
this.pendingDirectives.add(directive);
}
overridePipe(pipe, override) {
this.verifyNoStandaloneFlagOverrides(pipe, override);
this.resolvers.pipe.addOverride(pipe, override);
this.pendingPipes.add(pipe);
}
verifyNoStandaloneFlagOverrides(type, override) {
if (override.add?.hasOwnProperty('standalone') ||
override.set?.hasOwnProperty('standalone') ||
override.remove?.hasOwnProperty('standalone')) {
throw new Error(`An override for the ${type.name} class has the \`standalone\` flag. ` +
`Changing the \`standalone\` flag via TestBed overrides is not supported.`);
}
}
overrideProvider(token, provider) {
let providerDef;
if (provider.useFactory !== undefined) {
providerDef = {
provide: token,
useFactory: provider.useFactory,
deps: provider.deps || [],
multi: provider.multi,
};
}
else if (provider.useValue !== undefined) {
providerDef = { provide: token, useValue: provider.useValue, multi: provider.multi };
}
else {
providerDef = { provide: token };
}
const injectableDef = typeof token !== 'string' ? _getInjectableDef(token) : null;
const providedIn = injectableDef === null ? null : resolveForwardRef(injectableDef.providedIn);
const overridesBucket = providedIn === 'root' ? this.rootProviderOverrides : this.providerOverrides;
overridesBucket.push(providerDef);
// Keep overrides grouped by token as well for fast lookups using token
this.providerOverridesByToken.set(token, providerDef);
if (injectableDef !== null && providedIn !== null && typeof providedIn !== 'string') {
const existingOverrides = this.providerOverridesByModule.get(providedIn);
if (existingOverrides !== undefined) {
existingOverrides.push(providerDef);
}
else {
this.providerOverridesByModule.set(providedIn, [providerDef]);
}
}
}
overrideTemplateUsingTestingModule(type, template) {
const def = type[_NG_COMP_DEF];
const hasStyleUrls = () => {
const metadata = this.resolvers.component.resolve(type);
return !!metadata.styleUrl || !!metadata.styleUrls?.length;
};
const overrideStyleUrls = !!def && !_isComponentDefPendingResolution(type) && hasStyleUrls();
// In Ivy, compiling a component does not require knowing the module providing the
// component's scope, so overrideTemplateUsingTestingModule can be implemented purely via
// overrideComponent. Important: overriding template requires full Component re-compilation,
// which may fail in case styleUrls are also present (thus Component is considered as required
// resolution). In order to avoid this, we preemptively set styleUrls to an empty array,
// preserve current styles available on Component def and restore styles back once compilation
// is complete.
const override = overrideStyleUrls
? { template, styles: [], styleUrls: [], styleUrl: undefined }
: { template };
this.overrideComponent(type, { set: override });
if (overrideStyleUrls && def.styles && def.styles.length > 0) {
this.existingComponentStyles.set(type, def.styles);
}
// Set the component's scope to be the testing module.
this.componentToModuleScope.set(type, TestingModuleOverride.OVERRIDE_TEMPLATE);
}
async resolvePendingComponentsWithAsyncMetadata() {
if (this.componentsWithAsyncMetadata.size === 0)
return;
const promises = [];
for (const component of this.componentsWithAsyncMetadata) {
const asyncMetadataFn = _getAsyncClassMetadataFn(component);
if (asyncMetadataFn) {
promises.push(asyncMetadataFn());
}
}
this.componentsWithAsyncMetadata.clear();
const resolvedDeps = await Promise.all(promises);
const flatResolvedDeps = resolvedDeps.flat(2);
this.queueTypesFromModulesArray(flatResolvedDeps);
// Loaded standalone components might contain imports of NgModules
// with providers, make sure we override providers there too.
for (const component of flatResolvedDeps) {
this.applyProviderOverridesInScope(component);
}
}
async compileComponents() {
this.clearComponentResolutionQueue();
// Wait for all async metadata for components that were
// overridden, we need resolved metadata to perform an override
// and re-compile a component.
await this.resolvePendingComponentsWithAsyncMetadata();
// Verify that there were no standalone components present in the `declarations` field
// during the `TestBed.configureTestingModule` call. We perform this check here in addition
// to the logic in the `configureTestingModule` function, since at this point we have
// all async metadata resolved.
assertNoStandaloneComponents(this.declarations, this.resolvers.component, '"TestBed.configureTestingModule" call');
// Run compilers for all queued types.
let needsAsyncResources = this.compileTypesSync();
// compileComponents() should not be async unless it needs to be.
if (needsAsyncResources) {
let resourceLoader;
let resolver = (url) => {
if (!resourceLoader) {
resourceLoader = this.injector.get(ResourceLoader);
}
return Promise.resolve(resourceLoader.get(url));
};
await _resolveComponentResources(resolver);
}
}
finalize() {
// One last compile
this.compileTypesSync();
// Create the testing module itself.
this.compileTestModule();
this.applyTransitiveScopes();
this.applyProviderOverrides();
// Patch previously stored `styles` Component values (taken from ɵcmp), in case these
// Components have `styleUrls` fields defined and template override was requested.
this.patchComponentsWithExistingStyles();
// Clear the componentToModuleScope map, so that future compilations don't reset the scope of
// every component.
this.componentToModuleScope.clear();
const parentInjector = this.platform.injector;
this.testModuleRef = new _Render3NgModuleRef(this.testModuleType, parentInjector, []);
// ApplicationInitStatus.runInitializers() is marked @internal to core.
// Cast it to any before accessing it.
this.testModuleRef.injector.get(ApplicationInitStatus).runInitializers();
// Set locale ID after running app initializers, since locale information might be updated while
// running initializers. This is also consistent with the execution order while bootstrapping an
// app (see `packages/core/src/application_ref.ts` file).
const localeId = this.testModuleRef.injector.get(LOCALE_ID, _DEFAULT_LOCALE_ID);
_setLocaleId(localeId);
return this.testModuleRef;
}
/**
* @internal
*/
_compileNgModuleSync(moduleType) {
this.queueTypesFromModulesArray([moduleType]);
this.compileTypesSync();
this.applyProviderOverrides();
this.applyProviderOverridesInScope(moduleType);
this.applyTransitiveScopes();
}
/**
* @internal
*/
async _compileNgModuleAsync(moduleType) {
this.queueTypesFromModulesArray([moduleType]);
await this.compileComponents();
this.applyProviderOverrides();
this.applyProviderOverridesInScope(moduleType);
this.applyTransitiveScopes();
}
/**
* @internal
*/
_getModuleResolver() {
return this.resolvers.module;
}
/**
* @internal
*/
_getComponentFactories(moduleType) {
return maybeUnwrapFn(moduleType.ɵmod.declarations).reduce((factories, declaration) => {
const componentDef = declaration.ɵcmp;
componentDef && factories.push(new _Render3ComponentFactory(componentDef, this.testModuleRef));
return factories;
}, []);
}
compileTypesSync() {
// Compile all queued components, directives, pipes.
let needsAsyncResources = false;
this.pendingComponents.forEach((declaration) => {
if (_getAsyncClassMetadataFn(declaration)) {
throw new Error(`Component '${declaration.name}' has unresolved metadata. ` +
`Please call \`await TestBed.compileComponents()\` before running this test.`);
}
needsAsyncResources = needsAsyncResources || _isComponentDefPendingResolution(declaration);
const metadata = this.resolvers.component.resolve(declaration);
if (metadata === null) {
throw invalidTypeError(declaration.name, 'Component');
}
this.maybeStoreNgDef(_NG_COMP_DEF, declaration);
if (_USE_RUNTIME_DEPS_TRACKER_FOR_JIT) {
_depsTracker.clearScopeCacheFor(declaration);
}
_compileComponent(declaration, metadata);
});
this.pendingComponents.clear();
this.pendingDirectives.forEach((declaration) => {
const metadata = this.resolvers.directive.resolve(declaration);
if (metadata === null) {
throw invalidTypeError(declaration.name, 'Directive');
}
this.maybeStoreNgDef(_NG_DIR_DEF, declaration);
_compileDirective(declaration, metadata);
});
this.pendingDirectives.clear();
this.pendingPipes.forEach((declaration) => {
const metadata = this.resolvers.pipe.resolve(declaration);
if (metadata === null) {
throw invalidTypeError(declaration.name, 'Pipe');
}
this.maybeStoreNgDef(_NG_PIPE_DEF, declaration);
_compilePipe(declaration, metadata);
});
this.pendingPipes.clear();
return needsAsyncResources;
}
applyTransitiveScopes() {
if (this.overriddenModules.size > 0) {
// Module overrides (via `TestBed.overrideModule`) might affect scopes that were previously
// calculated and stored in `transitiveCompileScopes`. If module overrides are present,
// collect all affected modules and reset scopes to force their re-calculation.
const testingModuleDef = this.testModuleType[_NG_MOD_DEF];
const affectedModules = this.collectModulesAffectedByOverrides(testingModuleDef.imports);
if (affectedModules.size > 0) {
affectedModules.forEach((moduleType) => {
if (!_USE_RUNTIME_DEPS_TRACKER_FOR_JIT) {
this.storeFieldOfDefOnType(moduleType, _NG_MOD_DEF, 'transitiveCompileScopes');
moduleType[_NG_MOD_DEF].transitiveCompileScopes = null;
}
else {
_depsTracker.clearScopeCacheFor(moduleType);
}
});
}
}
const moduleToScope = new Map();
const getScopeOfModule = (moduleType) => {
if (!moduleToScope.has(moduleType)) {
const isTestingModule = isTestingModuleOverride(moduleType);
const realType = isTestingModule ? this.testModuleType : moduleType;
moduleToScope.set(moduleType, _transitiveScopesFor(realType));
}
return moduleToScope.get(moduleType);
};
this.componentToModuleScope.forEach((moduleType, componentType) => {
if (moduleType !== null) {
const moduleScope = getScopeOfModule(moduleType);
this.storeFieldOfDefOnType(componentType, _NG_COMP_DEF, 'directiveDefs');
this.storeFieldOfDefOnType(componentType, _NG_COMP_DEF, 'pipeDefs');
_patchComponentDefWithScope(getComponentDef(componentType), moduleScope);
}
// `tView` that is stored on component def contains information about directives and pipes
// that are in the scope of this component. Patching component scope will cause `tView` to be
// changed. Store original `tView` before patching scope, so the `tView` (including scope
// information) is restored back to its previous/original state before running next test.
// Resetting `tView` is also needed for cases when we apply provider overrides and those
// providers are defined on component's level, in which case they may end up included into
// `tView.blueprint`.
this.storeFieldOfDefOnType(componentType, _NG_COMP_DEF, 'tView');
});
this.componentToModuleScope.clear();
}
applyProviderOverrides() {
const maybeApplyOverrides = (field) => (type) => {
const resolver = field === _NG_COMP_DEF ? this.resolvers.component : this.resolvers.directive;
const metadata = resolver.resolve(type);
if (this.hasProviderOverrides(metadata.providers)) {
this.patchDefWithProviderOverrides(type, field);
}
};
this.seenComponents.forEach(maybeApplyOverrides(_NG_COMP_DEF));
this.seenDirectives.forEach(maybeApplyOverrides(_NG_DIR_DEF));
this.seenComponents.clear();
this.seenDirectives.clear();
}
/**
* Applies provider overrides to a given type (either an NgModule or a standalone component