This blog represents techniques to setup unit testing environment for doing unit tests for Angular components with external template and CSS files. The blog is applicable for Angular 2/Angular 4/Angular 5 versions.
Using first technique, BeforeEach method is called for two times:
describe('SignupComponent', () => { let fixture: ComponentFixture<SignupComponent>; let component: SignupComponent; beforeEach(async(() => { TestBed.configureTestingModule({ declarations: [ SignupComponent ], imports: [ FormsModule ], }).compileComponents(); })); beforeEach(() => { fixture = TestBed.createComponent(SignupComponent); component = fixture.componentInstance; }); it('should create the app', async(() => { expect(component).toBeTruthy(); })); });
In this technique, the synchronous execution of code related with creation of components etc is moved with TestBed.compileComponents.then(…) callback method. Note that the compileComponents method returns a promise so you can perform additional tasks immediately after it finishes.
describe('SignupComponent', () => { let fixture: ComponentFixture<SignupComponent>; let component: SignupComponent; beforeEach(async(() => { TestBed.configureTestingModule({ declarations: [ SignupComponent ], imports: [ FormsModule ], }).compileComponents().then(() => { fixture = TestBed.createComponent(SignupComponent); component = fixture.componentInstance; }); })); it('should create the app', async(() => { expect(component).toBeTruthy(); })); });
In case you are developing web apps using Spring and Angular, check out my book, Building web apps with Spring 5 and Angular. Grab your ebook today and get started.
Last updated: 15th May, 2024 Have you ever wondered how your bank decides what to…
In this fast-changing world, the ability to learn effectively is more valuable than ever. Whether…
Last updated: 13th May, 2024 Whether you are a researcher, data analyst, or data scientist,…
Last updated: 12th May, 2024 Data lakehouses are a relatively new concept in the data…
Last updated: 12th May 2024 In this blog, we get an overview of the machine…
Last updated: 12th May, 2024 In the world of generative AI models, autoencoders (AE) and…