feat(core): allow to throw on unknown elements in tests (#45479)

Allows to provide a TestBed option to throw on unknown elements in templates:

```ts
getTestBed().initTestEnvironment(
  BrowserDynamicTestingModule,
  platformBrowserDynamicTesting(), {
    errorOnUnknownElements: true
  }
);
```

The default value of `errorOnUnknownElements` is `false`, so this is not a breaking change.

PR Close #45479
This commit is contained in:
Cédric Exbrayat
2022-03-30 19:21:48 +02:00
committed by Dylan Hunn
parent 6a3ca0eb45
commit e702cafcf2
8 changed files with 231 additions and 8 deletions
@@ -209,6 +209,8 @@ export {
ɵɵtextInterpolate8,
ɵɵtextInterpolateV,
ɵɵviewQuery,
ɵgetUnknownElementStrictMode,
ɵsetUnknownElementStrictMode
} from './render3/index';
export {
LContext as ɵLContext,
+2
View File
@@ -129,6 +129,8 @@ export {
ɵɵtextInterpolate7,
ɵɵtextInterpolate8,
ɵɵtextInterpolateV,
ɵgetUnknownElementStrictMode,
ɵsetUnknownElementStrictMode
} from './instructions/all';
export {ɵɵi18n, ɵɵi18nApply, ɵɵi18nAttributes, ɵɵi18nEnd, ɵɵi18nExp,ɵɵi18nPostprocess, ɵɵi18nStart} from './instructions/i18n';
export {RenderFlags} from './interfaces/definition';
@@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/
import {formatRuntimeError, RuntimeErrorCode} from '../../errors';
import {formatRuntimeError, RuntimeError, RuntimeErrorCode} from '../../errors';
import {SchemaMetadata} from '../../metadata/schema';
import {assertDefined, assertEqual, assertIndexInRange} from '../../util/assert';
import {assertFirstCreatePass, assertHasParent} from '../assert';
@@ -26,7 +26,23 @@ import {getConstant} from '../util/view_utils';
import {setDirectiveInputsWhichShadowsStyling} from './property';
import {createDirectivesInstances, executeContentQueries, getOrCreateTNode, matchingSchemas, resolveDirectives, saveResolvedLocalsInData} from './shared';
let shouldThrowErrorOnUnknownElement = false;
/**
* Sets a strict mode for JIT-compiled components to throw an error on unknown elements,
* instead of just logging the error.
* (for AOT-compiled ones this check happens at build time).
*/
export function ɵsetUnknownElementStrictMode(shouldThrow: boolean) {
shouldThrowErrorOnUnknownElement = shouldThrow;
}
/**
* Gets the current value of the strict mode.
*/
export function ɵgetUnknownElementStrictMode() {
return shouldThrowErrorOnUnknownElement;
}
function elementStartFirstCreatePass(
index: number, tView: TView, lView: LView, native: RElement, name: string,
@@ -241,7 +257,11 @@ function validateElementIsKnown(
message +=
`2. To allow any element add 'NO_ERRORS_SCHEMA' to the '@NgModule.schemas' of this component.`;
}
console.error(formatRuntimeError(RuntimeErrorCode.UNKNOWN_ELEMENT, message));
if (shouldThrowErrorOnUnknownElement) {
throw new RuntimeError(RuntimeErrorCode.UNKNOWN_ELEMENT, message);
} else {
console.error(formatRuntimeError(RuntimeErrorCode.UNKNOWN_ELEMENT, message));
}
}
}
}