Skip to content

Commit 6a3ca0e

Browse files
committed
Revert "feat(core): allow to throw on unknown elements in tests (#45479)" (#45839)
This reverts commit 6662a97. PR Close #45839
1 parent 9c2f219 commit 6a3ca0e

File tree

8 files changed

+8
-231
lines changed

8 files changed

+8
-231
lines changed

goldens/public-api/core/testing/index.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,6 @@ export class TestComponentRenderer {
215215

216216
// @public (undocumented)
217217
export interface TestEnvironmentOptions {
218-
errorOnUnknownElements?: boolean;
219218
teardown?: ModuleTeardownOptions;
220219
}
221220

@@ -226,7 +225,6 @@ export type TestModuleMetadata = {
226225
imports?: any[];
227226
schemas?: Array<SchemaMetadata | any[]>;
228227
teardown?: ModuleTeardownOptions;
229-
errorOnUnknownElements?: boolean;
230228
};
231229

232230
// @public

packages/core/src/core_render3_private_export.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,6 @@ export {
209209
ɵɵtextInterpolate8,
210210
ɵɵtextInterpolateV,
211211
ɵɵviewQuery,
212-
ɵgetUnknownElementStrictMode,
213-
ɵsetUnknownElementStrictMode
214212
} from './render3/index';
215213
export {
216214
LContext as ɵLContext,

packages/core/src/render3/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,6 @@ export {
129129
ɵɵtextInterpolate7,
130130
ɵɵtextInterpolate8,
131131
ɵɵtextInterpolateV,
132-
ɵgetUnknownElementStrictMode,
133-
ɵsetUnknownElementStrictMode
134132
} from './instructions/all';
135133
export {ɵɵi18n, ɵɵi18nApply, ɵɵi18nAttributes, ɵɵi18nEnd, ɵɵi18nExp,ɵɵi18nPostprocess, ɵɵi18nStart} from './instructions/i18n';
136134
export {RenderFlags} from './interfaces/definition';

packages/core/src/render3/instructions/element.ts

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {formatRuntimeError, RuntimeError, RuntimeErrorCode} from '../../errors';
9+
import {formatRuntimeError, RuntimeErrorCode} from '../../errors';
1010
import {SchemaMetadata} from '../../metadata/schema';
1111
import {assertDefined, assertEqual, assertIndexInRange} from '../../util/assert';
1212
import {assertFirstCreatePass, assertHasParent} from '../assert';
@@ -26,23 +26,7 @@ import {getConstant} from '../util/view_utils';
2626
import {setDirectiveInputsWhichShadowsStyling} from './property';
2727
import {createDirectivesInstances, executeContentQueries, getOrCreateTNode, matchingSchemas, resolveDirectives, saveResolvedLocalsInData} from './shared';
2828

29-
let shouldThrowErrorOnUnknownElement = false;
3029

31-
/**
32-
* Sets a strict mode for JIT-compiled components to throw an error on unknown elements,
33-
* instead of just logging the error.
34-
* (for AOT-compiled ones this check happens at build time).
35-
*/
36-
export function ɵsetUnknownElementStrictMode(shouldThrow: boolean) {
37-
shouldThrowErrorOnUnknownElement = shouldThrow;
38-
}
39-
40-
/**
41-
* Gets the current value of the strict mode.
42-
*/
43-
export function ɵgetUnknownElementStrictMode() {
44-
return shouldThrowErrorOnUnknownElement;
45-
}
4630

4731
function elementStartFirstCreatePass(
4832
index: number, tView: TView, lView: LView, native: RElement, name: string,
@@ -257,11 +241,7 @@ function validateElementIsKnown(
257241
message +=
258242
`2. To allow any element add 'NO_ERRORS_SCHEMA' to the '@NgModule.schemas' of this component.`;
259243
}
260-
if (shouldThrowErrorOnUnknownElement) {
261-
throw new RuntimeError(RuntimeErrorCode.UNKNOWN_ELEMENT, message);
262-
} else {
263-
console.error(formatRuntimeError(RuntimeErrorCode.UNKNOWN_ELEMENT, message));
264-
}
244+
console.error(formatRuntimeError(RuntimeErrorCode.UNKNOWN_ELEMENT, message));
265245
}
266246
}
267247
}

packages/core/test/acceptance/ng_module_spec.ts

Lines changed: 0 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -331,19 +331,6 @@ describe('NgModule', () => {
331331
expect(spy).not.toHaveBeenCalled();
332332
});
333333

334-
it('should throw an error about unknown element without CUSTOM_ELEMENTS_SCHEMA for element with dash in tag name',
335-
() => {
336-
@Component({template: `<custom-el></custom-el>`})
337-
class MyComp {
338-
}
339-
340-
TestBed.configureTestingModule({declarations: [MyComp], errorOnUnknownElements: true});
341-
expect(() => {
342-
const fixture = TestBed.createComponent(MyComp);
343-
fixture.detectChanges();
344-
}).toThrowError(/NG0304: 'custom-el' is not a known element/g);
345-
});
346-
347334
it('should log an error about unknown element without CUSTOM_ELEMENTS_SCHEMA for element without dash in tag name',
348335
() => {
349336
@Component({template: `<custom></custom>`})
@@ -357,19 +344,6 @@ describe('NgModule', () => {
357344
expect(spy.calls.mostRecent().args[0]).toMatch(/'custom' is not a known element/);
358345
});
359346

360-
it('should throw an error about unknown element without CUSTOM_ELEMENTS_SCHEMA for element without dash in tag name',
361-
() => {
362-
@Component({template: `<custom></custom>`})
363-
class MyComp {
364-
}
365-
366-
TestBed.configureTestingModule({declarations: [MyComp], errorOnUnknownElements: true});
367-
expect(() => {
368-
const fixture = TestBed.createComponent(MyComp);
369-
fixture.detectChanges();
370-
}).toThrowError(/NG0304: 'custom' is not a known element/g);
371-
});
372-
373347
it('should report unknown property bindings on ng-content', () => {
374348
@Component({template: `<ng-content *unknownProp="123"></ng-content>`})
375349
class App {
@@ -531,25 +505,6 @@ describe('NgModule', () => {
531505
expect(spy).not.toHaveBeenCalled();
532506
});
533507

534-
it('should not throw an error about unknown elements with CUSTOM_ELEMENTS_SCHEMA', () => {
535-
@Component({template: `<custom-el></custom-el>`})
536-
class MyComp {
537-
}
538-
539-
const spy = spyOn(console, 'error');
540-
TestBed.configureTestingModule({
541-
declarations: [MyComp],
542-
schemas: [CUSTOM_ELEMENTS_SCHEMA],
543-
errorOnUnknownElements: true
544-
});
545-
546-
const fixture = TestBed.createComponent(MyComp);
547-
fixture.detectChanges();
548-
// We do not expect any errors being thrown or logged in a console,
549-
// since the `CUSTOM_ELEMENTS_SCHEMA` is applied.
550-
expect(spy).not.toHaveBeenCalled();
551-
});
552-
553508
it('should not log an error about unknown elements with NO_ERRORS_SCHEMA', () => {
554509
@Component({template: `<custom-el></custom-el>`})
555510
class MyComp {
@@ -566,22 +521,6 @@ describe('NgModule', () => {
566521
expect(spy).not.toHaveBeenCalled();
567522
});
568523

569-
it('should not throw an error about unknown elements with NO_ERRORS_SCHEMA', () => {
570-
@Component({template: `<custom-el></custom-el>`})
571-
class MyComp {
572-
}
573-
574-
const spy = spyOn(console, 'error');
575-
TestBed.configureTestingModule(
576-
{declarations: [MyComp], schemas: [NO_ERRORS_SCHEMA], errorOnUnknownElements: true});
577-
578-
const fixture = TestBed.createComponent(MyComp);
579-
fixture.detectChanges();
580-
// We do not expect any errors being thrown or logged in a console,
581-
// since the `NO_ERRORS_SCHEMA` is applied.
582-
expect(spy).not.toHaveBeenCalled();
583-
});
584-
585524
it('should not log an error about unknown elements if element matches a directive', () => {
586525
@Component({
587526
selector: 'custom-el',
@@ -602,29 +541,6 @@ describe('NgModule', () => {
602541
expect(spy).not.toHaveBeenCalled();
603542
});
604543

605-
it('should not throw an error about unknown elements if element matches a directive', () => {
606-
@Component({
607-
selector: 'custom-el',
608-
template: '',
609-
})
610-
class CustomEl {
611-
}
612-
613-
@Component({template: `<custom-el></custom-el>`})
614-
class MyComp {
615-
}
616-
617-
const spy = spyOn(console, 'error');
618-
TestBed.configureTestingModule(
619-
{declarations: [MyComp, CustomEl], errorOnUnknownElements: true});
620-
621-
const fixture = TestBed.createComponent(MyComp);
622-
fixture.detectChanges();
623-
// We do not expect any errors being thrown or logged in a console,
624-
// since the element matches a directive.
625-
expect(spy).not.toHaveBeenCalled();
626-
});
627-
628544
it('should not log an error for HTML elements inside an SVG foreignObject', () => {
629545
@Component({
630546
template: `
@@ -649,33 +565,6 @@ describe('NgModule', () => {
649565
fixture.detectChanges();
650566
expect(spy).not.toHaveBeenCalled();
651567
});
652-
653-
it('should not throw an error for HTML elements inside an SVG foreignObject', () => {
654-
@Component({
655-
template: `
656-
<svg>
657-
<svg:foreignObject>
658-
<xhtml:div>Hello</xhtml:div>
659-
</svg:foreignObject>
660-
</svg>
661-
`,
662-
})
663-
class MyComp {
664-
}
665-
666-
@NgModule({declarations: [MyComp]})
667-
class MyModule {
668-
}
669-
670-
const spy = spyOn(console, 'error');
671-
TestBed.configureTestingModule({imports: [MyModule], errorOnUnknownElements: true});
672-
673-
const fixture = TestBed.createComponent(MyComp);
674-
fixture.detectChanges();
675-
// We do not expect any errors being thrown or logged in a console,
676-
// since the element is inside an SVG foreignObject.
677-
expect(spy).not.toHaveBeenCalled();
678-
});
679568
});
680569

681570
describe('createNgModuleRef function', () => {

packages/core/test/test_bed_spec.ts

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {expect} from '@angular/platform-browser/testing/src/matchers';
1313

1414
import {getNgModuleById} from '../public_api';
1515
import {TestBedRender3} from '../testing/src/r3_test_bed';
16-
import {TEARDOWN_TESTING_MODULE_ON_DESTROY_DEFAULT, THROW_ON_UNKNOWN_ELEMENTS_DEFAULT} from '../testing/src/test_bed_common';
16+
import {TEARDOWN_TESTING_MODULE_ON_DESTROY_DEFAULT} from '../testing/src/test_bed_common';
1717

1818
const NAME = new InjectionToken<string>('name');
1919

@@ -1714,34 +1714,3 @@ describe('TestBed module teardown', () => {
17141714
expect(TestBed.shouldRethrowTeardownErrors()).toBe(false);
17151715
});
17161716
});
1717-
1718-
describe('TestBed module `errorOnUnknownElements`', () => {
1719-
// Cast the `TestBed` to the internal data type since we're testing private APIs.
1720-
let TestBed: TestBedRender3;
1721-
1722-
beforeEach(() => {
1723-
TestBed = getTestBed() as unknown as TestBedRender3;
1724-
TestBed.resetTestingModule();
1725-
});
1726-
1727-
it('should not throw based on the default behavior', () => {
1728-
expect(TestBed.shouldThrowErrorOnUnknownElements()).toBe(THROW_ON_UNKNOWN_ELEMENTS_DEFAULT);
1729-
});
1730-
1731-
it('should not throw if the option is omitted', () => {
1732-
TestBed.configureTestingModule({});
1733-
expect(TestBed.shouldThrowErrorOnUnknownElements()).toBe(false);
1734-
});
1735-
1736-
it('should be able to configure the option', () => {
1737-
TestBed.configureTestingModule({errorOnUnknownElements: true});
1738-
expect(TestBed.shouldThrowErrorOnUnknownElements()).toBe(true);
1739-
});
1740-
1741-
it('should reset the option back to the default when TestBed is reset', () => {
1742-
TestBed.configureTestingModule({errorOnUnknownElements: true});
1743-
expect(TestBed.shouldThrowErrorOnUnknownElements()).toBe(true);
1744-
TestBed.resetTestingModule();
1745-
expect(TestBed.shouldThrowErrorOnUnknownElements()).toBe(false);
1746-
});
1747-
});

packages/core/testing/src/r3_test_bed.ts

Lines changed: 5 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,10 @@ import {
2424
ProviderToken,
2525
Type,
2626
ɵflushModuleScopingQueueAsMuchAsPossible as flushModuleScopingQueueAsMuchAsPossible,
27-
ɵgetUnknownElementStrictMode as getUnknownElementStrictMode,
2827
ɵRender3ComponentFactory as ComponentFactory,
2928
ɵRender3NgModuleRef as NgModuleRef,
3029
ɵresetCompiledComponents as resetCompiledComponents,
3130
ɵsetAllowDuplicateNgModuleIdsForTest as setAllowDuplicateNgModuleIdsForTest,
32-
ɵsetUnknownElementStrictMode as setUnknownElementStrictMode,
3331
ɵstringify as stringify,
3432
} from '@angular/core';
3533

@@ -39,7 +37,7 @@ import {ComponentFixture} from './component_fixture';
3937
import {MetadataOverride} from './metadata_override';
4038
import {R3TestBedCompiler} from './r3_test_bed_compiler';
4139
import {TestBed} from './test_bed';
42-
import {ComponentFixtureAutoDetect, ComponentFixtureNoNgZone, ModuleTeardownOptions, TEARDOWN_TESTING_MODULE_ON_DESTROY_DEFAULT, TestBedStatic, TestComponentRenderer, TestEnvironmentOptions, TestModuleMetadata, THROW_ON_UNKNOWN_ELEMENTS_DEFAULT} from './test_bed_common';
40+
import {ComponentFixtureAutoDetect, ComponentFixtureNoNgZone, ModuleTeardownOptions, TEARDOWN_TESTING_MODULE_ON_DESTROY_DEFAULT, TestBedStatic, TestComponentRenderer, TestEnvironmentOptions, TestModuleMetadata} from './test_bed_common';
4341

4442
let _nextRootElementId = 0;
4543

@@ -61,30 +59,12 @@ export class TestBedRender3 implements TestBed {
6159
*/
6260
private static _environmentTeardownOptions: ModuleTeardownOptions|undefined;
6361

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-
7062
/**
7163
* Teardown options that have been configured at the `TestBed` instance level.
72-
* These options take precedence over the environment-level ones.
64+
* These options take precedence over the environemnt-level ones.
7365
*/
7466
private _instanceTeardownOptions: ModuleTeardownOptions|undefined;
7567

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-
8868
/**
8969
* Initialize the environment for testing with a compiler factory, a PlatformRef, and an
9070
* angular module. These are common to every test in the suite.
@@ -257,8 +237,6 @@ export class TestBedRender3 implements TestBed {
257237

258238
TestBedRender3._environmentTeardownOptions = options?.teardown;
259239

260-
TestBedRender3._environmentErrorOnUnknownElementsOption = options?.errorOnUnknownElements;
261-
262240
this.platform = platform;
263241
this.ngModule = ngModule;
264242
this._compiler = new R3TestBedCompiler(this.platform, this.ngModule);
@@ -291,9 +269,6 @@ export class TestBedRender3 implements TestBed {
291269
this.compiler.restoreOriginalState();
292270
}
293271
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);
297272

298273
// We have to chain a couple of try/finally blocks, because each step can
299274
// throw errors and we don't want it to interrupt the next step and we also
@@ -308,7 +283,6 @@ export class TestBedRender3 implements TestBed {
308283
} finally {
309284
this._testModuleRef = null;
310285
this._instanceTeardownOptions = undefined;
311-
this._instanceErrorOnUnknownElementsOption = undefined;
312286
}
313287
}
314288
}
@@ -332,14 +306,9 @@ export class TestBedRender3 implements TestBed {
332306
// description for additional info.
333307
this.checkGlobalCompilationFinished();
334308

335-
// Always re-assign the options, even if they're undefined.
336-
// This ensures that we don't carry them between tests.
309+
// Always re-assign the teardown options, even if they're undefined.
310+
// This ensures that we don't carry the options between tests.
337311
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());
343312
this.compiler.configureTestingModule(moduleDef);
344313
}
345314

@@ -512,7 +481,7 @@ export class TestBedRender3 implements TestBed {
512481
}
513482
}
514483

515-
shouldRethrowTeardownErrors(): boolean {
484+
shouldRethrowTeardownErrors() {
516485
const instanceOptions = this._instanceTeardownOptions;
517486
const environmentOptions = TestBedRender3._environmentTeardownOptions;
518487

@@ -526,13 +495,6 @@ export class TestBedRender3 implements TestBed {
526495
this.shouldTearDownTestingModule();
527496
}
528497

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-
536498
shouldTearDownTestingModule(): boolean {
537499
return this._instanceTeardownOptions?.destroyAfterEach ??
538500
TestBedRender3._environmentTeardownOptions?.destroyAfterEach ??

0 commit comments

Comments
 (0)