@@ -24,10 +24,12 @@ import {
24
24
ProviderToken ,
25
25
Type ,
26
26
ɵflushModuleScopingQueueAsMuchAsPossible as flushModuleScopingQueueAsMuchAsPossible ,
27
+ ɵgetUnknownElementStrictMode as getUnknownElementStrictMode ,
27
28
ɵRender3ComponentFactory as ComponentFactory ,
28
29
ɵRender3NgModuleRef as NgModuleRef ,
29
30
ɵresetCompiledComponents as resetCompiledComponents ,
30
31
ɵsetAllowDuplicateNgModuleIdsForTest as setAllowDuplicateNgModuleIdsForTest ,
32
+ ɵsetUnknownElementStrictMode as setUnknownElementStrictMode ,
31
33
ɵstringify as stringify ,
32
34
} from '@angular/core' ;
33
35
@@ -37,7 +39,7 @@ import {ComponentFixture} from './component_fixture';
37
39
import { MetadataOverride } from './metadata_override' ;
38
40
import { R3TestBedCompiler } from './r3_test_bed_compiler' ;
39
41
import { TestBed } from './test_bed' ;
40
- import { ComponentFixtureAutoDetect , ComponentFixtureNoNgZone , ModuleTeardownOptions , TEARDOWN_TESTING_MODULE_ON_DESTROY_DEFAULT , TestBedStatic , TestComponentRenderer , TestEnvironmentOptions , TestModuleMetadata } from './test_bed_common' ;
42
+ import { ComponentFixtureAutoDetect , ComponentFixtureNoNgZone , ModuleTeardownOptions , TEARDOWN_TESTING_MODULE_ON_DESTROY_DEFAULT , TestBedStatic , TestComponentRenderer , TestEnvironmentOptions , TestModuleMetadata , THROW_ON_UNKNOWN_ELEMENTS_DEFAULT } from './test_bed_common' ;
41
43
42
44
let _nextRootElementId = 0 ;
43
45
@@ -59,12 +61,30 @@ export class TestBedRender3 implements TestBed {
59
61
*/
60
62
private static _environmentTeardownOptions : ModuleTeardownOptions | undefined ;
61
63
64
+ /**
65
+ * "Error on unknown elements" option that has been configured at the environment level.
66
+ * Used as a fallback if no instance-level option has been provided.
67
+ */
68
+ private static _environmentErrorOnUnknownElementsOption : boolean | undefined ;
69
+
62
70
/**
63
71
* Teardown options that have been configured at the `TestBed` instance level.
64
- * These options take precedence over the environemnt -level ones.
72
+ * These options take precedence over the environment -level ones.
65
73
*/
66
74
private _instanceTeardownOptions : ModuleTeardownOptions | undefined ;
67
75
76
+ /**
77
+ * "Error on unknown elements" option that has been configured at the `TestBed` instance level.
78
+ * This option takes precedence over the environment-level one.
79
+ */
80
+ private _instanceErrorOnUnknownElementsOption : boolean | undefined ;
81
+
82
+ /**
83
+ * Stores the previous "Error on unknown elements" option value,
84
+ * allowing to restore it in the reset testing module logic.
85
+ */
86
+ private _previousErrorOnUnknownElementsOption : boolean | undefined ;
87
+
68
88
/**
69
89
* Initialize the environment for testing with a compiler factory, a PlatformRef, and an
70
90
* angular module. These are common to every test in the suite.
@@ -237,6 +257,8 @@ export class TestBedRender3 implements TestBed {
237
257
238
258
TestBedRender3 . _environmentTeardownOptions = options ?. teardown ;
239
259
260
+ TestBedRender3 . _environmentErrorOnUnknownElementsOption = options ?. errorOnUnknownElements ;
261
+
240
262
this . platform = platform ;
241
263
this . ngModule = ngModule ;
242
264
this . _compiler = new R3TestBedCompiler ( this . platform , this . ngModule ) ;
@@ -269,6 +291,9 @@ export class TestBedRender3 implements TestBed {
269
291
this . compiler . restoreOriginalState ( ) ;
270
292
}
271
293
this . _compiler = new R3TestBedCompiler ( this . platform , this . ngModule ) ;
294
+ // Restore the previous value of the "error on unknown elements" option
295
+ setUnknownElementStrictMode (
296
+ this . _previousErrorOnUnknownElementsOption ?? THROW_ON_UNKNOWN_ELEMENTS_DEFAULT ) ;
272
297
273
298
// We have to chain a couple of try/finally blocks, because each step can
274
299
// throw errors and we don't want it to interrupt the next step and we also
@@ -283,6 +308,7 @@ export class TestBedRender3 implements TestBed {
283
308
} finally {
284
309
this . _testModuleRef = null ;
285
310
this . _instanceTeardownOptions = undefined ;
311
+ this . _instanceErrorOnUnknownElementsOption = undefined ;
286
312
}
287
313
}
288
314
}
@@ -306,9 +332,14 @@ export class TestBedRender3 implements TestBed {
306
332
// description for additional info.
307
333
this . checkGlobalCompilationFinished ( ) ;
308
334
309
- // Always re-assign the teardown options, even if they're undefined.
310
- // This ensures that we don't carry the options between tests.
335
+ // Always re-assign the options, even if they're undefined.
336
+ // This ensures that we don't carry them between tests.
311
337
this . _instanceTeardownOptions = moduleDef . teardown ;
338
+ this . _instanceErrorOnUnknownElementsOption = moduleDef . errorOnUnknownElements ;
339
+ // Store the current value of the strict mode option,
340
+ // so we can restore it later
341
+ this . _previousErrorOnUnknownElementsOption = getUnknownElementStrictMode ( ) ;
342
+ setUnknownElementStrictMode ( this . shouldThrowErrorOnUnknownElements ( ) ) ;
312
343
this . compiler . configureTestingModule ( moduleDef ) ;
313
344
}
314
345
@@ -481,7 +512,7 @@ export class TestBedRender3 implements TestBed {
481
512
}
482
513
}
483
514
484
- shouldRethrowTeardownErrors ( ) {
515
+ shouldRethrowTeardownErrors ( ) : boolean {
485
516
const instanceOptions = this . _instanceTeardownOptions ;
486
517
const environmentOptions = TestBedRender3 . _environmentTeardownOptions ;
487
518
@@ -495,6 +526,13 @@ export class TestBedRender3 implements TestBed {
495
526
this . shouldTearDownTestingModule ( ) ;
496
527
}
497
528
529
+ shouldThrowErrorOnUnknownElements ( ) : boolean {
530
+ // Check if a configuration has been provided to throw when an unknown element is found
531
+ return this . _instanceErrorOnUnknownElementsOption ??
532
+ TestBedRender3 . _environmentErrorOnUnknownElementsOption ??
533
+ THROW_ON_UNKNOWN_ELEMENTS_DEFAULT ;
534
+ }
535
+
498
536
shouldTearDownTestingModule ( ) : boolean {
499
537
return this . _instanceTeardownOptions ?. destroyAfterEach ??
500
538
TestBedRender3 . _environmentTeardownOptions ?. destroyAfterEach ??
0 commit comments