-
Notifications
You must be signed in to change notification settings - Fork 27.2k
Expand file tree
/
Copy pathtest_bed.ts
More file actions
994 lines (870 loc) Β· 32.5 KB
/
test_bed.ts
File metadata and controls
994 lines (870 loc) Β· 32.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
// The formatter and CI disagree on how this import statement should be formatted. Both try to keep
// it on one line, too, which has gotten very hard to read & manage. So disable the formatter for
// this statement only.
import {
ApplicationRef,
Binding,
Component,
Ι΅Render3ComponentFactory as ComponentFactory,
ComponentRef,
Ι΅DeferBlockBehavior as DeferBlockBehavior,
Directive,
EnvironmentInjector,
Ι΅flushModuleScopingQueueAsMuchAsPossible as flushModuleScopingQueueAsMuchAsPossible,
Ι΅getAsyncClassMetadataFn as getAsyncClassMetadataFn,
Ι΅getUnknownElementStrictMode as getUnknownElementStrictMode,
Ι΅getUnknownPropertyStrictMode as getUnknownPropertyStrictMode,
InjectOptions,
Injector,
NgModule,
Ι΅Render3NgModuleRef as NgModuleRef,
NgZone,
Pipe,
PlatformRef,
ProviderToken,
Ι΅resetCompiledComponents as resetCompiledComponents,
runInInjectionContext,
Ι΅setAllowDuplicateNgModuleIdsForTest as setAllowDuplicateNgModuleIdsForTest,
Ι΅setUnknownElementStrictMode as setUnknownElementStrictMode,
Ι΅setUnknownPropertyStrictMode as setUnknownPropertyStrictMode,
Ι΅stringify as stringify,
Type,
Ι΅inferTagNameFromDefinition as inferTagNameFromDefinition,
Ι΅getComponentDef as getComponentDef,
} from '../../src/core';
import {ComponentFixture} from './component_fixture';
import {MetadataOverride} from './metadata_override';
import {
ANIMATIONS_ENABLED_DEFAULT,
ComponentFixtureNoNgZone,
DEFER_BLOCK_DEFAULT_BEHAVIOR,
ModuleTeardownOptions,
TEARDOWN_TESTING_MODULE_ON_DESTROY_DEFAULT,
TestComponentRenderer,
TestEnvironmentOptions,
TestModuleMetadata,
THROW_ON_UNKNOWN_ELEMENTS_DEFAULT,
THROW_ON_UNKNOWN_PROPERTIES_DEFAULT,
} from './test_bed_common';
import {TestBedCompiler} from './test_bed_compiler';
/**
* Static methods implemented by the `TestBed`.
*
* @publicApi
*/
export interface TestBedStatic extends TestBed {
new (...args: any[]): TestBed;
}
/**
* Options that can be configured for a test component.
*
* @publicApi
*/
export interface TestComponentOptions {
/** Bindings to apply to the test component. */
bindings?: Binding[];
/**
* Whether to infer the tag name of the test component from its selector.
* Otherwise `div` will be used as its tag name.
*/
inferTagName?: boolean;
}
/**
* @publicApi
*/
export interface TestBed {
get platform(): PlatformRef;
get ngModule(): Type<any> | Type<any>[];
/**
* Initialize the environment for testing with a compiler factory, a PlatformRef, and an
* angular module. These are common to every test in the suite.
*
* This may only be called once, to set up the common providers for the current test
* suite on the current platform. If you absolutely need to change the providers,
* first use `resetTestEnvironment`.
*
* Test modules and platforms for individual platforms are available from
* '@angular/<platform_name>/testing'.
*/
initTestEnvironment(
ngModule: Type<any> | Type<any>[],
platform: PlatformRef,
options?: TestEnvironmentOptions,
): void;
/**
* Reset the providers for the test injector.
*/
resetTestEnvironment(): void;
resetTestingModule(): TestBed;
configureCompiler(config: {providers?: any[]; useJit?: boolean}): void;
configureTestingModule(moduleDef: TestModuleMetadata): TestBed;
compileComponents(): Promise<any>;
inject<T>(
token: ProviderToken<T>,
notFoundValue: undefined,
options: InjectOptions & {
optional?: false;
},
): T;
inject<T>(
token: ProviderToken<T>,
notFoundValue: null | undefined,
options: InjectOptions,
): T | null;
inject<T>(token: ProviderToken<T>, notFoundValue?: T, options?: InjectOptions): T;
/**
* Runs the given function in the `EnvironmentInjector` context of `TestBed`.
*
* @see {@link https://angular.dev/api/core/EnvironmentInjector#runInContext}
*/
runInInjectionContext<T>(fn: () => T): T;
execute(tokens: any[], fn: Function, context?: any): any;
overrideModule(ngModule: Type<any>, override: MetadataOverride<NgModule>): TestBed;
overrideComponent(component: Type<any>, override: MetadataOverride<Component>): TestBed;
overrideDirective(directive: Type<any>, override: MetadataOverride<Directive>): TestBed;
overridePipe(pipe: Type<any>, override: MetadataOverride<Pipe>): TestBed;
overrideTemplate(component: Type<any>, template: string): TestBed;
/**
* Overwrites all providers for the given token with the given provider definition.
*/
overrideProvider(
token: any,
provider: {useFactory: Function; deps: any[]; multi?: boolean},
): TestBed;
overrideProvider(token: any, provider: {useValue: any; multi?: boolean}): TestBed;
overrideProvider(
token: any,
provider: {useFactory?: Function; useValue?: any; deps?: any[]; multi?: boolean},
): TestBed;
overrideTemplateUsingTestingModule(component: Type<any>, template: string): TestBed;
createComponent<T>(component: Type<T>, options?: TestComponentOptions): ComponentFixture<T>;
/**
* Returns the most recently created `ComponentFixture`, or throws an error if one has not
* yet been created.
*/
getLastFixture<T = unknown>(): ComponentFixture<T>;
/**
* Execute any pending effects.
*
* @deprecated use `TestBed.tick()` instead
*/
flushEffects(): void;
/**
* Execute any pending work required to synchronize model to the UI.
*
* @publicApi 20.0
*/
tick(): void;
}
let _nextRootElementId = 0;
/**
* Returns a singleton of the `TestBed` class.
*
* @publicApi
*/
export function getTestBed(): TestBed {
return TestBedImpl.INSTANCE;
}
/**
* @description
* Configures and initializes environment for unit testing and provides methods for
* creating components and services in unit tests.
*
* TestBed is the primary api for writing unit tests for Angular applications and libraries.
*/
export class TestBedImpl implements TestBed {
private static _INSTANCE: TestBedImpl | null = null;
static get INSTANCE(): TestBedImpl {
return (TestBedImpl._INSTANCE = TestBedImpl._INSTANCE || new TestBedImpl());
}
/**
* Teardown options that have been configured at the environment level.
* Used as a fallback if no instance-level options have been provided.
*/
private static _environmentTeardownOptions: ModuleTeardownOptions | undefined;
/**
* "Error on unknown elements" option that has been configured at the environment level.
* Used as a fallback if no instance-level option has been provided.
*/
private static _environmentErrorOnUnknownElementsOption: boolean | undefined;
/**
* "Error on unknown properties" option that has been configured at the environment level.
* Used as a fallback if no instance-level option has been provided.
*/
private static _environmentErrorOnUnknownPropertiesOption: boolean | undefined;
/**
* Teardown options that have been configured at the `TestBed` instance level.
* These options take precedence over the environment-level ones.
*/
private _instanceTeardownOptions: ModuleTeardownOptions | undefined;
/**
* Defer block behavior option that specifies whether defer blocks will be triggered manually
* or set to play through.
*/
private _instanceDeferBlockBehavior = DEFER_BLOCK_DEFAULT_BEHAVIOR;
/**
* Animations behavior option that specifies whether animations are enabled or disabled.
*/
private _instanceAnimationsEnabled = ANIMATIONS_ENABLED_DEFAULT;
/**
* "Error on unknown elements" option that has been configured at the `TestBed` instance level.
* This option takes precedence over the environment-level one.
*/
private _instanceErrorOnUnknownElementsOption: boolean | undefined;
/**
* "Error on unknown properties" option that has been configured at the `TestBed` instance level.
* This option takes precedence over the environment-level one.
*/
private _instanceErrorOnUnknownPropertiesOption: boolean | undefined;
/**
* Stores the previous "Error on unknown elements" option value,
* allowing to restore it in the reset testing module logic.
*/
private _previousErrorOnUnknownElementsOption: boolean | undefined;
/**
* Stores the previous "Error on unknown properties" option value,
* allowing to restore it in the reset testing module logic.
*/
private _previousErrorOnUnknownPropertiesOption: boolean | undefined;
/**
* Stores the value for `inferTagName` from the testing module.
*/
private _instanceInferTagName: boolean | undefined;
/**
* Initialize the environment for testing with a compiler factory, a PlatformRef, and an
* angular module. These are common to every test in the suite.
*
* This may only be called once, to set up the common providers for the current test
* suite on the current platform. If you absolutely need to change the providers,
* first use `resetTestEnvironment`.
*
* Test modules and platforms for individual platforms are available from
* '@angular/<platform_name>/testing'.
*
* @publicApi
*/
static initTestEnvironment(
ngModule: Type<any> | Type<any>[],
platform: PlatformRef,
options?: TestEnvironmentOptions,
): TestBed {
const testBed = TestBedImpl.INSTANCE;
testBed.initTestEnvironment(ngModule, platform, options);
return testBed;
}
/**
* Reset the providers for the test injector.
*
* @publicApi
*/
static resetTestEnvironment(): void {
TestBedImpl.INSTANCE.resetTestEnvironment();
}
static configureCompiler(config: {providers?: any[]; useJit?: boolean}): TestBed {
return TestBedImpl.INSTANCE.configureCompiler(config);
}
/**
* Allows overriding default providers, directives, pipes, modules of the test injector,
* which are defined in test_injector.js
*/
static configureTestingModule(moduleDef: TestModuleMetadata): TestBed {
return TestBedImpl.INSTANCE.configureTestingModule(moduleDef);
}
/**
* Compile components with a `templateUrl` for the test's NgModule.
* It is necessary to call this function
* as fetching urls is asynchronous.
*/
static compileComponents(): Promise<any> {
return TestBedImpl.INSTANCE.compileComponents();
}
static overrideModule(ngModule: Type<any>, override: MetadataOverride<NgModule>): TestBed {
return TestBedImpl.INSTANCE.overrideModule(ngModule, override);
}
static overrideComponent(component: Type<any>, override: MetadataOverride<Component>): TestBed {
return TestBedImpl.INSTANCE.overrideComponent(component, override);
}
static overrideDirective(directive: Type<any>, override: MetadataOverride<Directive>): TestBed {
return TestBedImpl.INSTANCE.overrideDirective(directive, override);
}
static overridePipe(pipe: Type<any>, override: MetadataOverride<Pipe>): TestBed {
return TestBedImpl.INSTANCE.overridePipe(pipe, override);
}
static overrideTemplate(component: Type<any>, template: string): TestBed {
return TestBedImpl.INSTANCE.overrideTemplate(component, template);
}
/**
* Overrides the template of the given component, compiling the template
* in the context of the TestingModule.
*
* Note: This works for JIT and AOTed components as well.
*/
static overrideTemplateUsingTestingModule(component: Type<any>, template: string): TestBed {
return TestBedImpl.INSTANCE.overrideTemplateUsingTestingModule(component, template);
}
static overrideProvider(
token: any,
provider: {
useFactory: Function;
deps: any[];
},
): TestBed;
static overrideProvider(token: any, provider: {useValue: any}): TestBed;
static overrideProvider(
token: any,
provider: {
useFactory?: Function;
useValue?: any;
deps?: any[];
},
): TestBed {
return TestBedImpl.INSTANCE.overrideProvider(token, provider);
}
static inject<T>(
token: ProviderToken<T>,
notFoundValue: undefined,
options: InjectOptions & {
optional?: false;
},
): T;
static inject<T>(
token: ProviderToken<T>,
notFoundValue: null | undefined,
options: InjectOptions,
): T | null;
static inject<T>(token: ProviderToken<T>, notFoundValue?: T, options?: InjectOptions): T;
static inject<T>(
token: ProviderToken<T>,
notFoundValue?: T | null,
options?: InjectOptions,
): T | null {
return TestBedImpl.INSTANCE.inject(token, notFoundValue, options);
}
/**
* Runs the given function in the `EnvironmentInjector` context of `TestBed`.
*
* @see {@link https://angular.dev/api/core/EnvironmentInjector#runInContext}
*/
static runInInjectionContext<T>(fn: () => T): T {
return TestBedImpl.INSTANCE.runInInjectionContext(fn);
}
static createComponent<T>(
component: Type<T>,
options?: TestComponentOptions,
): ComponentFixture<T> {
return TestBedImpl.INSTANCE.createComponent(component, options);
}
static getLastFixture<T = unknown>(): ComponentFixture<T> {
return TestBedImpl.INSTANCE.getLastFixture();
}
static resetTestingModule(): TestBed {
return TestBedImpl.INSTANCE.resetTestingModule();
}
static execute(tokens: any[], fn: Function, context?: any): any {
return TestBedImpl.INSTANCE.execute(tokens, fn, context);
}
static get platform(): PlatformRef {
return TestBedImpl.INSTANCE.platform;
}
static get ngModule(): Type<any> | Type<any>[] {
return TestBedImpl.INSTANCE.ngModule;
}
static flushEffects(): void {
return TestBedImpl.INSTANCE.tick();
}
static tick(): void {
return TestBedImpl.INSTANCE.tick();
}
// Properties
platform: PlatformRef = null!;
ngModule: Type<any> | Type<any>[] = null!;
private _compiler: TestBedCompiler | null = null;
private _testModuleRef: NgModuleRef<any> | null = null;
private _activeFixtures: ComponentFixture<any>[] = [];
/**
* Internal-only flag to indicate whether a module
* scoping queue has been checked and flushed already.
* @docs-private
*/
globalCompilationChecked = false;
/**
* Initialize the environment for testing with a compiler factory, a PlatformRef, and an
* angular module. These are common to every test in the suite.
*
* This may only be called once, to set up the common providers for the current test
* suite on the current platform. If you absolutely need to change the providers,
* first use `resetTestEnvironment`.
*
* Test modules and platforms for individual platforms are available from
* '@angular/<platform_name>/testing'.
*
* @publicApi
*/
initTestEnvironment(
ngModule: Type<any> | Type<any>[],
platform: PlatformRef,
options?: TestEnvironmentOptions,
): void {
if (this.platform || this.ngModule) {
throw new Error('Cannot set base providers because it has already been called');
}
TestBedImpl._environmentTeardownOptions = options?.teardown;
TestBedImpl._environmentErrorOnUnknownElementsOption = options?.errorOnUnknownElements;
TestBedImpl._environmentErrorOnUnknownPropertiesOption = options?.errorOnUnknownProperties;
this.platform = platform;
this.ngModule = ngModule;
this._compiler = new TestBedCompiler(this.platform, this.ngModule);
// TestBed does not have an API which can reliably detect the start of a test, and thus could be
// used to track the state of the NgModule registry and reset it correctly. Instead, when we
// know we're in a testing scenario, we disable the check for duplicate NgModule registration
// completely.
setAllowDuplicateNgModuleIdsForTest(true);
}
/**
* Reset the providers for the test injector.
*
* @publicApi
*/
resetTestEnvironment(): void {
this.resetTestingModule();
this._compiler = null;
this.platform = null!;
this.ngModule = null!;
TestBedImpl._environmentTeardownOptions = undefined;
setAllowDuplicateNgModuleIdsForTest(false);
}
resetTestingModule(): this {
this.checkGlobalCompilationFinished();
resetCompiledComponents();
if (this._compiler !== null) {
this.compiler.restoreOriginalState();
}
this._compiler = new TestBedCompiler(this.platform, this.ngModule);
// Restore the previous value of the "error on unknown elements" option
setUnknownElementStrictMode(
this._previousErrorOnUnknownElementsOption ?? THROW_ON_UNKNOWN_ELEMENTS_DEFAULT,
);
// Restore the previous value of the "error on unknown properties" option
setUnknownPropertyStrictMode(
this._previousErrorOnUnknownPropertiesOption ?? THROW_ON_UNKNOWN_PROPERTIES_DEFAULT,
);
// We have to chain a couple of try/finally blocks, because each step can
// throw errors and we don't want it to interrupt the next step and we also
// want an error to be thrown at the end.
try {
this.destroyActiveFixtures();
} finally {
try {
if (this.shouldTearDownTestingModule()) {
this.tearDownTestingModule();
}
} finally {
this._testModuleRef = null;
this._instanceTeardownOptions = undefined;
this._instanceErrorOnUnknownElementsOption = undefined;
this._instanceErrorOnUnknownPropertiesOption = undefined;
this._instanceInferTagName = undefined;
this._instanceDeferBlockBehavior = DEFER_BLOCK_DEFAULT_BEHAVIOR;
this._instanceAnimationsEnabled = ANIMATIONS_ENABLED_DEFAULT;
}
}
return this;
}
configureCompiler(config: {providers?: any[]; useJit?: boolean}): this {
if (config.useJit != null) {
throw new Error('JIT compiler is not configurable via TestBed APIs.');
}
if (config.providers !== undefined) {
this.compiler.setCompilerProviders(config.providers);
}
return this;
}
configureTestingModule(moduleDef: TestModuleMetadata): this {
this.assertNotInstantiated('TestBed.configureTestingModule', 'configure the test module');
// Trigger module scoping queue flush before executing other TestBed operations in a test.
// This is needed for the first test invocation to ensure that globally declared modules have
// their components scoped properly. See the `checkGlobalCompilationFinished` function
// description for additional info.
this.checkGlobalCompilationFinished();
// Always re-assign the options, even if they're undefined.
// This ensures that we don't carry them between tests.
this._instanceTeardownOptions = moduleDef.teardown;
this._instanceErrorOnUnknownElementsOption = moduleDef.errorOnUnknownElements;
this._instanceErrorOnUnknownPropertiesOption = moduleDef.errorOnUnknownProperties;
this._instanceInferTagName = moduleDef.inferTagName;
this._instanceDeferBlockBehavior = moduleDef.deferBlockBehavior ?? DEFER_BLOCK_DEFAULT_BEHAVIOR;
this._instanceAnimationsEnabled = moduleDef.animationsEnabled ?? ANIMATIONS_ENABLED_DEFAULT;
// Store the current value of the strict mode option,
// so we can restore it later
this._previousErrorOnUnknownElementsOption = getUnknownElementStrictMode();
setUnknownElementStrictMode(this.shouldThrowErrorOnUnknownElements());
this._previousErrorOnUnknownPropertiesOption = getUnknownPropertyStrictMode();
setUnknownPropertyStrictMode(this.shouldThrowErrorOnUnknownProperties());
this.compiler.configureTestingModule(moduleDef);
return this;
}
compileComponents(): Promise<any> {
return this.compiler.compileComponents();
}
inject<T>(
token: ProviderToken<T>,
notFoundValue: undefined,
options: InjectOptions & {
optional: true;
},
): T | null;
inject<T>(token: ProviderToken<T>, notFoundValue?: T, options?: InjectOptions): T;
inject<T>(token: ProviderToken<T>, notFoundValue: null, options?: InjectOptions): T | null;
inject<T>(token: ProviderToken<T>, notFoundValue?: T | null, options?: InjectOptions): T | null {
if ((token as unknown) === TestBed) {
return this as any;
}
const UNDEFINED = {} as unknown as T;
const result = this.testModuleRef.injector.get(token, UNDEFINED, options);
return result === UNDEFINED
? (this.compiler.injector.get(token, notFoundValue, options) as any)
: result;
}
runInInjectionContext<T>(fn: () => T): T {
return runInInjectionContext(this.inject(EnvironmentInjector), fn);
}
execute(tokens: any[], fn: Function, context?: any): any {
const params = tokens.map((t) => this.inject(t));
return fn.apply(context, params);
}
overrideModule(ngModule: Type<any>, override: MetadataOverride<NgModule>): this {
this.assertNotInstantiated('overrideModule', 'override module metadata');
this.compiler.overrideModule(ngModule, override);
return this;
}
overrideComponent(component: Type<any>, override: MetadataOverride<Component>): this {
this.assertNotInstantiated('overrideComponent', 'override component metadata');
this.compiler.overrideComponent(component, override);
return this;
}
overrideTemplateUsingTestingModule(component: Type<any>, template: string): this {
this.assertNotInstantiated(
'TestBed.overrideTemplateUsingTestingModule',
'Cannot override template when the test module has already been instantiated',
);
this.compiler.overrideTemplateUsingTestingModule(component, template);
return this;
}
overrideDirective(directive: Type<any>, override: MetadataOverride<Directive>): this {
this.assertNotInstantiated('overrideDirective', 'override directive metadata');
this.compiler.overrideDirective(directive, override);
return this;
}
overridePipe(pipe: Type<any>, override: MetadataOverride<Pipe>): this {
this.assertNotInstantiated('overridePipe', 'override pipe metadata');
this.compiler.overridePipe(pipe, override);
return this;
}
/**
* Overwrites all providers for the given token with the given provider definition.
*/
overrideProvider(
token: any,
provider: {useFactory?: Function; useValue?: any; deps?: any[]},
): this {
this.assertNotInstantiated('overrideProvider', 'override provider');
this.compiler.overrideProvider(token, provider);
return this;
}
overrideTemplate(component: Type<any>, template: string): TestBed {
return this.overrideComponent(component, {set: {template, templateUrl: null!}});
}
createComponent<T>(type: Type<T>, options?: TestComponentOptions): ComponentFixture<T> {
if (getAsyncClassMetadataFn(type)) {
throw new Error(
`Component '${type.name}' has unresolved metadata. ` +
`Please call \`await TestBed.compileComponents()\` before running this test.`,
);
}
// Note: injecting the renderer before accessing the definition appears to be load-bearing.
const testComponentRenderer = this.inject(TestComponentRenderer);
const shouldInferTagName = options?.inferTagName ?? this._instanceInferTagName ?? false;
const componentDef = getComponentDef(type);
const rootElId = `root${_nextRootElementId++}`;
if (!componentDef) {
throw new Error(`It looks like '${stringify(type)}' has not been compiled.`);
}
testComponentRenderer.insertRootElement(
rootElId,
shouldInferTagName ? inferTagNameFromDefinition(componentDef) : undefined,
);
const componentFactory = new ComponentFactory(componentDef);
const initComponent = () => {
const componentRef = componentFactory.create(
Injector.NULL,
[],
`#${rootElId}`,
this.testModuleRef,
undefined,
options?.bindings,
) as ComponentRef<T>;
return this.runInInjectionContext(() => new ComponentFixture(componentRef));
};
const noNgZone = this.inject(ComponentFixtureNoNgZone, false);
const ngZone = noNgZone ? null : this.inject(NgZone, null);
const fixture = ngZone ? ngZone.run(initComponent) : initComponent();
this._activeFixtures.push(fixture);
return fixture;
}
getLastFixture<T = unknown>(): ComponentFixture<T> {
if (this._activeFixtures.length === 0) {
throw new Error('No fixture has been created yet.');
}
return this._activeFixtures[this._activeFixtures.length - 1];
}
/**
* @internal strip this from published d.ts files due to
* https://github.com/microsoft/TypeScript/issues/36216
*/
private get compiler(): TestBedCompiler {
if (this._compiler === null) {
throw new Error(`Need to call TestBed.initTestEnvironment() first`);
}
return this._compiler;
}
/**
* @internal strip this from published d.ts files due to
* https://github.com/microsoft/TypeScript/issues/36216
*/
private get testModuleRef(): NgModuleRef<any> {
if (this._testModuleRef === null) {
this._testModuleRef = this.compiler.finalize();
}
return this._testModuleRef;
}
private assertNotInstantiated(methodName: string, methodDescription: string) {
if (this._testModuleRef !== null) {
throw new Error(
`Cannot ${methodDescription} when the test module has already been instantiated. ` +
`Make sure you are not using \`inject\` before \`${methodName}\`.`,
);
}
}
/**
* Check whether the module scoping queue should be flushed, and flush it if needed.
*
* When the TestBed is reset, it clears the JIT module compilation queue, cancelling any
* in-progress module compilation. This creates a potential hazard - the very first time the
* TestBed is initialized (or if it's reset without being initialized), there may be pending
* compilations of modules declared in global scope. These compilations should be finished.
*
* To ensure that globally declared modules have their components scoped properly, this function
* is called whenever TestBed is initialized or reset. The _first_ time that this happens, prior
* to any other operations, the scoping queue is flushed.
*/
private checkGlobalCompilationFinished(): void {
// Checking _testNgModuleRef is null should not be necessary, but is left in as an additional
// guard that compilations queued in tests (after instantiation) are never flushed accidentally.
if (!this.globalCompilationChecked && this._testModuleRef === null) {
flushModuleScopingQueueAsMuchAsPossible();
}
this.globalCompilationChecked = true;
}
private destroyActiveFixtures(): void {
let errorCount = 0;
this._activeFixtures.forEach((fixture) => {
try {
fixture.destroy();
} catch (e) {
errorCount++;
console.error('Error during cleanup of component', {
component: fixture.componentInstance,
stacktrace: e,
});
}
});
this._activeFixtures = [];
if (errorCount > 0 && this.shouldRethrowTeardownErrors()) {
throw Error(
`${errorCount} ${errorCount === 1 ? 'component' : 'components'} ` +
`threw errors during cleanup`,
);
}
}
shouldRethrowTeardownErrors(): boolean {
const instanceOptions = this._instanceTeardownOptions;
const environmentOptions = TestBedImpl._environmentTeardownOptions;
// If the new teardown behavior hasn't been configured, preserve the old behavior.
if (!instanceOptions && !environmentOptions) {
return TEARDOWN_TESTING_MODULE_ON_DESTROY_DEFAULT;
}
// Otherwise use the configured behavior or default to rethrowing.
return (
instanceOptions?.rethrowErrors ??
environmentOptions?.rethrowErrors ??
this.shouldTearDownTestingModule()
);
}
shouldThrowErrorOnUnknownElements(): boolean {
// Check if a configuration has been provided to throw when an unknown element is found
return (
this._instanceErrorOnUnknownElementsOption ??
TestBedImpl._environmentErrorOnUnknownElementsOption ??
THROW_ON_UNKNOWN_ELEMENTS_DEFAULT
);
}
shouldThrowErrorOnUnknownProperties(): boolean {
// Check if a configuration has been provided to throw when an unknown property is found
return (
this._instanceErrorOnUnknownPropertiesOption ??
TestBedImpl._environmentErrorOnUnknownPropertiesOption ??
THROW_ON_UNKNOWN_PROPERTIES_DEFAULT
);
}
shouldTearDownTestingModule(): boolean {
return (
this._instanceTeardownOptions?.destroyAfterEach ??
TestBedImpl._environmentTeardownOptions?.destroyAfterEach ??
TEARDOWN_TESTING_MODULE_ON_DESTROY_DEFAULT
);
}
getDeferBlockBehavior(): DeferBlockBehavior {
return this._instanceDeferBlockBehavior;
}
getAnimationsEnabled(): boolean {
return this._instanceAnimationsEnabled;
}
tearDownTestingModule() {
// If the module ref has already been destroyed, we won't be able to get a test renderer.
if (this._testModuleRef === null) {
return;
}
// Resolve the renderer ahead of time, because we want to remove the root elements as the very
// last step, but the injector will be destroyed as a part of the module ref destruction.
const testRenderer = this.inject(TestComponentRenderer);
try {
this._testModuleRef.destroy();
} catch (e) {
if (this.shouldRethrowTeardownErrors()) {
throw e;
} else {
console.error('Error during cleanup of a testing module', {
component: this._testModuleRef.instance,
stacktrace: e,
});
}
} finally {
testRenderer.removeAllRootElements?.();
}
}
/**
* Execute any pending effects by executing any pending work required to synchronize model to the UI.
*
* @deprecated use `TestBed.tick()` instead
*/
flushEffects(): void {
this.tick();
}
/**
* Execute any pending work required to synchronize model to the UI.
*
* @publicApi
*/
tick(): void {
const appRef = this.inject(ApplicationRef);
try {
// TODO(atscott): ApplicationRef.tick should set includeAllTestViews to true itself rather than doing this here and in ComponentFixture
// The behavior should be that TestBed.tick, ComponentFixture.detectChanges, and ApplicationRef.tick all result in the test fixtures
// getting synchronized, regardless of whether they are autoDetect: true.
// Automatic scheduling (zone or zoneless) will call _tick which will _not_ include fixtures with autoDetect: false
// If this does get changed, we will need a new flag for the scheduler to use to omit the microtask scheduling
// from a tick initiated by tests.
(appRef as any).includeAllTestViews = true;
appRef.tick();
} finally {
(appRef as any).includeAllTestViews = false;
}
}
}
/**
* @description
* Configures and initializes environment for unit testing and provides methods for
* creating components and services in unit tests.
*
* `TestBed` is the primary api for writing unit tests for Angular applications and libraries.
*
* @publicApi
*/
export const TestBed: TestBedStatic = TestBedImpl;
/**
* Allows injecting dependencies in `beforeEach()` and `it()`. Note: this function
* (imported from the `@angular/core/testing` package) can **only** be used to inject dependencies
* in tests. To inject dependencies in your application code, use the [`inject`](api/core/inject)
* function from the `@angular/core` package instead.
*
* Example:
*
* ```ts
* beforeEach(inject([Dependency, AClass], (dep, object) => {
* // some code that uses `dep` and `object`
* // ...
* }));
*
* it('...', inject([AClass], (object) => {
* object.doSomething();
* expect(...);
* })
* ```
*
* @publicApi
*/
export function inject(tokens: any[], fn: Function): () => any {
const testBed = TestBedImpl.INSTANCE;
// Not using an arrow function to preserve context passed from call site
return function (this: unknown) {
return testBed.execute(tokens, fn, this);
};
}
/**
* @publicApi
*/
export class InjectSetupWrapper {
constructor(private _moduleDef: () => TestModuleMetadata) {}
private _addModule() {
const moduleDef = this._moduleDef();
if (moduleDef) {
TestBedImpl.configureTestingModule(moduleDef);
}
}
inject(tokens: any[], fn: Function): () => any {
const self = this;
// Not using an arrow function to preserve context passed from call site
return function (this: unknown) {
self._addModule();
return inject(tokens, fn).call(this);
};
}
}
/**
* @publicApi
*/
export function withModule(moduleDef: TestModuleMetadata): InjectSetupWrapper;
export function withModule(moduleDef: TestModuleMetadata, fn: Function): () => any;
export function withModule(
moduleDef: TestModuleMetadata,
fn?: Function | null,
): (() => any) | InjectSetupWrapper {
if (fn) {
// Not using an arrow function to preserve context passed from call site
return function (this: unknown) {
const testBed = TestBedImpl.INSTANCE;
if (moduleDef) {
testBed.configureTestingModule(moduleDef);
}
return fn.apply(this);
};
}
return new InjectSetupWrapper(() => moduleDef);
}