💻 Tech
In Angular, Jasmine is the default testing framework. One way to run specific test suite (describe()
) or test case (it()
) is by using fdescribe()
and fit()
.
fdescribe()
and fit()
stand for focused describe()
and it()
respectively. They only run test cases inside an fdescribe
or a single test case it()
.
Below is the code for AppComponent and ChildComponent. Even with fdescribe
and fit
, all the test cases are executed because AppComponent
focuses tests on all the test cases in it, and ChildComponent
focuses on a specific test case, which is also the the only test case it has. The number of test cases executed is the same with the number of test cases executed even without those focused versions.
fdescribe('AppComponent', () => {
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [AppComponent],
}).compileComponents();
});
it('should create the app', () => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.componentInstance;
expect(app).toBeTruthy();
});
it(`should have the 'unittest' title`, () => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.componentInstance;
expect(app.title).toEqual('unittest');
});
it('should render title', () => {
const fixture = TestBed.createComponent(AppComponent);
fixture.detectChanges();
const compiled = fixture.nativeElement as HTMLElement;
expect(compiled.querySelector('h1')?.textContent).toContain('Hello, unittest');
});
});
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { ChildComponent } from './child.component';
describe('ChildComponent', () => {
let component: ChildComponent;
let fixture: ComponentFixture<ChildComponent>;
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [ChildComponent]
})
.compileComponents();
fixture = TestBed.createComponent(ChildComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
fit('should create', () => {
expect(component).toBeTruthy();
});
});