For example, say you have the following component:
import { Another } from "./Another";
@Component({
entryComponents: [
Another
]
})
export class App {}
Even when using NO_ERRORS_SCHEMA
I still have to include Another
as part of the test declarations:
import { ComponentFixture, TestBed } from "@angular/core/testing";
import { NO_ERRORS_SCHEMA } from '@angular/core';
import { App } from "./App";
import { Another } from "./Another";
describe("App", () => {
let comp: App;
let fixture: ComponentFixture<App>;
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [ App, Another ],
schemas: [ NO_ERRORS_SCHEMA ]
});
fixture = TestBed.createComponent(App);
comp = fixture.componentInstance;
});
it("can load instance", () => {
expect(comp).toBeTruthy();
});
});
Shallow tests might be good quick win practice optimizing tests. Isolation. It's quite common to add a new dependency to a resuable directive or component. It tends to break other tests because something is missing in dependency injection tree.
entryComponents has been deprecated since Angular 9 (check our blog post for more details). When updating to v13. 1, a schematics will automatically remove entryComponents from your @Component and @NgModule if you still have some.
Difference between entry Component and Declaration: entryComponents are used to register components for offline computation in a module. These components are referenced here as they not referenced anywhere else in HTML template. Declarations are used to make Directives(components, pipes etc.) in a specific module.
The entryComponent is the component which loads angular by force, that means these components are not referenced in the HTML template. In most of the cases, Angular loads a component when it is explicitly declared in the component template.
They plan on adding EntryComponents
to the testing module interface. See issue: https://github.com/angular/angular/issues/10760
For a current workaround see the Angular Material library see https://github.com/angular/components/blob/master/src/material/dialog/dialog.spec.ts#L1660.
Basically, they create a real module on the fly and then import it for the tests.
// Create a real (non-test) NgModule as a workaround for
// https://github.com/angular/angular/issues/10760
const TEST_DIRECTIVES = [
ComponentWithChildViewContainer,
PizzaMsg,
DirectiveWithViewContainer,
ContentElementDialog
];
@NgModule({
imports: [MdDialogModule],
exports: TEST_DIRECTIVES,
declarations: TEST_DIRECTIVES,
entryComponents: [ComponentWithChildViewContainer, PizzaMsg, ContentElementDialog],
})
class DialogTestModule { }
Now you can use DialogTestModule
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [MdDialogModule.forRoot(), DialogTestModule]
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With