UNPKG

108 kBJavaScriptView Raw
1/**
2 * @license Angular v13.0.2
3 * (c) 2010-2021 Google LLC. https://angular.io/
4 * License: MIT
5 */
6
7import * as i0 from '@angular/core';
8import { getDebugNode, RendererFactory2, ɵstringify, ɵReflectionCapabilities, Directive, Component, Pipe, NgModule, ɵgetInjectableDef, resolveForwardRef, ɵNG_COMP_DEF, ɵRender3NgModuleRef, ApplicationInitStatus, LOCALE_ID, ɵDEFAULT_LOCALE_ID, ɵsetLocaleId, ɵRender3ComponentFactory, ɵcompileComponent, ɵNG_DIR_DEF, ɵcompileDirective, ɵNG_PIPE_DEF, ɵcompilePipe, ɵNG_MOD_DEF, ɵtransitiveScopesFor, ɵpatchComponentDefWithScope, ɵNG_INJ_DEF, ɵcompileNgModuleDefs, NgZone, Compiler, COMPILER_OPTIONS, ɵNgModuleFactory, ModuleWithComponentFactories, InjectionToken, Injector, InjectFlags, ɵresetCompiledComponents, ɵflushModuleScopingQueueAsMuchAsPossible, Injectable, ɵclearOverrides, ɵoverrideComponentView, ɵINJECTOR_SCOPE, Optional, SkipSelf, ɵoverrideProvider, ɵivyEnabled } from '@angular/core';
9import { __awaiter, __decorate } from 'tslib';
10import { ResourceLoader } from '@angular/compiler';
11
12/**
13 * @license
14 * Copyright Google LLC All Rights Reserved.
15 *
16 * Use of this source code is governed by an MIT-style license that can be
17 * found in the LICENSE file at https://angular.io/license
18 */
19/**
20 * Wraps a test function in an asynchronous test zone. The test will automatically
21 * complete when all asynchronous calls within this zone are done. Can be used
22 * to wrap an {@link inject} call.
23 *
24 * Example:
25 *
26 * ```
27 * it('...', waitForAsync(inject([AClass], (object) => {
28 * object.doSomething.then(() => {
29 * expect(...);
30 * })
31 * });
32 * ```
33 *
34 * @publicApi
35 */
36function waitForAsync(fn) {
37 const _Zone = typeof Zone !== 'undefined' ? Zone : null;
38 if (!_Zone) {
39 return function () {
40 return Promise.reject('Zone is needed for the waitForAsync() test helper but could not be found. ' +
41 'Please make sure that your environment includes zone.js');
42 };
43 }
44 const asyncTest = _Zone && _Zone[_Zone.__symbol__('asyncTest')];
45 if (typeof asyncTest === 'function') {
46 return asyncTest(fn);
47 }
48 return function () {
49 return Promise.reject('zone-testing.js is needed for the async() test helper but could not be found. ' +
50 'Please make sure that your environment includes zone.js/testing');
51 };
52}
53/**
54 * @deprecated use `waitForAsync()`, (expected removal in v12)
55 * @see {@link waitForAsync}
56 * @publicApi
57 * */
58function async(fn) {
59 return waitForAsync(fn);
60}
61
62/**
63 * @license
64 * Copyright Google LLC All Rights Reserved.
65 *
66 * Use of this source code is governed by an MIT-style license that can be
67 * found in the LICENSE file at https://angular.io/license
68 */
69/**
70 * Fixture for debugging and testing a component.
71 *
72 * @publicApi
73 */
74class ComponentFixture {
75 constructor(componentRef, ngZone, _autoDetect) {
76 this.componentRef = componentRef;
77 this.ngZone = ngZone;
78 this._autoDetect = _autoDetect;
79 this._isStable = true;
80 this._isDestroyed = false;
81 this._resolve = null;
82 this._promise = null;
83 this._onUnstableSubscription = null;
84 this._onStableSubscription = null;
85 this._onMicrotaskEmptySubscription = null;
86 this._onErrorSubscription = null;
87 this.changeDetectorRef = componentRef.changeDetectorRef;
88 this.elementRef = componentRef.location;
89 this.debugElement = getDebugNode(this.elementRef.nativeElement);
90 this.componentInstance = componentRef.instance;
91 this.nativeElement = this.elementRef.nativeElement;
92 this.componentRef = componentRef;
93 this.ngZone = ngZone;
94 if (ngZone) {
95 // Create subscriptions outside the NgZone so that the callbacks run oustide
96 // of NgZone.
97 ngZone.runOutsideAngular(() => {
98 this._onUnstableSubscription = ngZone.onUnstable.subscribe({
99 next: () => {
100 this._isStable = false;
101 }
102 });
103 this._onMicrotaskEmptySubscription = ngZone.onMicrotaskEmpty.subscribe({
104 next: () => {
105 if (this._autoDetect) {
106 // Do a change detection run with checkNoChanges set to true to check
107 // there are no changes on the second run.
108 this.detectChanges(true);
109 }
110 }
111 });
112 this._onStableSubscription = ngZone.onStable.subscribe({
113 next: () => {
114 this._isStable = true;
115 // Check whether there is a pending whenStable() completer to resolve.
116 if (this._promise !== null) {
117 // If so check whether there are no pending macrotasks before resolving.
118 // Do this check in the next tick so that ngZone gets a chance to update the state of
119 // pending macrotasks.
120 scheduleMicroTask(() => {
121 if (!ngZone.hasPendingMacrotasks) {
122 if (this._promise !== null) {
123 this._resolve(true);
124 this._resolve = null;
125 this._promise = null;
126 }
127 }
128 });
129 }
130 }
131 });
132 this._onErrorSubscription = ngZone.onError.subscribe({
133 next: (error) => {
134 throw error;
135 }
136 });
137 });
138 }
139 }
140 _tick(checkNoChanges) {
141 this.changeDetectorRef.detectChanges();
142 if (checkNoChanges) {
143 this.checkNoChanges();
144 }
145 }
146 /**
147 * Trigger a change detection cycle for the component.
148 */
149 detectChanges(checkNoChanges = true) {
150 if (this.ngZone != null) {
151 // Run the change detection inside the NgZone so that any async tasks as part of the change
152 // detection are captured by the zone and can be waited for in isStable.
153 this.ngZone.run(() => {
154 this._tick(checkNoChanges);
155 });
156 }
157 else {
158 // Running without zone. Just do the change detection.
159 this._tick(checkNoChanges);
160 }
161 }
162 /**
163 * Do a change detection run to make sure there were no changes.
164 */
165 checkNoChanges() {
166 this.changeDetectorRef.checkNoChanges();
167 }
168 /**
169 * Set whether the fixture should autodetect changes.
170 *
171 * Also runs detectChanges once so that any existing change is detected.
172 */
173 autoDetectChanges(autoDetect = true) {
174 if (this.ngZone == null) {
175 throw new Error('Cannot call autoDetectChanges when ComponentFixtureNoNgZone is set');
176 }
177 this._autoDetect = autoDetect;
178 this.detectChanges();
179 }
180 /**
181 * Return whether the fixture is currently stable or has async tasks that have not been completed
182 * yet.
183 */
184 isStable() {
185 return this._isStable && !this.ngZone.hasPendingMacrotasks;
186 }
187 /**
188 * Get a promise that resolves when the fixture is stable.
189 *
190 * This can be used to resume testing after events have triggered asynchronous activity or
191 * asynchronous change detection.
192 */
193 whenStable() {
194 if (this.isStable()) {
195 return Promise.resolve(false);
196 }
197 else if (this._promise !== null) {
198 return this._promise;
199 }
200 else {
201 this._promise = new Promise(res => {
202 this._resolve = res;
203 });
204 return this._promise;
205 }
206 }
207 _getRenderer() {
208 if (this._renderer === undefined) {
209 this._renderer = this.componentRef.injector.get(RendererFactory2, null);
210 }
211 return this._renderer;
212 }
213 /**
214 * Get a promise that resolves when the ui state is stable following animations.
215 */
216 whenRenderingDone() {
217 const renderer = this._getRenderer();
218 if (renderer && renderer.whenRenderingDone) {
219 return renderer.whenRenderingDone();
220 }
221 return this.whenStable();
222 }
223 /**
224 * Trigger component destruction.
225 */
226 destroy() {
227 if (!this._isDestroyed) {
228 this.componentRef.destroy();
229 if (this._onUnstableSubscription != null) {
230 this._onUnstableSubscription.unsubscribe();
231 this._onUnstableSubscription = null;
232 }
233 if (this._onStableSubscription != null) {
234 this._onStableSubscription.unsubscribe();
235 this._onStableSubscription = null;
236 }
237 if (this._onMicrotaskEmptySubscription != null) {
238 this._onMicrotaskEmptySubscription.unsubscribe();
239 this._onMicrotaskEmptySubscription = null;
240 }
241 if (this._onErrorSubscription != null) {
242 this._onErrorSubscription.unsubscribe();
243 this._onErrorSubscription = null;
244 }
245 this._isDestroyed = true;
246 }
247 }
248}
249function scheduleMicroTask(fn) {
250 Zone.current.scheduleMicroTask('scheduleMicrotask', fn);
251}
252
253/**
254 * @license
255 * Copyright Google LLC All Rights Reserved.
256 *
257 * Use of this source code is governed by an MIT-style license that can be
258 * found in the LICENSE file at https://angular.io/license
259 */
260const _Zone = typeof Zone !== 'undefined' ? Zone : null;
261const fakeAsyncTestModule = _Zone && _Zone[_Zone.__symbol__('fakeAsyncTest')];
262const fakeAsyncTestModuleNotLoadedErrorMessage = `zone-testing.js is needed for the fakeAsync() test helper but could not be found.
263 Please make sure that your environment includes zone.js/testing`;
264/**
265 * Clears out the shared fake async zone for a test.
266 * To be called in a global `beforeEach`.
267 *
268 * @publicApi
269 */
270function resetFakeAsyncZone() {
271 if (fakeAsyncTestModule) {
272 return fakeAsyncTestModule.resetFakeAsyncZone();
273 }
274 throw new Error(fakeAsyncTestModuleNotLoadedErrorMessage);
275}
276/**
277 * Wraps a function to be executed in the `fakeAsync` zone:
278 * - Microtasks are manually executed by calling `flushMicrotasks()`.
279 * - Timers are synchronous; `tick()` simulates the asynchronous passage of time.
280 *
281 * If there are any pending timers at the end of the function, an exception is thrown.
282 *
283 * Can be used to wrap `inject()` calls.
284 *
285 * @param fn The function that you want to wrap in the `fakeAysnc` zone.
286 *
287 * @usageNotes
288 * ### Example
289 *
290 * {@example core/testing/ts/fake_async.ts region='basic'}
291 *
292 *
293 * @returns The function wrapped to be executed in the `fakeAsync` zone.
294 * Any arguments passed when calling this returned function will be passed through to the `fn`
295 * function in the parameters when it is called.
296 *
297 * @publicApi
298 */
299function fakeAsync(fn) {
300 if (fakeAsyncTestModule) {
301 return fakeAsyncTestModule.fakeAsync(fn);
302 }
303 throw new Error(fakeAsyncTestModuleNotLoadedErrorMessage);
304}
305/**
306 * Simulates the asynchronous passage of time for the timers in the `fakeAsync` zone.
307 *
308 * The microtasks queue is drained at the very start of this function and after any timer callback
309 * has been executed.
310 *
311 * @param millis The number of milliseconds to advance the virtual timer.
312 * @param tickOptions The options to pass to the `tick()` function.
313 *
314 * @usageNotes
315 *
316 * The `tick()` option is a flag called `processNewMacroTasksSynchronously`,
317 * which determines whether or not to invoke new macroTasks.
318 *
319 * If you provide a `tickOptions` object, but do not specify a
320 * `processNewMacroTasksSynchronously` property (`tick(100, {})`),
321 * then `processNewMacroTasksSynchronously` defaults to true.
322 *
323 * If you omit the `tickOptions` parameter (`tick(100))`), then
324 * `tickOptions` defaults to `{processNewMacroTasksSynchronously: true}`.
325 *
326 * ### Example
327 *
328 * {@example core/testing/ts/fake_async.ts region='basic'}
329 *
330 * The following example includes a nested timeout (new macroTask), and
331 * the `tickOptions` parameter is allowed to default. In this case,
332 * `processNewMacroTasksSynchronously` defaults to true, and the nested
333 * function is executed on each tick.
334 *
335 * ```
336 * it ('test with nested setTimeout', fakeAsync(() => {
337 * let nestedTimeoutInvoked = false;
338 * function funcWithNestedTimeout() {
339 * setTimeout(() => {
340 * nestedTimeoutInvoked = true;
341 * });
342 * };
343 * setTimeout(funcWithNestedTimeout);
344 * tick();
345 * expect(nestedTimeoutInvoked).toBe(true);
346 * }));
347 * ```
348 *
349 * In the following case, `processNewMacroTasksSynchronously` is explicitly
350 * set to false, so the nested timeout function is not invoked.
351 *
352 * ```
353 * it ('test with nested setTimeout', fakeAsync(() => {
354 * let nestedTimeoutInvoked = false;
355 * function funcWithNestedTimeout() {
356 * setTimeout(() => {
357 * nestedTimeoutInvoked = true;
358 * });
359 * };
360 * setTimeout(funcWithNestedTimeout);
361 * tick(0, {processNewMacroTasksSynchronously: false});
362 * expect(nestedTimeoutInvoked).toBe(false);
363 * }));
364 * ```
365 *
366 *
367 * @publicApi
368 */
369function tick(millis = 0, tickOptions = {
370 processNewMacroTasksSynchronously: true
371}) {
372 if (fakeAsyncTestModule) {
373 return fakeAsyncTestModule.tick(millis, tickOptions);
374 }
375 throw new Error(fakeAsyncTestModuleNotLoadedErrorMessage);
376}
377/**
378 * Simulates the asynchronous passage of time for the timers in the `fakeAsync` zone by
379 * draining the macrotask queue until it is empty.
380 *
381 * @param maxTurns The maximum number of times the scheduler attempts to clear its queue before
382 * throwing an error.
383 * @returns The simulated time elapsed, in milliseconds.
384 *
385 * @publicApi
386 */
387function flush(maxTurns) {
388 if (fakeAsyncTestModule) {
389 return fakeAsyncTestModule.flush(maxTurns);
390 }
391 throw new Error(fakeAsyncTestModuleNotLoadedErrorMessage);
392}
393/**
394 * Discard all remaining periodic tasks.
395 *
396 * @publicApi
397 */
398function discardPeriodicTasks() {
399 if (fakeAsyncTestModule) {
400 return fakeAsyncTestModule.discardPeriodicTasks();
401 }
402 throw new Error(fakeAsyncTestModuleNotLoadedErrorMessage);
403}
404/**
405 * Flush any pending microtasks.
406 *
407 * @publicApi
408 */
409function flushMicrotasks() {
410 if (fakeAsyncTestModule) {
411 return fakeAsyncTestModule.flushMicrotasks();
412 }
413 throw new Error(fakeAsyncTestModuleNotLoadedErrorMessage);
414}
415
416/**
417 * @license
418 * Copyright Google LLC All Rights Reserved.
419 *
420 * Use of this source code is governed by an MIT-style license that can be
421 * found in the LICENSE file at https://angular.io/license
422 */
423/**
424 * Used to resolve resource URLs on `@Component` when used with JIT compilation.
425 *
426 * Example:
427 * ```
428 * @Component({
429 * selector: 'my-comp',
430 * templateUrl: 'my-comp.html', // This requires asynchronous resolution
431 * })
432 * class MyComponent{
433 * }
434 *
435 * // Calling `renderComponent` will fail because `renderComponent` is a synchronous process
436 * // and `MyComponent`'s `@Component.templateUrl` needs to be resolved asynchronously.
437 *
438 * // Calling `resolveComponentResources()` will resolve `@Component.templateUrl` into
439 * // `@Component.template`, which allows `renderComponent` to proceed in a synchronous manner.
440 *
441 * // Use browser's `fetch()` function as the default resource resolution strategy.
442 * resolveComponentResources(fetch).then(() => {
443 * // After resolution all URLs have been converted into `template` strings.
444 * renderComponent(MyComponent);
445 * });
446 *
447 * ```
448 *
449 * NOTE: In AOT the resolution happens during compilation, and so there should be no need
450 * to call this method outside JIT mode.
451 *
452 * @param resourceResolver a function which is responsible for returning a `Promise` to the
453 * contents of the resolved URL. Browser's `fetch()` method is a good default implementation.
454 */
455function resolveComponentResources(resourceResolver) {
456 // Store all promises which are fetching the resources.
457 const componentResolved = [];
458 // Cache so that we don't fetch the same resource more than once.
459 const urlMap = new Map();
460 function cachedResourceResolve(url) {
461 let promise = urlMap.get(url);
462 if (!promise) {
463 const resp = resourceResolver(url);
464 urlMap.set(url, promise = resp.then(unwrapResponse));
465 }
466 return promise;
467 }
468 componentResourceResolutionQueue.forEach((component, type) => {
469 const promises = [];
470 if (component.templateUrl) {
471 promises.push(cachedResourceResolve(component.templateUrl).then((template) => {
472 component.template = template;
473 }));
474 }
475 const styleUrls = component.styleUrls;
476 const styles = component.styles || (component.styles = []);
477 const styleOffset = component.styles.length;
478 styleUrls && styleUrls.forEach((styleUrl, index) => {
479 styles.push(''); // pre-allocate array.
480 promises.push(cachedResourceResolve(styleUrl).then((style) => {
481 styles[styleOffset + index] = style;
482 styleUrls.splice(styleUrls.indexOf(styleUrl), 1);
483 if (styleUrls.length == 0) {
484 component.styleUrls = undefined;
485 }
486 }));
487 });
488 const fullyResolved = Promise.all(promises).then(() => componentDefResolved(type));
489 componentResolved.push(fullyResolved);
490 });
491 clearResolutionOfComponentResourcesQueue();
492 return Promise.all(componentResolved).then(() => undefined);
493}
494let componentResourceResolutionQueue = new Map();
495// Track when existing ɵcmp for a Type is waiting on resources.
496const componentDefPendingResolution = new Set();
497function maybeQueueResolutionOfComponentResources(type, metadata) {
498 if (componentNeedsResolution(metadata)) {
499 componentResourceResolutionQueue.set(type, metadata);
500 componentDefPendingResolution.add(type);
501 }
502}
503function isComponentDefPendingResolution(type) {
504 return componentDefPendingResolution.has(type);
505}
506function componentNeedsResolution(component) {
507 return !!((component.templateUrl && !component.hasOwnProperty('template')) ||
508 component.styleUrls && component.styleUrls.length);
509}
510function clearResolutionOfComponentResourcesQueue() {
511 const old = componentResourceResolutionQueue;
512 componentResourceResolutionQueue = new Map();
513 return old;
514}
515function restoreComponentResolutionQueue(queue) {
516 componentDefPendingResolution.clear();
517 queue.forEach((_, type) => componentDefPendingResolution.add(type));
518 componentResourceResolutionQueue = queue;
519}
520function isComponentResourceResolutionQueueEmpty() {
521 return componentResourceResolutionQueue.size === 0;
522}
523function unwrapResponse(response) {
524 return typeof response == 'string' ? response : response.text();
525}
526function componentDefResolved(type) {
527 componentDefPendingResolution.delete(type);
528}
529
530/**
531 * @license
532 * Copyright Google LLC All Rights Reserved.
533 *
534 * Use of this source code is governed by an MIT-style license that can be
535 * found in the LICENSE file at https://angular.io/license
536 */
537let _nextReferenceId = 0;
538class MetadataOverrider {
539 constructor() {
540 this._references = new Map();
541 }
542 /**
543 * Creates a new instance for the given metadata class
544 * based on an old instance and overrides.
545 */
546 overrideMetadata(metadataClass, oldMetadata, override) {
547 const props = {};
548 if (oldMetadata) {
549 _valueProps(oldMetadata).forEach((prop) => props[prop] = oldMetadata[prop]);
550 }
551 if (override.set) {
552 if (override.remove || override.add) {
553 throw new Error(`Cannot set and add/remove ${ɵstringify(metadataClass)} at the same time!`);
554 }
555 setMetadata(props, override.set);
556 }
557 if (override.remove) {
558 removeMetadata(props, override.remove, this._references);
559 }
560 if (override.add) {
561 addMetadata(props, override.add);
562 }
563 return new metadataClass(props);
564 }
565}
566function removeMetadata(metadata, remove, references) {
567 const removeObjects = new Set();
568 for (const prop in remove) {
569 const removeValue = remove[prop];
570 if (Array.isArray(removeValue)) {
571 removeValue.forEach((value) => {
572 removeObjects.add(_propHashKey(prop, value, references));
573 });
574 }
575 else {
576 removeObjects.add(_propHashKey(prop, removeValue, references));
577 }
578 }
579 for (const prop in metadata) {
580 const propValue = metadata[prop];
581 if (Array.isArray(propValue)) {
582 metadata[prop] = propValue.filter((value) => !removeObjects.has(_propHashKey(prop, value, references)));
583 }
584 else {
585 if (removeObjects.has(_propHashKey(prop, propValue, references))) {
586 metadata[prop] = undefined;
587 }
588 }
589 }
590}
591function addMetadata(metadata, add) {
592 for (const prop in add) {
593 const addValue = add[prop];
594 const propValue = metadata[prop];
595 if (propValue != null && Array.isArray(propValue)) {
596 metadata[prop] = propValue.concat(addValue);
597 }
598 else {
599 metadata[prop] = addValue;
600 }
601 }
602}
603function setMetadata(metadata, set) {
604 for (const prop in set) {
605 metadata[prop] = set[prop];
606 }
607}
608function _propHashKey(propName, propValue, references) {
609 const replacer = (key, value) => {
610 if (typeof value === 'function') {
611 value = _serializeReference(value, references);
612 }
613 return value;
614 };
615 return `${propName}:${JSON.stringify(propValue, replacer)}`;
616}
617function _serializeReference(ref, references) {
618 let id = references.get(ref);
619 if (!id) {
620 id = `${ɵstringify(ref)}${_nextReferenceId++}`;
621 references.set(ref, id);
622 }
623 return id;
624}
625function _valueProps(obj) {
626 const props = [];
627 // regular public props
628 Object.keys(obj).forEach((prop) => {
629 if (!prop.startsWith('_')) {
630 props.push(prop);
631 }
632 });
633 // getters
634 let proto = obj;
635 while (proto = Object.getPrototypeOf(proto)) {
636 Object.keys(proto).forEach((protoProp) => {
637 const desc = Object.getOwnPropertyDescriptor(proto, protoProp);
638 if (!protoProp.startsWith('_') && desc && 'get' in desc) {
639 props.push(protoProp);
640 }
641 });
642 }
643 return props;
644}
645
646/**
647 * @license
648 * Copyright Google LLC All Rights Reserved.
649 *
650 * Use of this source code is governed by an MIT-style license that can be
651 * found in the LICENSE file at https://angular.io/license
652 */
653const reflection = new ɵReflectionCapabilities();
654/**
655 * Allows to override ivy metadata for tests (via the `TestBed`).
656 */
657class OverrideResolver {
658 constructor() {
659 this.overrides = new Map();
660 this.resolved = new Map();
661 }
662 addOverride(type, override) {
663 const overrides = this.overrides.get(type) || [];
664 overrides.push(override);
665 this.overrides.set(type, overrides);
666 this.resolved.delete(type);
667 }
668 setOverrides(overrides) {
669 this.overrides.clear();
670 overrides.forEach(([type, override]) => {
671 this.addOverride(type, override);
672 });
673 }
674 getAnnotation(type) {
675 const annotations = reflection.annotations(type);
676 // Try to find the nearest known Type annotation and make sure that this annotation is an
677 // instance of the type we are looking for, so we can use it for resolution. Note: there might
678 // be multiple known annotations found due to the fact that Components can extend Directives (so
679 // both Directive and Component annotations would be present), so we always check if the known
680 // annotation has the right type.
681 for (let i = annotations.length - 1; i >= 0; i--) {
682 const annotation = annotations[i];
683 const isKnownType = annotation instanceof Directive || annotation instanceof Component ||
684 annotation instanceof Pipe || annotation instanceof NgModule;
685 if (isKnownType) {
686 return annotation instanceof this.type ? annotation : null;
687 }
688 }
689 return null;
690 }
691 resolve(type) {
692 let resolved = this.resolved.get(type) || null;
693 if (!resolved) {
694 resolved = this.getAnnotation(type);
695 if (resolved) {
696 const overrides = this.overrides.get(type);
697 if (overrides) {
698 const overrider = new MetadataOverrider();
699 overrides.forEach(override => {
700 resolved = overrider.overrideMetadata(this.type, resolved, override);
701 });
702 }
703 }
704 this.resolved.set(type, resolved);
705 }
706 return resolved;
707 }
708}
709class DirectiveResolver extends OverrideResolver {
710 get type() {
711 return Directive;
712 }
713}
714class ComponentResolver extends OverrideResolver {
715 get type() {
716 return Component;
717 }
718}
719class PipeResolver extends OverrideResolver {
720 get type() {
721 return Pipe;
722 }
723}
724class NgModuleResolver extends OverrideResolver {
725 get type() {
726 return NgModule;
727 }
728}
729
730var TestingModuleOverride;
731(function (TestingModuleOverride) {
732 TestingModuleOverride[TestingModuleOverride["DECLARATION"] = 0] = "DECLARATION";
733 TestingModuleOverride[TestingModuleOverride["OVERRIDE_TEMPLATE"] = 1] = "OVERRIDE_TEMPLATE";
734})(TestingModuleOverride || (TestingModuleOverride = {}));
735function isTestingModuleOverride(value) {
736 return value === TestingModuleOverride.DECLARATION ||
737 value === TestingModuleOverride.OVERRIDE_TEMPLATE;
738}
739class R3TestBedCompiler {
740 constructor(platform, additionalModuleTypes) {
741 this.platform = platform;
742 this.additionalModuleTypes = additionalModuleTypes;
743 this.originalComponentResolutionQueue = null;
744 // Testing module configuration
745 this.declarations = [];
746 this.imports = [];
747 this.providers = [];
748 this.schemas = [];
749 // Queues of components/directives/pipes that should be recompiled.
750 this.pendingComponents = new Set();
751 this.pendingDirectives = new Set();
752 this.pendingPipes = new Set();
753 // Keep track of all components and directives, so we can patch Providers onto defs later.
754 this.seenComponents = new Set();
755 this.seenDirectives = new Set();
756 // Keep track of overridden modules, so that we can collect all affected ones in the module tree.
757 this.overriddenModules = new Set();
758 // Store resolved styles for Components that have template overrides present and `styleUrls`
759 // defined at the same time.
760 this.existingComponentStyles = new Map();
761 this.resolvers = initResolvers();
762 this.componentToModuleScope = new Map();
763 // Map that keeps initial version of component/directive/pipe defs in case
764 // we compile a Type again, thus overriding respective static fields. This is
765 // required to make sure we restore defs to their initial states between test runs
766 // TODO: we should support the case with multiple defs on a type
767 this.initialNgDefs = new Map();
768 // Array that keeps cleanup operations for initial versions of component/directive/pipe/module
769 // defs in case TestBed makes changes to the originals.
770 this.defCleanupOps = [];
771 this._injector = null;
772 this.compilerProviders = null;
773 this.providerOverrides = [];
774 this.rootProviderOverrides = [];
775 // Overrides for injectables with `{providedIn: SomeModule}` need to be tracked and added to that
776 // module's provider list.
777 this.providerOverridesByModule = new Map();
778 this.providerOverridesByToken = new Map();
779 this.moduleProvidersOverridden = new Set();
780 this.testModuleRef = null;
781 class DynamicTestModule {
782 }
783 this.testModuleType = DynamicTestModule;
784 }
785 setCompilerProviders(providers) {
786 this.compilerProviders = providers;
787 this._injector = null;
788 }
789 configureTestingModule(moduleDef) {
790 // Enqueue any compilation tasks for the directly declared component.
791 if (moduleDef.declarations !== undefined) {
792 this.queueTypeArray(moduleDef.declarations, TestingModuleOverride.DECLARATION);
793 this.declarations.push(...moduleDef.declarations);
794 }
795 // Enqueue any compilation tasks for imported modules.
796 if (moduleDef.imports !== undefined) {
797 this.queueTypesFromModulesArray(moduleDef.imports);
798 this.imports.push(...moduleDef.imports);
799 }
800 if (moduleDef.providers !== undefined) {
801 this.providers.push(...moduleDef.providers);
802 }
803 if (moduleDef.schemas !== undefined) {
804 this.schemas.push(...moduleDef.schemas);
805 }
806 }
807 overrideModule(ngModule, override) {
808 this.overriddenModules.add(ngModule);
809 // Compile the module right away.
810 this.resolvers.module.addOverride(ngModule, override);
811 const metadata = this.resolvers.module.resolve(ngModule);
812 if (metadata === null) {
813 throw invalidTypeError(ngModule.name, 'NgModule');
814 }
815 this.recompileNgModule(ngModule, metadata);
816 // At this point, the module has a valid module def (ɵmod), but the override may have introduced
817 // new declarations or imported modules. Ingest any possible new types and add them to the
818 // current queue.
819 this.queueTypesFromModulesArray([ngModule]);
820 }
821 overrideComponent(component, override) {
822 this.resolvers.component.addOverride(component, override);
823 this.pendingComponents.add(component);
824 }
825 overrideDirective(directive, override) {
826 this.resolvers.directive.addOverride(directive, override);
827 this.pendingDirectives.add(directive);
828 }
829 overridePipe(pipe, override) {
830 this.resolvers.pipe.addOverride(pipe, override);
831 this.pendingPipes.add(pipe);
832 }
833 overrideProvider(token, provider) {
834 let providerDef;
835 if (provider.useFactory !== undefined) {
836 providerDef = {
837 provide: token,
838 useFactory: provider.useFactory,
839 deps: provider.deps || [],
840 multi: provider.multi
841 };
842 }
843 else if (provider.useValue !== undefined) {
844 providerDef = { provide: token, useValue: provider.useValue, multi: provider.multi };
845 }
846 else {
847 providerDef = { provide: token };
848 }
849 const injectableDef = typeof token !== 'string' ? ɵgetInjectableDef(token) : null;
850 const providedIn = injectableDef === null ? null : resolveForwardRef(injectableDef.providedIn);
851 const overridesBucket = providedIn === 'root' ? this.rootProviderOverrides : this.providerOverrides;
852 overridesBucket.push(providerDef);
853 // Keep overrides grouped by token as well for fast lookups using token
854 this.providerOverridesByToken.set(token, providerDef);
855 if (injectableDef !== null && providedIn !== null && typeof providedIn !== 'string') {
856 const existingOverrides = this.providerOverridesByModule.get(providedIn);
857 if (existingOverrides !== undefined) {
858 existingOverrides.push(providerDef);
859 }
860 else {
861 this.providerOverridesByModule.set(providedIn, [providerDef]);
862 }
863 }
864 }
865 overrideTemplateUsingTestingModule(type, template) {
866 const def = type[ɵNG_COMP_DEF];
867 const hasStyleUrls = () => {
868 const metadata = this.resolvers.component.resolve(type);
869 return !!metadata.styleUrls && metadata.styleUrls.length > 0;
870 };
871 const overrideStyleUrls = !!def && !isComponentDefPendingResolution(type) && hasStyleUrls();
872 // In Ivy, compiling a component does not require knowing the module providing the
873 // component's scope, so overrideTemplateUsingTestingModule can be implemented purely via
874 // overrideComponent. Important: overriding template requires full Component re-compilation,
875 // which may fail in case styleUrls are also present (thus Component is considered as required
876 // resolution). In order to avoid this, we preemptively set styleUrls to an empty array,
877 // preserve current styles available on Component def and restore styles back once compilation
878 // is complete.
879 const override = overrideStyleUrls ? { template, styles: [], styleUrls: [] } : { template };
880 this.overrideComponent(type, { set: override });
881 if (overrideStyleUrls && def.styles && def.styles.length > 0) {
882 this.existingComponentStyles.set(type, def.styles);
883 }
884 // Set the component's scope to be the testing module.
885 this.componentToModuleScope.set(type, TestingModuleOverride.OVERRIDE_TEMPLATE);
886 }
887 compileComponents() {
888 return __awaiter(this, void 0, void 0, function* () {
889 this.clearComponentResolutionQueue();
890 // Run compilers for all queued types.
891 let needsAsyncResources = this.compileTypesSync();
892 // compileComponents() should not be async unless it needs to be.
893 if (needsAsyncResources) {
894 let resourceLoader;
895 let resolver = (url) => {
896 if (!resourceLoader) {
897 resourceLoader = this.injector.get(ResourceLoader);
898 }
899 return Promise.resolve(resourceLoader.get(url));
900 };
901 yield resolveComponentResources(resolver);
902 }
903 });
904 }
905 finalize() {
906 // One last compile
907 this.compileTypesSync();
908 // Create the testing module itself.
909 this.compileTestModule();
910 this.applyTransitiveScopes();
911 this.applyProviderOverrides();
912 // Patch previously stored `styles` Component values (taken from ɵcmp), in case these
913 // Components have `styleUrls` fields defined and template override was requested.
914 this.patchComponentsWithExistingStyles();
915 // Clear the componentToModuleScope map, so that future compilations don't reset the scope of
916 // every component.
917 this.componentToModuleScope.clear();
918 const parentInjector = this.platform.injector;
919 this.testModuleRef = new ɵRender3NgModuleRef(this.testModuleType, parentInjector);
920 // ApplicationInitStatus.runInitializers() is marked @internal to core.
921 // Cast it to any before accessing it.
922 this.testModuleRef.injector.get(ApplicationInitStatus).runInitializers();
923 // Set locale ID after running app initializers, since locale information might be updated while
924 // running initializers. This is also consistent with the execution order while bootstrapping an
925 // app (see `packages/core/src/application_ref.ts` file).
926 const localeId = this.testModuleRef.injector.get(LOCALE_ID, ɵDEFAULT_LOCALE_ID);
927 ɵsetLocaleId(localeId);
928 return this.testModuleRef;
929 }
930 /**
931 * @internal
932 */
933 _compileNgModuleSync(moduleType) {
934 this.queueTypesFromModulesArray([moduleType]);
935 this.compileTypesSync();
936 this.applyProviderOverrides();
937 this.applyProviderOverridesToModule(moduleType);
938 this.applyTransitiveScopes();
939 }
940 /**
941 * @internal
942 */
943 _compileNgModuleAsync(moduleType) {
944 return __awaiter(this, void 0, void 0, function* () {
945 this.queueTypesFromModulesArray([moduleType]);
946 yield this.compileComponents();
947 this.applyProviderOverrides();
948 this.applyProviderOverridesToModule(moduleType);
949 this.applyTransitiveScopes();
950 });
951 }
952 /**
953 * @internal
954 */
955 _getModuleResolver() {
956 return this.resolvers.module;
957 }
958 /**
959 * @internal
960 */
961 _getComponentFactories(moduleType) {
962 return maybeUnwrapFn(moduleType.ɵmod.declarations).reduce((factories, declaration) => {
963 const componentDef = declaration.ɵcmp;
964 componentDef && factories.push(new ɵRender3ComponentFactory(componentDef, this.testModuleRef));
965 return factories;
966 }, []);
967 }
968 compileTypesSync() {
969 // Compile all queued components, directives, pipes.
970 let needsAsyncResources = false;
971 this.pendingComponents.forEach(declaration => {
972 needsAsyncResources = needsAsyncResources || isComponentDefPendingResolution(declaration);
973 const metadata = this.resolvers.component.resolve(declaration);
974 if (metadata === null) {
975 throw invalidTypeError(declaration.name, 'Component');
976 }
977 this.maybeStoreNgDef(ɵNG_COMP_DEF, declaration);
978 ɵcompileComponent(declaration, metadata);
979 });
980 this.pendingComponents.clear();
981 this.pendingDirectives.forEach(declaration => {
982 const metadata = this.resolvers.directive.resolve(declaration);
983 if (metadata === null) {
984 throw invalidTypeError(declaration.name, 'Directive');
985 }
986 this.maybeStoreNgDef(ɵNG_DIR_DEF, declaration);
987 ɵcompileDirective(declaration, metadata);
988 });
989 this.pendingDirectives.clear();
990 this.pendingPipes.forEach(declaration => {
991 const metadata = this.resolvers.pipe.resolve(declaration);
992 if (metadata === null) {
993 throw invalidTypeError(declaration.name, 'Pipe');
994 }
995 this.maybeStoreNgDef(ɵNG_PIPE_DEF, declaration);
996 ɵcompilePipe(declaration, metadata);
997 });
998 this.pendingPipes.clear();
999 return needsAsyncResources;
1000 }
1001 applyTransitiveScopes() {
1002 if (this.overriddenModules.size > 0) {
1003 // Module overrides (via `TestBed.overrideModule`) might affect scopes that were previously
1004 // calculated and stored in `transitiveCompileScopes`. If module overrides are present,
1005 // collect all affected modules and reset scopes to force their re-calculation.
1006 const testingModuleDef = this.testModuleType[ɵNG_MOD_DEF];
1007 const affectedModules = this.collectModulesAffectedByOverrides(testingModuleDef.imports);
1008 if (affectedModules.size > 0) {
1009 affectedModules.forEach(moduleType => {
1010 this.storeFieldOfDefOnType(moduleType, ɵNG_MOD_DEF, 'transitiveCompileScopes');
1011 moduleType[ɵNG_MOD_DEF].transitiveCompileScopes = null;
1012 });
1013 }
1014 }
1015 const moduleToScope = new Map();
1016 const getScopeOfModule = (moduleType) => {
1017 if (!moduleToScope.has(moduleType)) {
1018 const isTestingModule = isTestingModuleOverride(moduleType);
1019 const realType = isTestingModule ? this.testModuleType : moduleType;
1020 moduleToScope.set(moduleType, ɵtransitiveScopesFor(realType));
1021 }
1022 return moduleToScope.get(moduleType);
1023 };
1024 this.componentToModuleScope.forEach((moduleType, componentType) => {
1025 const moduleScope = getScopeOfModule(moduleType);
1026 this.storeFieldOfDefOnType(componentType, ɵNG_COMP_DEF, 'directiveDefs');
1027 this.storeFieldOfDefOnType(componentType, ɵNG_COMP_DEF, 'pipeDefs');
1028 // `tView` that is stored on component def contains information about directives and pipes
1029 // that are in the scope of this component. Patching component scope will cause `tView` to be
1030 // changed. Store original `tView` before patching scope, so the `tView` (including scope
1031 // information) is restored back to its previous/original state before running next test.
1032 this.storeFieldOfDefOnType(componentType, ɵNG_COMP_DEF, 'tView');
1033 ɵpatchComponentDefWithScope(componentType.ɵcmp, moduleScope);
1034 });
1035 this.componentToModuleScope.clear();
1036 }
1037 applyProviderOverrides() {
1038 const maybeApplyOverrides = (field) => (type) => {
1039 const resolver = field === ɵNG_COMP_DEF ? this.resolvers.component : this.resolvers.directive;
1040 const metadata = resolver.resolve(type);
1041 if (this.hasProviderOverrides(metadata.providers)) {
1042 this.patchDefWithProviderOverrides(type, field);
1043 }
1044 };
1045 this.seenComponents.forEach(maybeApplyOverrides(ɵNG_COMP_DEF));
1046 this.seenDirectives.forEach(maybeApplyOverrides(ɵNG_DIR_DEF));
1047 this.seenComponents.clear();
1048 this.seenDirectives.clear();
1049 }
1050 applyProviderOverridesToModule(moduleType) {
1051 if (this.moduleProvidersOverridden.has(moduleType)) {
1052 return;
1053 }
1054 this.moduleProvidersOverridden.add(moduleType);
1055 const injectorDef = moduleType[ɵNG_INJ_DEF];
1056 if (this.providerOverridesByToken.size > 0) {
1057 const providers = [
1058 ...injectorDef.providers,
1059 ...(this.providerOverridesByModule.get(moduleType) || [])
1060 ];
1061 if (this.hasProviderOverrides(providers)) {
1062 this.maybeStoreNgDef(ɵNG_INJ_DEF, moduleType);
1063 this.storeFieldOfDefOnType(moduleType, ɵNG_INJ_DEF, 'providers');
1064 injectorDef.providers = this.getOverriddenProviders(providers);
1065 }
1066 // Apply provider overrides to imported modules recursively
1067 const moduleDef = moduleType[ɵNG_MOD_DEF];
1068 const imports = maybeUnwrapFn(moduleDef.imports);
1069 for (const importedModule of imports) {
1070 this.applyProviderOverridesToModule(importedModule);
1071 }
1072 // Also override the providers on any ModuleWithProviders imports since those don't appear in
1073 // the moduleDef.
1074 for (const importedModule of flatten(injectorDef.imports)) {
1075 if (isModuleWithProviders(importedModule)) {
1076 this.defCleanupOps.push({
1077 object: importedModule,
1078 fieldName: 'providers',
1079 originalValue: importedModule.providers
1080 });
1081 importedModule.providers = this.getOverriddenProviders(importedModule.providers);
1082 }
1083 }
1084 }
1085 }
1086 patchComponentsWithExistingStyles() {
1087 this.existingComponentStyles.forEach((styles, type) => type[ɵNG_COMP_DEF].styles = styles);
1088 this.existingComponentStyles.clear();
1089 }
1090 queueTypeArray(arr, moduleType) {
1091 for (const value of arr) {
1092 if (Array.isArray(value)) {
1093 this.queueTypeArray(value, moduleType);
1094 }
1095 else {
1096 this.queueType(value, moduleType);
1097 }
1098 }
1099 }
1100 recompileNgModule(ngModule, metadata) {
1101 // Cache the initial ngModuleDef as it will be overwritten.
1102 this.maybeStoreNgDef(ɵNG_MOD_DEF, ngModule);
1103 this.maybeStoreNgDef(ɵNG_INJ_DEF, ngModule);
1104 ɵcompileNgModuleDefs(ngModule, metadata);
1105 }
1106 queueType(type, moduleType) {
1107 const component = this.resolvers.component.resolve(type);
1108 if (component) {
1109 // Check whether a give Type has respective NG def (ɵcmp) and compile if def is
1110 // missing. That might happen in case a class without any Angular decorators extends another
1111 // class where Component/Directive/Pipe decorator is defined.
1112 if (isComponentDefPendingResolution(type) || !type.hasOwnProperty(ɵNG_COMP_DEF)) {
1113 this.pendingComponents.add(type);
1114 }
1115 this.seenComponents.add(type);
1116 // Keep track of the module which declares this component, so later the component's scope
1117 // can be set correctly. If the component has already been recorded here, then one of several
1118 // cases is true:
1119 // * the module containing the component was imported multiple times (common).
1120 // * the component is declared in multiple modules (which is an error).
1121 // * the component was in 'declarations' of the testing module, and also in an imported module
1122 // in which case the module scope will be TestingModuleOverride.DECLARATION.
1123 // * overrideTemplateUsingTestingModule was called for the component in which case the module
1124 // scope will be TestingModuleOverride.OVERRIDE_TEMPLATE.
1125 //
1126 // If the component was previously in the testing module's 'declarations' (meaning the
1127 // current value is TestingModuleOverride.DECLARATION), then `moduleType` is the component's
1128 // real module, which was imported. This pattern is understood to mean that the component
1129 // should use its original scope, but that the testing module should also contain the
1130 // component in its scope.
1131 if (!this.componentToModuleScope.has(type) ||
1132 this.componentToModuleScope.get(type) === TestingModuleOverride.DECLARATION) {
1133 this.componentToModuleScope.set(type, moduleType);
1134 }
1135 return;
1136 }
1137 const directive = this.resolvers.directive.resolve(type);
1138 if (directive) {
1139 if (!type.hasOwnProperty(ɵNG_DIR_DEF)) {
1140 this.pendingDirectives.add(type);
1141 }
1142 this.seenDirectives.add(type);
1143 return;
1144 }
1145 const pipe = this.resolvers.pipe.resolve(type);
1146 if (pipe && !type.hasOwnProperty(ɵNG_PIPE_DEF)) {
1147 this.pendingPipes.add(type);
1148 return;
1149 }
1150 }
1151 queueTypesFromModulesArray(arr) {
1152 // Because we may encounter the same NgModule while processing the imports and exports of an
1153 // NgModule tree, we cache them in this set so we can skip ones that have already been seen
1154 // encountered. In some test setups, this caching resulted in 10X runtime improvement.
1155 const processedNgModuleDefs = new Set();
1156 const queueTypesFromModulesArrayRecur = (arr) => {
1157 for (const value of arr) {
1158 if (Array.isArray(value)) {
1159 queueTypesFromModulesArrayRecur(value);
1160 }
1161 else if (hasNgModuleDef(value)) {
1162 const def = value.ɵmod;
1163 if (processedNgModuleDefs.has(def)) {
1164 continue;
1165 }
1166 processedNgModuleDefs.add(def);
1167 // Look through declarations, imports, and exports, and queue
1168 // everything found there.
1169 this.queueTypeArray(maybeUnwrapFn(def.declarations), value);
1170 queueTypesFromModulesArrayRecur(maybeUnwrapFn(def.imports));
1171 queueTypesFromModulesArrayRecur(maybeUnwrapFn(def.exports));
1172 }
1173 else if (isModuleWithProviders(value)) {
1174 queueTypesFromModulesArrayRecur([value.ngModule]);
1175 }
1176 }
1177 };
1178 queueTypesFromModulesArrayRecur(arr);
1179 }
1180 // When module overrides (via `TestBed.overrideModule`) are present, it might affect all modules
1181 // that import (even transitively) an overridden one. For all affected modules we need to
1182 // recalculate their scopes for a given test run and restore original scopes at the end. The goal
1183 // of this function is to collect all affected modules in a set for further processing. Example:
1184 // if we have the following module hierarchy: A -> B -> C (where `->` means `imports`) and module
1185 // `C` is overridden, we consider `A` and `B` as affected, since their scopes might become
1186 // invalidated with the override.
1187 collectModulesAffectedByOverrides(arr) {
1188 const seenModules = new Set();
1189 const affectedModules = new Set();
1190 const calcAffectedModulesRecur = (arr, path) => {
1191 for (const value of arr) {
1192 if (Array.isArray(value)) {
1193 // If the value is an array, just flatten it (by invoking this function recursively),
1194 // keeping "path" the same.
1195 calcAffectedModulesRecur(value, path);
1196 }
1197 else if (hasNgModuleDef(value)) {
1198 if (seenModules.has(value)) {
1199 // If we've seen this module before and it's included into "affected modules" list, mark
1200 // the whole path that leads to that module as affected, but do not descend into its
1201 // imports, since we already examined them before.
1202 if (affectedModules.has(value)) {
1203 path.forEach(item => affectedModules.add(item));
1204 }
1205 continue;
1206 }
1207 seenModules.add(value);
1208 if (this.overriddenModules.has(value)) {
1209 path.forEach(item => affectedModules.add(item));
1210 }
1211 // Examine module imports recursively to look for overridden modules.
1212 const moduleDef = value[ɵNG_MOD_DEF];
1213 calcAffectedModulesRecur(maybeUnwrapFn(moduleDef.imports), path.concat(value));
1214 }
1215 }
1216 };
1217 calcAffectedModulesRecur(arr, []);
1218 return affectedModules;
1219 }
1220 maybeStoreNgDef(prop, type) {
1221 if (!this.initialNgDefs.has(type)) {
1222 const currentDef = Object.getOwnPropertyDescriptor(type, prop);
1223 this.initialNgDefs.set(type, [prop, currentDef]);
1224 }
1225 }
1226 storeFieldOfDefOnType(type, defField, fieldName) {
1227 const def = type[defField];
1228 const originalValue = def[fieldName];
1229 this.defCleanupOps.push({ object: def, fieldName, originalValue });
1230 }
1231 /**
1232 * Clears current components resolution queue, but stores the state of the queue, so we can
1233 * restore it later. Clearing the queue is required before we try to compile components (via
1234 * `TestBed.compileComponents`), so that component defs are in sync with the resolution queue.
1235 */
1236 clearComponentResolutionQueue() {
1237 if (this.originalComponentResolutionQueue === null) {
1238 this.originalComponentResolutionQueue = new Map();
1239 }
1240 clearResolutionOfComponentResourcesQueue().forEach((value, key) => this.originalComponentResolutionQueue.set(key, value));
1241 }
1242 /*
1243 * Restores component resolution queue to the previously saved state. This operation is performed
1244 * as a part of restoring the state after completion of the current set of tests (that might
1245 * potentially mutate the state).
1246 */
1247 restoreComponentResolutionQueue() {
1248 if (this.originalComponentResolutionQueue !== null) {
1249 restoreComponentResolutionQueue(this.originalComponentResolutionQueue);
1250 this.originalComponentResolutionQueue = null;
1251 }
1252 }
1253 restoreOriginalState() {
1254 // Process cleanup ops in reverse order so the field's original value is restored correctly (in
1255 // case there were multiple overrides for the same field).
1256 forEachRight(this.defCleanupOps, (op) => {
1257 op.object[op.fieldName] = op.originalValue;
1258 });
1259 // Restore initial component/directive/pipe defs
1260 this.initialNgDefs.forEach((value, type) => {
1261 const [prop, descriptor] = value;
1262 if (!descriptor) {
1263 // Delete operations are generally undesirable since they have performance implications
1264 // on objects they were applied to. In this particular case, situations where this code
1265 // is invoked should be quite rare to cause any noticeable impact, since it's applied
1266 // only to some test cases (for example when class with no annotations extends some
1267 // @Component) when we need to clear 'ɵcmp' field on a given class to restore
1268 // its original state (before applying overrides and running tests).
1269 delete type[prop];
1270 }
1271 else {
1272 Object.defineProperty(type, prop, descriptor);
1273 }
1274 });
1275 this.initialNgDefs.clear();
1276 this.moduleProvidersOverridden.clear();
1277 this.restoreComponentResolutionQueue();
1278 // Restore the locale ID to the default value, this shouldn't be necessary but we never know
1279 ɵsetLocaleId(ɵDEFAULT_LOCALE_ID);
1280 }
1281 compileTestModule() {
1282 class RootScopeModule {
1283 }
1284 ɵcompileNgModuleDefs(RootScopeModule, {
1285 providers: [...this.rootProviderOverrides],
1286 });
1287 const ngZone = new NgZone({ enableLongStackTrace: true });
1288 const providers = [
1289 { provide: NgZone, useValue: ngZone },
1290 { provide: Compiler, useFactory: () => new R3TestCompiler(this) },
1291 ...this.providers,
1292 ...this.providerOverrides,
1293 ];
1294 const imports = [RootScopeModule, this.additionalModuleTypes, this.imports || []];
1295 // clang-format off
1296 ɵcompileNgModuleDefs(this.testModuleType, {
1297 declarations: this.declarations,
1298 imports,
1299 schemas: this.schemas,
1300 providers,
1301 }, /* allowDuplicateDeclarationsInRoot */ true);
1302 // clang-format on
1303 this.applyProviderOverridesToModule(this.testModuleType);
1304 }
1305 get injector() {
1306 if (this._injector !== null) {
1307 return this._injector;
1308 }
1309 const providers = [];
1310 const compilerOptions = this.platform.injector.get(COMPILER_OPTIONS);
1311 compilerOptions.forEach(opts => {
1312 if (opts.providers) {
1313 providers.push(opts.providers);
1314 }
1315 });
1316 if (this.compilerProviders !== null) {
1317 providers.push(...this.compilerProviders);
1318 }
1319 // TODO(ocombe): make this work with an Injector directly instead of creating a module for it
1320 class CompilerModule {
1321 }
1322 ɵcompileNgModuleDefs(CompilerModule, { providers });
1323 const CompilerModuleFactory = new ɵNgModuleFactory(CompilerModule);
1324 this._injector = CompilerModuleFactory.create(this.platform.injector).injector;
1325 return this._injector;
1326 }
1327 // get overrides for a specific provider (if any)
1328 getSingleProviderOverrides(provider) {
1329 const token = getProviderToken(provider);
1330 return this.providerOverridesByToken.get(token) || null;
1331 }
1332 getProviderOverrides(providers) {
1333 if (!providers || !providers.length || this.providerOverridesByToken.size === 0)
1334 return [];
1335 // There are two flattening operations here. The inner flatten() operates on the metadata's
1336 // providers and applies a mapping function which retrieves overrides for each incoming
1337 // provider. The outer flatten() then flattens the produced overrides array. If this is not
1338 // done, the array can contain other empty arrays (e.g. `[[], []]`) which leak into the
1339 // providers array and contaminate any error messages that might be generated.
1340 return flatten(flatten(providers, (provider) => this.getSingleProviderOverrides(provider) || []));
1341 }
1342 getOverriddenProviders(providers) {
1343 if (!providers || !providers.length || this.providerOverridesByToken.size === 0)
1344 return [];
1345 const flattenedProviders = flatten(providers);
1346 const overrides = this.getProviderOverrides(flattenedProviders);
1347 const overriddenProviders = [...flattenedProviders, ...overrides];
1348 const final = [];
1349 const seenOverriddenProviders = new Set();
1350 // We iterate through the list of providers in reverse order to make sure provider overrides
1351 // take precedence over the values defined in provider list. We also filter out all providers
1352 // that have overrides, keeping overridden values only. This is needed, since presence of a
1353 // provider with `ngOnDestroy` hook will cause this hook to be registered and invoked later.
1354 forEachRight(overriddenProviders, (provider) => {
1355 const token = getProviderToken(provider);
1356 if (this.providerOverridesByToken.has(token)) {
1357 if (!seenOverriddenProviders.has(token)) {
1358 seenOverriddenProviders.add(token);
1359 // Treat all overridden providers as `{multi: false}` (even if it's a multi-provider) to
1360 // make sure that provided override takes highest precedence and is not combined with
1361 // other instances of the same multi provider.
1362 final.unshift(Object.assign(Object.assign({}, provider), { multi: false }));
1363 }
1364 }
1365 else {
1366 final.unshift(provider);
1367 }
1368 });
1369 return final;
1370 }
1371 hasProviderOverrides(providers) {
1372 return this.getProviderOverrides(providers).length > 0;
1373 }
1374 patchDefWithProviderOverrides(declaration, field) {
1375 const def = declaration[field];
1376 if (def && def.providersResolver) {
1377 this.maybeStoreNgDef(field, declaration);
1378 const resolver = def.providersResolver;
1379 const processProvidersFn = (providers) => this.getOverriddenProviders(providers);
1380 this.storeFieldOfDefOnType(declaration, field, 'providersResolver');
1381 def.providersResolver = (ngDef) => resolver(ngDef, processProvidersFn);
1382 }
1383 }
1384}
1385function initResolvers() {
1386 return {
1387 module: new NgModuleResolver(),
1388 component: new ComponentResolver(),
1389 directive: new DirectiveResolver(),
1390 pipe: new PipeResolver()
1391 };
1392}
1393function hasNgModuleDef(value) {
1394 return value.hasOwnProperty('ɵmod');
1395}
1396function maybeUnwrapFn(maybeFn) {
1397 return maybeFn instanceof Function ? maybeFn() : maybeFn;
1398}
1399function flatten(values, mapFn) {
1400 const out = [];
1401 values.forEach(value => {
1402 if (Array.isArray(value)) {
1403 out.push(...flatten(value, mapFn));
1404 }
1405 else {
1406 out.push(mapFn ? mapFn(value) : value);
1407 }
1408 });
1409 return out;
1410}
1411function getProviderField(provider, field) {
1412 return provider && typeof provider === 'object' && provider[field];
1413}
1414function getProviderToken(provider) {
1415 return getProviderField(provider, 'provide') || provider;
1416}
1417function isModuleWithProviders(value) {
1418 return value.hasOwnProperty('ngModule');
1419}
1420function forEachRight(values, fn) {
1421 for (let idx = values.length - 1; idx >= 0; idx--) {
1422 fn(values[idx], idx);
1423 }
1424}
1425function invalidTypeError(name, expectedType) {
1426 return new Error(`${name} class doesn't have @${expectedType} decorator or is missing metadata.`);
1427}
1428class R3TestCompiler {
1429 constructor(testBed) {
1430 this.testBed = testBed;
1431 }
1432 compileModuleSync(moduleType) {
1433 this.testBed._compileNgModuleSync(moduleType);
1434 return new ɵNgModuleFactory(moduleType);
1435 }
1436 compileModuleAsync(moduleType) {
1437 return __awaiter(this, void 0, void 0, function* () {
1438 yield this.testBed._compileNgModuleAsync(moduleType);
1439 return new ɵNgModuleFactory(moduleType);
1440 });
1441 }
1442 compileModuleAndAllComponentsSync(moduleType) {
1443 const ngModuleFactory = this.compileModuleSync(moduleType);
1444 const componentFactories = this.testBed._getComponentFactories(moduleType);
1445 return new ModuleWithComponentFactories(ngModuleFactory, componentFactories);
1446 }
1447 compileModuleAndAllComponentsAsync(moduleType) {
1448 return __awaiter(this, void 0, void 0, function* () {
1449 const ngModuleFactory = yield this.compileModuleAsync(moduleType);
1450 const componentFactories = this.testBed._getComponentFactories(moduleType);
1451 return new ModuleWithComponentFactories(ngModuleFactory, componentFactories);
1452 });
1453 }
1454 clearCache() { }
1455 clearCacheFor(type) { }
1456 getModuleId(moduleType) {
1457 const meta = this.testBed._getModuleResolver().resolve(moduleType);
1458 return meta && meta.id || undefined;
1459 }
1460}
1461
1462/**
1463 * @license
1464 * Copyright Google LLC All Rights Reserved.
1465 *
1466 * Use of this source code is governed by an MIT-style license that can be
1467 * found in the LICENSE file at https://angular.io/license
1468 */
1469/** Whether test modules should be torn down by default. */
1470const TEARDOWN_TESTING_MODULE_ON_DESTROY_DEFAULT = true;
1471/**
1472 * An abstract class for inserting the root test component element in a platform independent way.
1473 *
1474 * @publicApi
1475 */
1476class TestComponentRenderer {
1477 insertRootElement(rootElementId) { }
1478 removeAllRootElements() { }
1479}
1480/**
1481 * @publicApi
1482 */
1483const ComponentFixtureAutoDetect = new InjectionToken('ComponentFixtureAutoDetect');
1484/**
1485 * @publicApi
1486 */
1487const ComponentFixtureNoNgZone = new InjectionToken('ComponentFixtureNoNgZone');
1488
1489/**
1490 * @license
1491 * Copyright Google LLC All Rights Reserved.
1492 *
1493 * Use of this source code is governed by an MIT-style license that can be
1494 * found in the LICENSE file at https://angular.io/license
1495 */
1496let _nextRootElementId$1 = 0;
1497/**
1498 * @description
1499 * Configures and initializes environment for unit testing and provides methods for
1500 * creating components and services in unit tests.
1501 *
1502 * TestBed is the primary api for writing unit tests for Angular applications and libraries.
1503 *
1504 * Note: Use `TestBed` in tests. It will be set to either `TestBedViewEngine` or `TestBedRender3`
1505 * according to the compiler used.
1506 */
1507class TestBedRender3 {
1508 constructor() {
1509 // Properties
1510 this.platform = null;
1511 this.ngModule = null;
1512 this._compiler = null;
1513 this._testModuleRef = null;
1514 this._activeFixtures = [];
1515 this._globalCompilationChecked = false;
1516 }
1517 /**
1518 * Initialize the environment for testing with a compiler factory, a PlatformRef, and an
1519 * angular module. These are common to every test in the suite.
1520 *
1521 * This may only be called once, to set up the common providers for the current test
1522 * suite on the current platform. If you absolutely need to change the providers,
1523 * first use `resetTestEnvironment`.
1524 *
1525 * Test modules and platforms for individual platforms are available from
1526 * '@angular/<platform_name>/testing'.
1527 *
1528 * @publicApi
1529 */
1530 static initTestEnvironment(ngModule, platform, summariesOrOptions) {
1531 const testBed = _getTestBedRender3();
1532 testBed.initTestEnvironment(ngModule, platform, summariesOrOptions);
1533 return testBed;
1534 }
1535 /**
1536 * Reset the providers for the test injector.
1537 *
1538 * @publicApi
1539 */
1540 static resetTestEnvironment() {
1541 _getTestBedRender3().resetTestEnvironment();
1542 }
1543 static configureCompiler(config) {
1544 _getTestBedRender3().configureCompiler(config);
1545 return TestBedRender3;
1546 }
1547 /**
1548 * Allows overriding default providers, directives, pipes, modules of the test injector,
1549 * which are defined in test_injector.js
1550 */
1551 static configureTestingModule(moduleDef) {
1552 _getTestBedRender3().configureTestingModule(moduleDef);
1553 return TestBedRender3;
1554 }
1555 /**
1556 * Compile components with a `templateUrl` for the test's NgModule.
1557 * It is necessary to call this function
1558 * as fetching urls is asynchronous.
1559 */
1560 static compileComponents() {
1561 return _getTestBedRender3().compileComponents();
1562 }
1563 static overrideModule(ngModule, override) {
1564 _getTestBedRender3().overrideModule(ngModule, override);
1565 return TestBedRender3;
1566 }
1567 static overrideComponent(component, override) {
1568 _getTestBedRender3().overrideComponent(component, override);
1569 return TestBedRender3;
1570 }
1571 static overrideDirective(directive, override) {
1572 _getTestBedRender3().overrideDirective(directive, override);
1573 return TestBedRender3;
1574 }
1575 static overridePipe(pipe, override) {
1576 _getTestBedRender3().overridePipe(pipe, override);
1577 return TestBedRender3;
1578 }
1579 static overrideTemplate(component, template) {
1580 _getTestBedRender3().overrideComponent(component, { set: { template, templateUrl: null } });
1581 return TestBedRender3;
1582 }
1583 /**
1584 * Overrides the template of the given component, compiling the template
1585 * in the context of the TestingModule.
1586 *
1587 * Note: This works for JIT and AOTed components as well.
1588 */
1589 static overrideTemplateUsingTestingModule(component, template) {
1590 _getTestBedRender3().overrideTemplateUsingTestingModule(component, template);
1591 return TestBedRender3;
1592 }
1593 static overrideProvider(token, provider) {
1594 _getTestBedRender3().overrideProvider(token, provider);
1595 return TestBedRender3;
1596 }
1597 static inject(token, notFoundValue, flags) {
1598 return _getTestBedRender3().inject(token, notFoundValue, flags);
1599 }
1600 /** @deprecated from v9.0.0 use TestBed.inject */
1601 static get(token, notFoundValue = Injector.THROW_IF_NOT_FOUND, flags = InjectFlags.Default) {
1602 return _getTestBedRender3().inject(token, notFoundValue, flags);
1603 }
1604 static createComponent(component) {
1605 return _getTestBedRender3().createComponent(component);
1606 }
1607 static resetTestingModule() {
1608 _getTestBedRender3().resetTestingModule();
1609 return TestBedRender3;
1610 }
1611 static shouldTearDownTestingModule() {
1612 return _getTestBedRender3().shouldTearDownTestingModule();
1613 }
1614 static tearDownTestingModule() {
1615 _getTestBedRender3().tearDownTestingModule();
1616 }
1617 /**
1618 * Initialize the environment for testing with a compiler factory, a PlatformRef, and an
1619 * angular module. These are common to every test in the suite.
1620 *
1621 * This may only be called once, to set up the common providers for the current test
1622 * suite on the current platform. If you absolutely need to change the providers,
1623 * first use `resetTestEnvironment`.
1624 *
1625 * Test modules and platforms for individual platforms are available from
1626 * '@angular/<platform_name>/testing'.
1627 *
1628 * @publicApi
1629 */
1630 initTestEnvironment(ngModule, platform, summariesOrOptions) {
1631 if (this.platform || this.ngModule) {
1632 throw new Error('Cannot set base providers because it has already been called');
1633 }
1634 // If `summariesOrOptions` is a function, it means that it's
1635 // an AOT summaries factory which Ivy doesn't support.
1636 TestBedRender3._environmentTeardownOptions =
1637 typeof summariesOrOptions === 'function' ? undefined : summariesOrOptions === null || summariesOrOptions === void 0 ? void 0 : summariesOrOptions.teardown;
1638 this.platform = platform;
1639 this.ngModule = ngModule;
1640 this._compiler = new R3TestBedCompiler(this.platform, this.ngModule);
1641 }
1642 /**
1643 * Reset the providers for the test injector.
1644 *
1645 * @publicApi
1646 */
1647 resetTestEnvironment() {
1648 this.resetTestingModule();
1649 this._compiler = null;
1650 this.platform = null;
1651 this.ngModule = null;
1652 TestBedRender3._environmentTeardownOptions = undefined;
1653 }
1654 resetTestingModule() {
1655 this.checkGlobalCompilationFinished();
1656 ɵresetCompiledComponents();
1657 if (this._compiler !== null) {
1658 this.compiler.restoreOriginalState();
1659 }
1660 this._compiler = new R3TestBedCompiler(this.platform, this.ngModule);
1661 // We have to chain a couple of try/finally blocks, because each step can
1662 // throw errors and we don't want it to interrupt the next step and we also
1663 // want an error to be thrown at the end.
1664 try {
1665 this.destroyActiveFixtures();
1666 }
1667 finally {
1668 try {
1669 if (this.shouldTearDownTestingModule()) {
1670 this.tearDownTestingModule();
1671 }
1672 }
1673 finally {
1674 this._testModuleRef = null;
1675 this._instanceTeardownOptions = undefined;
1676 }
1677 }
1678 }
1679 configureCompiler(config) {
1680 if (config.useJit != null) {
1681 throw new Error('the Render3 compiler JiT mode is not configurable !');
1682 }
1683 if (config.providers !== undefined) {
1684 this.compiler.setCompilerProviders(config.providers);
1685 }
1686 }
1687 configureTestingModule(moduleDef) {
1688 this.assertNotInstantiated('R3TestBed.configureTestingModule', 'configure the test module');
1689 // Always re-assign the teardown options, even if they're undefined.
1690 // This ensures that we don't carry the options between tests.
1691 this._instanceTeardownOptions = moduleDef.teardown;
1692 this.compiler.configureTestingModule(moduleDef);
1693 }
1694 compileComponents() {
1695 return this.compiler.compileComponents();
1696 }
1697 inject(token, notFoundValue, flags) {
1698 if (token === TestBedRender3) {
1699 return this;
1700 }
1701 const UNDEFINED = {};
1702 const result = this.testModuleRef.injector.get(token, UNDEFINED, flags);
1703 return result === UNDEFINED ? this.compiler.injector.get(token, notFoundValue, flags) :
1704 result;
1705 }
1706 /** @deprecated from v9.0.0 use TestBed.inject */
1707 get(token, notFoundValue = Injector.THROW_IF_NOT_FOUND, flags = InjectFlags.Default) {
1708 return this.inject(token, notFoundValue, flags);
1709 }
1710 execute(tokens, fn, context) {
1711 const params = tokens.map(t => this.inject(t));
1712 return fn.apply(context, params);
1713 }
1714 overrideModule(ngModule, override) {
1715 this.assertNotInstantiated('overrideModule', 'override module metadata');
1716 this.compiler.overrideModule(ngModule, override);
1717 }
1718 overrideComponent(component, override) {
1719 this.assertNotInstantiated('overrideComponent', 'override component metadata');
1720 this.compiler.overrideComponent(component, override);
1721 }
1722 overrideTemplateUsingTestingModule(component, template) {
1723 this.assertNotInstantiated('R3TestBed.overrideTemplateUsingTestingModule', 'Cannot override template when the test module has already been instantiated');
1724 this.compiler.overrideTemplateUsingTestingModule(component, template);
1725 }
1726 overrideDirective(directive, override) {
1727 this.assertNotInstantiated('overrideDirective', 'override directive metadata');
1728 this.compiler.overrideDirective(directive, override);
1729 }
1730 overridePipe(pipe, override) {
1731 this.assertNotInstantiated('overridePipe', 'override pipe metadata');
1732 this.compiler.overridePipe(pipe, override);
1733 }
1734 /**
1735 * Overwrites all providers for the given token with the given provider definition.
1736 */
1737 overrideProvider(token, provider) {
1738 this.assertNotInstantiated('overrideProvider', 'override provider');
1739 this.compiler.overrideProvider(token, provider);
1740 }
1741 createComponent(type) {
1742 const testComponentRenderer = this.inject(TestComponentRenderer);
1743 const rootElId = `root${_nextRootElementId$1++}`;
1744 testComponentRenderer.insertRootElement(rootElId);
1745 const componentDef = type.ɵcmp;
1746 if (!componentDef) {
1747 throw new Error(`It looks like '${ɵstringify(type)}' has not been IVY compiled - it has no 'ɵcmp' field`);
1748 }
1749 // TODO: Don't cast as `InjectionToken<boolean>`, proper type is boolean[]
1750 const noNgZone = this.inject(ComponentFixtureNoNgZone, false);
1751 // TODO: Don't cast as `InjectionToken<boolean>`, proper type is boolean[]
1752 const autoDetect = this.inject(ComponentFixtureAutoDetect, false);
1753 const ngZone = noNgZone ? null : this.inject(NgZone, null);
1754 const componentFactory = new ɵRender3ComponentFactory(componentDef);
1755 const initComponent = () => {
1756 const componentRef = componentFactory.create(Injector.NULL, [], `#${rootElId}`, this.testModuleRef);
1757 return new ComponentFixture(componentRef, ngZone, autoDetect);
1758 };
1759 const fixture = ngZone ? ngZone.run(initComponent) : initComponent();
1760 this._activeFixtures.push(fixture);
1761 return fixture;
1762 }
1763 /**
1764 * @internal strip this from published d.ts files due to
1765 * https://github.com/microsoft/TypeScript/issues/36216
1766 */
1767 get compiler() {
1768 if (this._compiler === null) {
1769 throw new Error(`Need to call TestBed.initTestEnvironment() first`);
1770 }
1771 return this._compiler;
1772 }
1773 /**
1774 * @internal strip this from published d.ts files due to
1775 * https://github.com/microsoft/TypeScript/issues/36216
1776 */
1777 get testModuleRef() {
1778 if (this._testModuleRef === null) {
1779 this._testModuleRef = this.compiler.finalize();
1780 }
1781 return this._testModuleRef;
1782 }
1783 assertNotInstantiated(methodName, methodDescription) {
1784 if (this._testModuleRef !== null) {
1785 throw new Error(`Cannot ${methodDescription} when the test module has already been instantiated. ` +
1786 `Make sure you are not using \`inject\` before \`${methodName}\`.`);
1787 }
1788 }
1789 /**
1790 * Check whether the module scoping queue should be flushed, and flush it if needed.
1791 *
1792 * When the TestBed is reset, it clears the JIT module compilation queue, cancelling any
1793 * in-progress module compilation. This creates a potential hazard - the very first time the
1794 * TestBed is initialized (or if it's reset without being initialized), there may be pending
1795 * compilations of modules declared in global scope. These compilations should be finished.
1796 *
1797 * To ensure that globally declared modules have their components scoped properly, this function
1798 * is called whenever TestBed is initialized or reset. The _first_ time that this happens, prior
1799 * to any other operations, the scoping queue is flushed.
1800 */
1801 checkGlobalCompilationFinished() {
1802 // Checking _testNgModuleRef is null should not be necessary, but is left in as an additional
1803 // guard that compilations queued in tests (after instantiation) are never flushed accidentally.
1804 if (!this._globalCompilationChecked && this._testModuleRef === null) {
1805 ɵflushModuleScopingQueueAsMuchAsPossible();
1806 }
1807 this._globalCompilationChecked = true;
1808 }
1809 destroyActiveFixtures() {
1810 let errorCount = 0;
1811 this._activeFixtures.forEach((fixture) => {
1812 try {
1813 fixture.destroy();
1814 }
1815 catch (e) {
1816 errorCount++;
1817 console.error('Error during cleanup of component', {
1818 component: fixture.componentInstance,
1819 stacktrace: e,
1820 });
1821 }
1822 });
1823 this._activeFixtures = [];
1824 if (errorCount > 0 && this.shouldRethrowTeardownErrors()) {
1825 throw Error(`${errorCount} ${(errorCount === 1 ? 'component' : 'components')} ` +
1826 `threw errors during cleanup`);
1827 }
1828 }
1829 shouldRethrowTeardownErrors() {
1830 var _a, _b;
1831 const instanceOptions = this._instanceTeardownOptions;
1832 const environmentOptions = TestBedRender3._environmentTeardownOptions;
1833 // If the new teardown behavior hasn't been configured, preserve the old behavior.
1834 if (!instanceOptions && !environmentOptions) {
1835 return TEARDOWN_TESTING_MODULE_ON_DESTROY_DEFAULT;
1836 }
1837 // Otherwise use the configured behavior or default to rethrowing.
1838 return (_b = (_a = instanceOptions === null || instanceOptions === void 0 ? void 0 : instanceOptions.rethrowErrors) !== null && _a !== void 0 ? _a : environmentOptions === null || environmentOptions === void 0 ? void 0 : environmentOptions.rethrowErrors) !== null && _b !== void 0 ? _b : this.shouldTearDownTestingModule();
1839 }
1840 shouldTearDownTestingModule() {
1841 var _a, _b, _c, _d;
1842 return (_d = (_b = (_a = this._instanceTeardownOptions) === null || _a === void 0 ? void 0 : _a.destroyAfterEach) !== null && _b !== void 0 ? _b : (_c = TestBedRender3._environmentTeardownOptions) === null || _c === void 0 ? void 0 : _c.destroyAfterEach) !== null && _d !== void 0 ? _d : TEARDOWN_TESTING_MODULE_ON_DESTROY_DEFAULT;
1843 }
1844 tearDownTestingModule() {
1845 var _a;
1846 // If the module ref has already been destroyed, we won't be able to get a test renderer.
1847 if (this._testModuleRef === null) {
1848 return;
1849 }
1850 // Resolve the renderer ahead of time, because we want to remove the root elements as the very
1851 // last step, but the injector will be destroyed as a part of the module ref destruction.
1852 const testRenderer = this.inject(TestComponentRenderer);
1853 try {
1854 this._testModuleRef.destroy();
1855 }
1856 catch (e) {
1857 if (this.shouldRethrowTeardownErrors()) {
1858 throw e;
1859 }
1860 else {
1861 console.error('Error during cleanup of a testing module', {
1862 component: this._testModuleRef.instance,
1863 stacktrace: e,
1864 });
1865 }
1866 }
1867 finally {
1868 (_a = testRenderer.removeAllRootElements) === null || _a === void 0 ? void 0 : _a.call(testRenderer);
1869 }
1870 }
1871}
1872let testBed$1;
1873function _getTestBedRender3() {
1874 return testBed$1 = testBed$1 || new TestBedRender3();
1875}
1876
1877/**
1878 * @license
1879 * Copyright Google LLC All Rights Reserved.
1880 *
1881 * Use of this source code is governed by an MIT-style license that can be
1882 * found in the LICENSE file at https://angular.io/license
1883 */
1884function unimplemented() {
1885 throw Error('unimplemented');
1886}
1887/**
1888 * Special interface to the compiler only used by testing
1889 *
1890 * @publicApi
1891 */
1892class TestingCompiler extends Compiler {
1893 get injector() {
1894 throw unimplemented();
1895 }
1896 overrideModule(module, overrides) {
1897 throw unimplemented();
1898 }
1899 overrideDirective(directive, overrides) {
1900 throw unimplemented();
1901 }
1902 overrideComponent(component, overrides) {
1903 throw unimplemented();
1904 }
1905 overridePipe(directive, overrides) {
1906 throw unimplemented();
1907 }
1908 /**
1909 * Allows to pass the compile summary from AOT compilation to the JIT compiler,
1910 * so that it can use the code generated by AOT.
1911 */
1912 loadAotSummaries(summaries) {
1913 throw unimplemented();
1914 }
1915 /**
1916 * Gets the component factory for the given component.
1917 * This assumes that the component has been compiled before calling this call using
1918 * `compileModuleAndAllComponents*`.
1919 */
1920 getComponentFactory(component) {
1921 throw unimplemented();
1922 }
1923 /**
1924 * Returns the component type that is stored in the given error.
1925 * This can be used for errors created by compileModule...
1926 */
1927 getComponentFromError(error) {
1928 throw unimplemented();
1929 }
1930}
1931TestingCompiler.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.0.2", ngImport: i0, type: TestingCompiler, deps: null, target: i0.ɵɵFactoryTarget.Injectable });
1932TestingCompiler.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "13.0.2", ngImport: i0, type: TestingCompiler });
1933i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.0.2", ngImport: i0, type: TestingCompiler, decorators: [{
1934 type: Injectable
1935 }] });
1936/**
1937 * A factory for creating a Compiler
1938 *
1939 * @publicApi
1940 */
1941class TestingCompilerFactory {
1942}
1943
1944/**
1945 * @license
1946 * Copyright Google LLC All Rights Reserved.
1947 *
1948 * Use of this source code is governed by an MIT-style license that can be
1949 * found in the LICENSE file at https://angular.io/license
1950 */
1951let _nextRootElementId = 0;
1952/**
1953 * @description
1954 * Configures and initializes environment for unit testing and provides methods for
1955 * creating components and services in unit tests.
1956 *
1957 * `TestBed` is the primary api for writing unit tests for Angular applications and libraries.
1958 *
1959 * Note: Use `TestBed` in tests. It will be set to either `TestBedViewEngine` or `TestBedRender3`
1960 * according to the compiler used.
1961 */
1962class TestBedViewEngine {
1963 constructor() {
1964 this._instantiated = false;
1965 this._compiler = null;
1966 this._moduleRef = null;
1967 this._moduleFactory = null;
1968 this._pendingModuleFactory = null;
1969 this._compilerOptions = [];
1970 this._moduleOverrides = [];
1971 this._componentOverrides = [];
1972 this._directiveOverrides = [];
1973 this._pipeOverrides = [];
1974 this._providers = [];
1975 this._declarations = [];
1976 this._imports = [];
1977 this._schemas = [];
1978 this._activeFixtures = [];
1979 this._testEnvAotSummaries = () => [];
1980 this._aotSummaries = [];
1981 this._templateOverrides = [];
1982 this._isRoot = true;
1983 this._rootProviderOverrides = [];
1984 this.platform = null;
1985 this.ngModule = null;
1986 }
1987 /**
1988 * Initialize the environment for testing with a compiler factory, a PlatformRef, and an
1989 * angular module. These are common to every test in the suite.
1990 *
1991 * This may only be called once, to set up the common providers for the current test
1992 * suite on the current platform. If you absolutely need to change the providers,
1993 * first use `resetTestEnvironment`.
1994 *
1995 * Test modules and platforms for individual platforms are available from
1996 * '@angular/<platform_name>/testing'.
1997 */
1998 static initTestEnvironment(ngModule, platform, summariesOrOptions) {
1999 const testBed = _getTestBedViewEngine();
2000 testBed.initTestEnvironment(ngModule, platform, summariesOrOptions);
2001 return testBed;
2002 }
2003 /**
2004 * Reset the providers for the test injector.
2005 */
2006 static resetTestEnvironment() {
2007 _getTestBedViewEngine().resetTestEnvironment();
2008 }
2009 static resetTestingModule() {
2010 _getTestBedViewEngine().resetTestingModule();
2011 return TestBedViewEngine;
2012 }
2013 /**
2014 * Allows overriding default compiler providers and settings
2015 * which are defined in test_injector.js
2016 */
2017 static configureCompiler(config) {
2018 _getTestBedViewEngine().configureCompiler(config);
2019 return TestBedViewEngine;
2020 }
2021 /**
2022 * Allows overriding default providers, directives, pipes, modules of the test injector,
2023 * which are defined in test_injector.js
2024 */
2025 static configureTestingModule(moduleDef) {
2026 _getTestBedViewEngine().configureTestingModule(moduleDef);
2027 return TestBedViewEngine;
2028 }
2029 /**
2030 * Compile components with a `templateUrl` for the test's NgModule.
2031 * It is necessary to call this function
2032 * as fetching urls is asynchronous.
2033 */
2034 static compileComponents() {
2035 return getTestBed().compileComponents();
2036 }
2037 static overrideModule(ngModule, override) {
2038 _getTestBedViewEngine().overrideModule(ngModule, override);
2039 return TestBedViewEngine;
2040 }
2041 static overrideComponent(component, override) {
2042 _getTestBedViewEngine().overrideComponent(component, override);
2043 return TestBedViewEngine;
2044 }
2045 static overrideDirective(directive, override) {
2046 _getTestBedViewEngine().overrideDirective(directive, override);
2047 return TestBedViewEngine;
2048 }
2049 static overridePipe(pipe, override) {
2050 _getTestBedViewEngine().overridePipe(pipe, override);
2051 return TestBedViewEngine;
2052 }
2053 static overrideTemplate(component, template) {
2054 _getTestBedViewEngine().overrideComponent(component, { set: { template, templateUrl: null } });
2055 return TestBedViewEngine;
2056 }
2057 /**
2058 * Overrides the template of the given component, compiling the template
2059 * in the context of the TestingModule.
2060 *
2061 * Note: This works for JIT and AOTed components as well.
2062 */
2063 static overrideTemplateUsingTestingModule(component, template) {
2064 _getTestBedViewEngine().overrideTemplateUsingTestingModule(component, template);
2065 return TestBedViewEngine;
2066 }
2067 static overrideProvider(token, provider) {
2068 _getTestBedViewEngine().overrideProvider(token, provider);
2069 return TestBedViewEngine;
2070 }
2071 static inject(token, notFoundValue, flags) {
2072 return _getTestBedViewEngine().inject(token, notFoundValue, flags);
2073 }
2074 /** @deprecated from v9.0.0 use TestBed.inject */
2075 static get(token, notFoundValue = Injector.THROW_IF_NOT_FOUND, flags = InjectFlags.Default) {
2076 return _getTestBedViewEngine().inject(token, notFoundValue, flags);
2077 }
2078 static createComponent(component) {
2079 return _getTestBedViewEngine().createComponent(component);
2080 }
2081 static shouldTearDownTestingModule() {
2082 return _getTestBedViewEngine().shouldTearDownTestingModule();
2083 }
2084 static tearDownTestingModule() {
2085 _getTestBedViewEngine().tearDownTestingModule();
2086 }
2087 /**
2088 * Initialize the environment for testing with a compiler factory, a PlatformRef, and an
2089 * angular module. These are common to every test in the suite.
2090 *
2091 * This may only be called once, to set up the common providers for the current test
2092 * suite on the current platform. If you absolutely need to change the providers,
2093 * first use `resetTestEnvironment`.
2094 *
2095 * Test modules and platforms for individual platforms are available from
2096 * '@angular/<platform_name>/testing'.
2097 */
2098 initTestEnvironment(ngModule, platform, summariesOrOptions) {
2099 if (this.platform || this.ngModule) {
2100 throw new Error('Cannot set base providers because it has already been called');
2101 }
2102 this.platform = platform;
2103 this.ngModule = ngModule;
2104 if (typeof summariesOrOptions === 'function') {
2105 this._testEnvAotSummaries = summariesOrOptions;
2106 TestBedViewEngine._environmentTeardownOptions = undefined;
2107 }
2108 else {
2109 this._testEnvAotSummaries = (summariesOrOptions === null || summariesOrOptions === void 0 ? void 0 : summariesOrOptions.aotSummaries) || (() => []);
2110 TestBedViewEngine._environmentTeardownOptions = summariesOrOptions === null || summariesOrOptions === void 0 ? void 0 : summariesOrOptions.teardown;
2111 }
2112 }
2113 /**
2114 * Reset the providers for the test injector.
2115 */
2116 resetTestEnvironment() {
2117 this.resetTestingModule();
2118 this.platform = null;
2119 this.ngModule = null;
2120 this._testEnvAotSummaries = () => [];
2121 TestBedViewEngine._environmentTeardownOptions = undefined;
2122 }
2123 resetTestingModule() {
2124 ɵclearOverrides();
2125 this._aotSummaries = [];
2126 this._templateOverrides = [];
2127 this._compiler = null;
2128 this._moduleOverrides = [];
2129 this._componentOverrides = [];
2130 this._directiveOverrides = [];
2131 this._pipeOverrides = [];
2132 this._isRoot = true;
2133 this._rootProviderOverrides = [];
2134 this._moduleFactory = null;
2135 this._pendingModuleFactory = null;
2136 this._compilerOptions = [];
2137 this._providers = [];
2138 this._declarations = [];
2139 this._imports = [];
2140 this._schemas = [];
2141 // We have to chain a couple of try/finally blocks, because each step can
2142 // throw errors and we don't want it to interrupt the next step and we also
2143 // want an error to be thrown at the end.
2144 try {
2145 this.destroyActiveFixtures();
2146 }
2147 finally {
2148 try {
2149 if (this.shouldTearDownTestingModule()) {
2150 this.tearDownTestingModule();
2151 }
2152 }
2153 finally {
2154 this._moduleRef = null;
2155 this._instanceTeardownOptions = undefined;
2156 this._instantiated = false;
2157 }
2158 }
2159 }
2160 configureCompiler(config) {
2161 this._assertNotInstantiated('TestBed.configureCompiler', 'configure the compiler');
2162 this._compilerOptions.push(config);
2163 }
2164 configureTestingModule(moduleDef) {
2165 this._assertNotInstantiated('TestBed.configureTestingModule', 'configure the test module');
2166 if (moduleDef.providers) {
2167 this._providers.push(...moduleDef.providers);
2168 }
2169 if (moduleDef.declarations) {
2170 this._declarations.push(...moduleDef.declarations);
2171 }
2172 if (moduleDef.imports) {
2173 this._imports.push(...moduleDef.imports);
2174 }
2175 if (moduleDef.schemas) {
2176 this._schemas.push(...moduleDef.schemas);
2177 }
2178 if (moduleDef.aotSummaries) {
2179 this._aotSummaries.push(moduleDef.aotSummaries);
2180 }
2181 // Always re-assign the teardown options, even if they're undefined.
2182 // This ensures that we don't carry the options between tests.
2183 this._instanceTeardownOptions = moduleDef.teardown;
2184 }
2185 compileComponents() {
2186 if (this._moduleFactory || this._instantiated) {
2187 return Promise.resolve(null);
2188 }
2189 const moduleType = this._createCompilerAndModule();
2190 this._pendingModuleFactory = moduleType;
2191 return this._compiler.compileModuleAndAllComponentsAsync(moduleType).then(result => {
2192 // If the module mismatches by the time the promise resolves, it means that the module has
2193 // already been destroyed and a new compilation has started. If that's the case, avoid
2194 // overwriting the module factory, because it can cause downstream errors.
2195 if (this._pendingModuleFactory === moduleType) {
2196 this._moduleFactory = result.ngModuleFactory;
2197 this._pendingModuleFactory = null;
2198 }
2199 });
2200 }
2201 _initIfNeeded() {
2202 if (this._instantiated) {
2203 return;
2204 }
2205 if (!this._moduleFactory) {
2206 try {
2207 const moduleType = this._createCompilerAndModule();
2208 this._moduleFactory =
2209 this._compiler.compileModuleAndAllComponentsSync(moduleType).ngModuleFactory;
2210 }
2211 catch (e) {
2212 const errorCompType = this._compiler.getComponentFromError(e);
2213 if (errorCompType) {
2214 throw new Error(`This test module uses the component ${ɵstringify(errorCompType)} which is using a "templateUrl" or "styleUrls", but they were never compiled. ` +
2215 `Please call "TestBed.compileComponents" before your test.`);
2216 }
2217 else {
2218 throw e;
2219 }
2220 }
2221 }
2222 for (const { component, templateOf } of this._templateOverrides) {
2223 const compFactory = this._compiler.getComponentFactory(templateOf);
2224 ɵoverrideComponentView(component, compFactory);
2225 }
2226 const ngZone = new NgZone({ enableLongStackTrace: true, shouldCoalesceEventChangeDetection: false });
2227 const providers = [{ provide: NgZone, useValue: ngZone }];
2228 const ngZoneInjector = Injector.create({
2229 providers: providers,
2230 parent: this.platform.injector,
2231 name: this._moduleFactory.moduleType.name
2232 });
2233 this._moduleRef = this._moduleFactory.create(ngZoneInjector);
2234 // ApplicationInitStatus.runInitializers() is marked @internal to core. So casting to any
2235 // before accessing it.
2236 try {
2237 this._moduleRef.injector.get(ApplicationInitStatus).runInitializers();
2238 }
2239 finally {
2240 this._instantiated = true;
2241 }
2242 }
2243 _createCompilerAndModule() {
2244 const providers = this._providers.concat([{ provide: TestBed, useValue: this }]);
2245 const declarations = [...this._declarations, ...this._templateOverrides.map(entry => entry.templateOf)];
2246 const rootScopeImports = [];
2247 const rootProviderOverrides = this._rootProviderOverrides;
2248 if (this._isRoot) {
2249 let RootScopeModule = class RootScopeModule {
2250 };
2251 RootScopeModule = __decorate([
2252 NgModule({
2253 providers: [
2254 ...rootProviderOverrides,
2255 ],
2256 jit: true,
2257 })
2258 ], RootScopeModule);
2259 rootScopeImports.push(RootScopeModule);
2260 }
2261 providers.push({ provide: ɵINJECTOR_SCOPE, useValue: this._isRoot ? 'root' : null });
2262 const imports = [rootScopeImports, this.ngModule, this._imports];
2263 const schemas = this._schemas;
2264 let DynamicTestModule = class DynamicTestModule {
2265 };
2266 DynamicTestModule = __decorate([
2267 NgModule({ providers, declarations, imports, schemas, jit: true })
2268 ], DynamicTestModule);
2269 const compilerFactory = this.platform.injector.get(TestingCompilerFactory);
2270 this._compiler = compilerFactory.createTestingCompiler(this._compilerOptions);
2271 for (const summary of [this._testEnvAotSummaries, ...this._aotSummaries]) {
2272 this._compiler.loadAotSummaries(summary);
2273 }
2274 this._moduleOverrides.forEach((entry) => this._compiler.overrideModule(entry[0], entry[1]));
2275 this._componentOverrides.forEach((entry) => this._compiler.overrideComponent(entry[0], entry[1]));
2276 this._directiveOverrides.forEach((entry) => this._compiler.overrideDirective(entry[0], entry[1]));
2277 this._pipeOverrides.forEach((entry) => this._compiler.overridePipe(entry[0], entry[1]));
2278 return DynamicTestModule;
2279 }
2280 _assertNotInstantiated(methodName, methodDescription) {
2281 if (this._instantiated) {
2282 throw new Error(`Cannot ${methodDescription} when the test module has already been instantiated. ` +
2283 `Make sure you are not using \`inject\` before \`${methodName}\`.`);
2284 }
2285 }
2286 inject(token, notFoundValue, flags) {
2287 this._initIfNeeded();
2288 if (token === TestBed) {
2289 return this;
2290 }
2291 // Tests can inject things from the ng module and from the compiler,
2292 // but the ng module can't inject things from the compiler and vice versa.
2293 const UNDEFINED = {};
2294 const result = this._moduleRef.injector.get(token, UNDEFINED, flags);
2295 return result === UNDEFINED ? this._compiler.injector.get(token, notFoundValue, flags) :
2296 result;
2297 }
2298 /** @deprecated from v9.0.0 use TestBed.inject */
2299 get(token, notFoundValue = Injector.THROW_IF_NOT_FOUND, flags = InjectFlags.Default) {
2300 return this.inject(token, notFoundValue, flags);
2301 }
2302 execute(tokens, fn, context) {
2303 this._initIfNeeded();
2304 const params = tokens.map(t => this.inject(t));
2305 return fn.apply(context, params);
2306 }
2307 overrideModule(ngModule, override) {
2308 this._assertNotInstantiated('overrideModule', 'override module metadata');
2309 this._moduleOverrides.push([ngModule, override]);
2310 }
2311 overrideComponent(component, override) {
2312 this._assertNotInstantiated('overrideComponent', 'override component metadata');
2313 this._componentOverrides.push([component, override]);
2314 }
2315 overrideDirective(directive, override) {
2316 this._assertNotInstantiated('overrideDirective', 'override directive metadata');
2317 this._directiveOverrides.push([directive, override]);
2318 }
2319 overridePipe(pipe, override) {
2320 this._assertNotInstantiated('overridePipe', 'override pipe metadata');
2321 this._pipeOverrides.push([pipe, override]);
2322 }
2323 overrideProvider(token, provider) {
2324 this._assertNotInstantiated('overrideProvider', 'override provider');
2325 this.overrideProviderImpl(token, provider);
2326 }
2327 overrideProviderImpl(token, provider, deprecated = false) {
2328 let def = null;
2329 if (typeof token !== 'string' && (def = ɵgetInjectableDef(token)) && def.providedIn === 'root') {
2330 if (provider.useFactory) {
2331 this._rootProviderOverrides.push({ provide: token, useFactory: provider.useFactory, deps: provider.deps || [] });
2332 }
2333 else {
2334 this._rootProviderOverrides.push({ provide: token, useValue: provider.useValue });
2335 }
2336 }
2337 let flags = 0;
2338 let value;
2339 if (provider.useFactory) {
2340 flags |= 1024 /* TypeFactoryProvider */;
2341 value = provider.useFactory;
2342 }
2343 else {
2344 flags |= 256 /* TypeValueProvider */;
2345 value = provider.useValue;
2346 }
2347 const deps = (provider.deps || []).map((dep) => {
2348 let depFlags = 0 /* None */;
2349 let depToken;
2350 if (Array.isArray(dep)) {
2351 dep.forEach((entry) => {
2352 if (entry instanceof Optional) {
2353 depFlags |= 2 /* Optional */;
2354 }
2355 else if (entry instanceof SkipSelf) {
2356 depFlags |= 1 /* SkipSelf */;
2357 }
2358 else {
2359 depToken = entry;
2360 }
2361 });
2362 }
2363 else {
2364 depToken = dep;
2365 }
2366 return [depFlags, depToken];
2367 });
2368 ɵoverrideProvider({ token, flags, deps, value, deprecatedBehavior: deprecated });
2369 }
2370 overrideTemplateUsingTestingModule(component, template) {
2371 this._assertNotInstantiated('overrideTemplateUsingTestingModule', 'override template');
2372 let OverrideComponent = class OverrideComponent {
2373 };
2374 OverrideComponent = __decorate([
2375 Component({ selector: 'empty', template, jit: true })
2376 ], OverrideComponent);
2377 this._templateOverrides.push({ component, templateOf: OverrideComponent });
2378 }
2379 createComponent(component) {
2380 this._initIfNeeded();
2381 const componentFactory = this._compiler.getComponentFactory(component);
2382 if (!componentFactory) {
2383 throw new Error(`Cannot create the component ${ɵstringify(component)} as it was not imported into the testing module!`);
2384 }
2385 // TODO: Don't cast as `InjectionToken<boolean>`, declared type is boolean[]
2386 const noNgZone = this.inject(ComponentFixtureNoNgZone, false);
2387 // TODO: Don't cast as `InjectionToken<boolean>`, declared type is boolean[]
2388 const autoDetect = this.inject(ComponentFixtureAutoDetect, false);
2389 const ngZone = noNgZone ? null : this.inject(NgZone, null);
2390 const testComponentRenderer = this.inject(TestComponentRenderer);
2391 const rootElId = `root${_nextRootElementId++}`;
2392 testComponentRenderer.insertRootElement(rootElId);
2393 const initComponent = () => {
2394 const componentRef = componentFactory.create(Injector.NULL, [], `#${rootElId}`, this._moduleRef);
2395 return new ComponentFixture(componentRef, ngZone, autoDetect);
2396 };
2397 const fixture = !ngZone ? initComponent() : ngZone.run(initComponent);
2398 this._activeFixtures.push(fixture);
2399 return fixture;
2400 }
2401 destroyActiveFixtures() {
2402 let errorCount = 0;
2403 this._activeFixtures.forEach((fixture) => {
2404 try {
2405 fixture.destroy();
2406 }
2407 catch (e) {
2408 errorCount++;
2409 console.error('Error during cleanup of component', {
2410 component: fixture.componentInstance,
2411 stacktrace: e,
2412 });
2413 }
2414 });
2415 this._activeFixtures = [];
2416 if (errorCount > 0 && this.shouldRethrowTeardownErrors()) {
2417 throw Error(`${errorCount} ${(errorCount === 1 ? 'component' : 'components')} ` +
2418 `threw errors during cleanup`);
2419 }
2420 }
2421 shouldRethrowTeardownErrors() {
2422 var _a, _b;
2423 const instanceOptions = this._instanceTeardownOptions;
2424 const environmentOptions = TestBedViewEngine._environmentTeardownOptions;
2425 // If the new teardown behavior hasn't been configured, preserve the old behavior.
2426 if (!instanceOptions && !environmentOptions) {
2427 return TEARDOWN_TESTING_MODULE_ON_DESTROY_DEFAULT;
2428 }
2429 // Otherwise use the configured behavior or default to rethrowing.
2430 return (_b = (_a = instanceOptions === null || instanceOptions === void 0 ? void 0 : instanceOptions.rethrowErrors) !== null && _a !== void 0 ? _a : environmentOptions === null || environmentOptions === void 0 ? void 0 : environmentOptions.rethrowErrors) !== null && _b !== void 0 ? _b : this.shouldTearDownTestingModule();
2431 }
2432 shouldTearDownTestingModule() {
2433 var _a, _b, _c, _d;
2434 return (_d = (_b = (_a = this._instanceTeardownOptions) === null || _a === void 0 ? void 0 : _a.destroyAfterEach) !== null && _b !== void 0 ? _b : (_c = TestBedViewEngine._environmentTeardownOptions) === null || _c === void 0 ? void 0 : _c.destroyAfterEach) !== null && _d !== void 0 ? _d : TEARDOWN_TESTING_MODULE_ON_DESTROY_DEFAULT;
2435 }
2436 tearDownTestingModule() {
2437 var _a, _b, _c, _d, _e;
2438 // If the module ref has already been destroyed, we won't be able to get a test renderer.
2439 if (this._moduleRef === null) {
2440 return;
2441 }
2442 // Resolve the renderer ahead of time, because we want to remove the root elements as the very
2443 // last step, but the injector will be destroyed as a part of the module ref destruction.
2444 const testRenderer = this.inject(TestComponentRenderer);
2445 try {
2446 this._moduleRef.destroy();
2447 }
2448 catch (e) {
2449 if ((_d = (_b = (_a = this._instanceTeardownOptions) === null || _a === void 0 ? void 0 : _a.rethrowErrors) !== null && _b !== void 0 ? _b : (_c = TestBedViewEngine._environmentTeardownOptions) === null || _c === void 0 ? void 0 : _c.rethrowErrors) !== null && _d !== void 0 ? _d : true) {
2450 throw e;
2451 }
2452 else {
2453 console.error('Error during cleanup of a testing module', {
2454 component: this._moduleRef.instance,
2455 stacktrace: e,
2456 });
2457 }
2458 }
2459 finally {
2460 (_e = testRenderer === null || testRenderer === void 0 ? void 0 : testRenderer.removeAllRootElements) === null || _e === void 0 ? void 0 : _e.call(testRenderer);
2461 }
2462 }
2463}
2464/**
2465 * @description
2466 * Configures and initializes environment for unit testing and provides methods for
2467 * creating components and services in unit tests.
2468 *
2469 * `TestBed` is the primary api for writing unit tests for Angular applications and libraries.
2470 *
2471 * Note: Use `TestBed` in tests. It will be set to either `TestBedViewEngine` or `TestBedRender3`
2472 * according to the compiler used.
2473 *
2474 * @publicApi
2475 */
2476const TestBed = ɵivyEnabled ? TestBedRender3 : TestBedViewEngine;
2477/**
2478 * Returns a singleton of the applicable `TestBed`.
2479 *
2480 * It will be either an instance of `TestBedViewEngine` or `TestBedRender3`.
2481 *
2482 * @publicApi
2483 */
2484const getTestBed = ɵivyEnabled ? _getTestBedRender3 : _getTestBedViewEngine;
2485let testBed;
2486function _getTestBedViewEngine() {
2487 return testBed = testBed || new TestBedViewEngine();
2488}
2489/**
2490 * Allows injecting dependencies in `beforeEach()` and `it()`.
2491 *
2492 * Example:
2493 *
2494 * ```
2495 * beforeEach(inject([Dependency, AClass], (dep, object) => {
2496 * // some code that uses `dep` and `object`
2497 * // ...
2498 * }));
2499 *
2500 * it('...', inject([AClass], (object) => {
2501 * object.doSomething();
2502 * expect(...);
2503 * })
2504 * ```
2505 *
2506 * @publicApi
2507 */
2508function inject(tokens, fn) {
2509 const testBed = getTestBed();
2510 // Not using an arrow function to preserve context passed from call site
2511 return function () {
2512 return testBed.execute(tokens, fn, this);
2513 };
2514}
2515/**
2516 * @publicApi
2517 */
2518class InjectSetupWrapper {
2519 constructor(_moduleDef) {
2520 this._moduleDef = _moduleDef;
2521 }
2522 _addModule() {
2523 const moduleDef = this._moduleDef();
2524 if (moduleDef) {
2525 getTestBed().configureTestingModule(moduleDef);
2526 }
2527 }
2528 inject(tokens, fn) {
2529 const self = this;
2530 // Not using an arrow function to preserve context passed from call site
2531 return function () {
2532 self._addModule();
2533 return inject(tokens, fn).call(this);
2534 };
2535 }
2536}
2537function withModule(moduleDef, fn) {
2538 if (fn) {
2539 // Not using an arrow function to preserve context passed from call site
2540 return function () {
2541 const testBed = getTestBed();
2542 if (moduleDef) {
2543 testBed.configureTestingModule(moduleDef);
2544 }
2545 return fn.apply(this);
2546 };
2547 }
2548 return new InjectSetupWrapper(() => moduleDef);
2549}
2550
2551/**
2552 * @license
2553 * Copyright Google LLC All Rights Reserved.
2554 *
2555 * Use of this source code is governed by an MIT-style license that can be
2556 * found in the LICENSE file at https://angular.io/license
2557 */
2558const _global = (typeof window === 'undefined' ? global : window);
2559// Reset the test providers and the fake async zone before each test.
2560if (_global.beforeEach) {
2561 _global.beforeEach(getCleanupHook(false));
2562}
2563// We provide both a `beforeEach` and `afterEach`, because the updated behavior for
2564// tearing down the module is supposed to run after the test so that we can associate
2565// teardown errors with the correct test.
2566if (_global.afterEach) {
2567 _global.afterEach(getCleanupHook(true));
2568}
2569function getCleanupHook(expectedTeardownValue) {
2570 return () => {
2571 if (TestBed.shouldTearDownTestingModule() ===
2572 expectedTeardownValue) {
2573 TestBed.resetTestingModule();
2574 resetFakeAsyncZone();
2575 }
2576 };
2577}
2578/**
2579 * This API should be removed. But doing so seems to break `google3` and so it requires a bit of
2580 * investigation.
2581 *
2582 * A work around is to mark it as `@codeGenApi` for now and investigate later.
2583 *
2584 * @codeGenApi
2585 */
2586// TODO(iminar): Remove this code in a safe way.
2587const __core_private_testing_placeholder__ = '';
2588
2589/**
2590 * @license
2591 * Copyright Google LLC All Rights Reserved.
2592 *
2593 * Use of this source code is governed by an MIT-style license that can be
2594 * found in the LICENSE file at https://angular.io/license
2595 */
2596
2597/**
2598 * @license
2599 * Copyright Google LLC All Rights Reserved.
2600 *
2601 * Use of this source code is governed by an MIT-style license that can be
2602 * found in the LICENSE file at https://angular.io/license
2603 */
2604
2605/**
2606 * @license
2607 * Copyright Google LLC All Rights Reserved.
2608 *
2609 * Use of this source code is governed by an MIT-style license that can be
2610 * found in the LICENSE file at https://angular.io/license
2611 */
2612// This file only reexports content of the `src` folder. Keep it that way.
2613
2614/**
2615 * @license
2616 * Copyright Google LLC All Rights Reserved.
2617 *
2618 * Use of this source code is governed by an MIT-style license that can be
2619 * found in the LICENSE file at https://angular.io/license
2620 */
2621
2622/**
2623 * Generated bundle index. Do not edit.
2624 */
2625
2626export { ComponentFixture, ComponentFixtureAutoDetect, ComponentFixtureNoNgZone, InjectSetupWrapper, TestBed, TestComponentRenderer, __core_private_testing_placeholder__, async, discardPeriodicTasks, fakeAsync, flush, flushMicrotasks, getTestBed, inject, resetFakeAsyncZone, tick, waitForAsync, withModule, MetadataOverrider as ɵMetadataOverrider, TestingCompiler as ɵTestingCompiler, TestingCompilerFactory as ɵTestingCompilerFactory };
2627//# sourceMappingURL=testing.mjs.map