mirror of
https://github.com/angular/angular.git
synced 2026-09-14 13:54:52 +08:00
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:
committed by
Dylan Hunn
parent
6a3ca0eb45
commit
e702cafcf2
@@ -209,6 +209,8 @@ export {
|
||||
ɵɵtextInterpolate8,
|
||||
ɵɵtextInterpolateV,
|
||||
ɵɵviewQuery,
|
||||
ɵgetUnknownElementStrictMode,
|
||||
ɵsetUnknownElementStrictMode
|
||||
} from './render3/index';
|
||||
export {
|
||||
LContext as ɵLContext,
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user