@@ -24,10 +24,12 @@ import {
2424 ProviderToken ,
2525 Type ,
2626 ɵflushModuleScopingQueueAsMuchAsPossible as flushModuleScopingQueueAsMuchAsPossible ,
27+ ɵgetUnknownElementStrictMode as getUnknownElementStrictMode ,
2728 ɵRender3ComponentFactory as ComponentFactory ,
2829 ɵRender3NgModuleRef as NgModuleRef ,
2930 ɵresetCompiledComponents as resetCompiledComponents ,
3031 ɵsetAllowDuplicateNgModuleIdsForTest as setAllowDuplicateNgModuleIdsForTest ,
32+ ɵsetUnknownElementStrictMode as setUnknownElementStrictMode ,
3133 ɵstringify as stringify ,
3234} from '@angular/core' ;
3335
@@ -37,7 +39,7 @@ import {ComponentFixture} from './component_fixture';
3739import { MetadataOverride } from './metadata_override' ;
3840import { R3TestBedCompiler } from './r3_test_bed_compiler' ;
3941import { 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' ;
4143
4244let _nextRootElementId = 0 ;
4345
@@ -59,12 +61,30 @@ export class TestBedRender3 implements TestBed {
5961 */
6062 private static _environmentTeardownOptions : ModuleTeardownOptions | undefined ;
6163
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+
6270 /**
6371 * 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.
6573 */
6674 private _instanceTeardownOptions : ModuleTeardownOptions | undefined ;
6775
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+
6888 /**
6989 * Initialize the environment for testing with a compiler factory, a PlatformRef, and an
7090 * angular module. These are common to every test in the suite.
@@ -237,6 +257,8 @@ export class TestBedRender3 implements TestBed {
237257
238258 TestBedRender3 . _environmentTeardownOptions = options ?. teardown ;
239259
260+ TestBedRender3 . _environmentErrorOnUnknownElementsOption = options ?. errorOnUnknownElements ;
261+
240262 this . platform = platform ;
241263 this . ngModule = ngModule ;
242264 this . _compiler = new R3TestBedCompiler ( this . platform , this . ngModule ) ;
@@ -269,6 +291,9 @@ export class TestBedRender3 implements TestBed {
269291 this . compiler . restoreOriginalState ( ) ;
270292 }
271293 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 ) ;
272297
273298 // We have to chain a couple of try/finally blocks, because each step can
274299 // 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 {
283308 } finally {
284309 this . _testModuleRef = null ;
285310 this . _instanceTeardownOptions = undefined ;
311+ this . _instanceErrorOnUnknownElementsOption = undefined ;
286312 }
287313 }
288314 }
@@ -306,9 +332,14 @@ export class TestBedRender3 implements TestBed {
306332 // description for additional info.
307333 this . checkGlobalCompilationFinished ( ) ;
308334
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.
311337 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 ( ) ) ;
312343 this . compiler . configureTestingModule ( moduleDef ) ;
313344 }
314345
@@ -481,7 +512,7 @@ export class TestBedRender3 implements TestBed {
481512 }
482513 }
483514
484- shouldRethrowTeardownErrors ( ) {
515+ shouldRethrowTeardownErrors ( ) : boolean {
485516 const instanceOptions = this . _instanceTeardownOptions ;
486517 const environmentOptions = TestBedRender3 . _environmentTeardownOptions ;
487518
@@ -495,6 +526,13 @@ export class TestBedRender3 implements TestBed {
495526 this . shouldTearDownTestingModule ( ) ;
496527 }
497528
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+
498536 shouldTearDownTestingModule ( ) : boolean {
499537 return this . _instanceTeardownOptions ?. destroyAfterEach ??
500538 TestBedRender3 . _environmentTeardownOptions ?. destroyAfterEach ??
0 commit comments