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: 25th Jan, 2025 Have you ever wondered how to seamlessly integrate the vast…
Hey there! As I venture into building agentic MEAN apps with LangChain.js, I wanted to…
Software-as-a-Service (SaaS) providers have long relied on traditional chatbot solutions like AWS Lex and Google…
Retrieval-Augmented Generation (RAG) is an innovative generative AI method that combines retrieval-based search with large…
The combination of Retrieval-Augmented Generation (RAG) and powerful language models enables the development of sophisticated…
Have you ever wondered how to use OpenAI APIs to create custom chatbots? With advancements…